Question : DataList - need dynamic ImageUrl for an <asp:Image> tag

I'm new to asp.net and still getting use to the different way of programming. I have an image that I want to display at the top of a products page. The image name is based on a querystring that is passed into the page. I've created a function to get the image filename, but it comes back with blank string. So I'm not sure if I can do it this way or I need to call the function in a different way. Thanks for you help.

'** Products.aspx **
<%@ Page Language="VB" Debug="true" MasterPageFile="~/_newPg/MasterPage.master" AutoEventWireup="false" CodeFile="Products.aspx.vb" Inherits="_newPg_Products" title="Products Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Path: <asp:Image ID="imgCategory" runat="server"  ImageUrl='<%#  GetImagePath() %>' />

  <asp:DataList ID="dlistProducts" runat="server" GridLines="Both" RepeatColumns="2"
    style="z-index: 101; left:80px; position: relative; top: 60px">
    <Headertemplate>Products</Headertemplate>
   
    <ItemTemplate>      
      <u><b><%#DataBinder.Eval(Container.DataItem, "ProductName")%></b></u><br />
      <b><%#DataBinder.Eval(Container.DataItem, "TagLine")%></b><br />
      <%#DataBinder.Eval(Container.DataItem, "ShortDescription")%><br /><br />

      <b><%#DataBinder.Eval(Container.DataItem, "ProductCode")%></b>&nbsp
      <%#DataBinder.Eval(Container.DataItem, "QtyandType")%><br /><br />
    </ItemTemplate>
   
    <SeparatorTemplate>
      <br /><hr /><br />
    </SeparatorTemplate>
   
    <FooterTemplate>
    <br /><br />
      <table width="333" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
        <tr>
          <td align="center" bordercolor="#FFFFFF" class="notes_text">
            These statements have not been evaluated by the Food and Drug Administration. These
            products are not intended to diagnose, treat, cure, or prevent any disease.
          </td>
        </tr>
      </table>
    </FooterTemplate>
   
  </asp:DataList><br /><br />
 
</asp:Content>

'** Products.aspx.vb (part) **
Imports System.Data
Imports System.Data.Sqlclient
Imports System.Data.Common    'DbDataRecord

Partial Class _newPg_Products

  Inherits System.Web.UI.Page

  Dim strCategoryType As String

 Protected Function GetImagePath() As String
    GetImagePath = "/images/" & strCategoryType & ".gif"
    'GetImagePath = "/images/" & "aa.gif"  'strCategoryType & ".gif"
  End Function


  Protected Sub dlistProducts_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles dlistProducts.Init
    strCategoryType = Request.QueryString("cat")
  End Sub

  Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    strCategoryType = Request.QueryString("cat")
  End Sub

'** From Webbrowser source **
Path: <img id="ctl00_ContentPlaceHolder1_imgCategory" src="" style="border-width:0px;" />
- the "src" is coming back with blank.

Note - you can see that I've also tried just hardcoding a name to see if the function was working. And it is not. thx again.

Answer : DataList - need dynamic ImageUrl for an <asp:Image> tag

Hi pttoy;

This should do what you want.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Dim width As Double = (ListBox1.ActualWidth / 3) - 2

For Each es As Employees_Structure In myEmp
    ' Create a new ListBox item for es
    Dim li As New ListBoxItem
    ' Create a Stack panel 
    Dim sp As New StackPanel
    sp.Orientation = Orientation.Horizontal
    sp.HorizontalAlignment = HorizontalAlignment.Stretch
    ' Create a TextBlock to place the info for a column in ListBox
    ' one for each column
    Dim tb1 As New TextBlock
    tb1.Text = es.LastName
    tb1.Width = width
    Dim tb2 As New TextBlock
    tb2.Text = es.FirstName
    tb2.Width = width
    Dim tb3 As New TextBlock
    tb3.Text = es.State
    tb3.Width = width
    ' Add the TextBox to the stack panel
    sp.Children.Add(tb1)
    sp.Children.Add(tb2)
    sp.Children.Add(tb3)
    ' Add the stack panel to the list box item
    li.Content = sp
    ' Add the list box item to the list box
    ListBox1.Items.Add(li)
Next
Random Solutions  
 
programming4us programming4us