views:

2809

answers:

4

This is my Repeater

<asp:Repeater ID="blogRepeater" runat="server">
                <ItemTemplate> 
                <br />       
                    <asp:Image ID="Image1" runat="server" Height="56px" ImageUrl='<%= string.Format( My_Variable) %>' Width="64px" />
                    <asp:HyperLink ID="HyperLink2" runat=server NavigateUrl='<%#Eval("Company_ID", "CompanyProfile.aspx?ID={0}")%>'><%#Eval("Name")%></asp:HyperLink>
                    <br /> 
                </ItemTemplate>
                <SeparatorTemplate>
                    <hr />
                </SeparatorTemplate>
            </asp:Repeater>

This is my Code behind in Page Load

' Define data objects
        Dim conn As Data.SqlClient.SqlConnection
        Dim Comm As Data.SqlClient.SqlCommand
        Dim reader As Data.SqlClient.SqlDataReader

        conn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        Comm = New Data.SqlClient.SqlCommand( _
        ("SELECT Company_ID, Name FROM CompanyTable ORDER BY Name"), conn)

        Dim My_Variable As String

        My_Variable = "~/createthumb.ashx?gu=/images/Logo.bmp" + "&xmax=75&ymax=75"

        ' Open the connection
        conn.Open()
        ' Execute the category command
        reader = Comm.ExecuteReader()

        ' Bind the reader to the repeater.......................
        blogRepeater.DataSource = reader

        blogRepeater.DataBind()

        ' Close the reader
        reader.Close()
        ' Close the connection
        conn.Close()

Now I have a variable called My_Variable. How can i place that variable My_Variable inside my Repeater above?

Any help will be great!

A: 

You need create protected method which return string for example:

 protected string GetCustomString(object MyVariable)

{ retun string.Format("{0}", MyVaraible.ToString()); }

You can generate html link on this method. and in your repeater just bind it method in need place.

<asp:Repeater ID="blogRepeater" runat="server">
            <ItemTemplate> 
             <%# GetCustomString(Eval("My_Variable")) %>

   <asp:HyperLink ID="HyperLink2" runat=server NavigateUrl='<%#Eval("My_Variable", "CompanyProfile.aspx?ID={0}")%>'><%#Eval("Name")%></asp:HyperLink>
                <br />
Maksim Kondratyuk
Please review my Updated code: i actually need to change the Image URL.........
Etienne
+2  A: 

In this way you can bind your variable to repeater :

<a href='<%= string.Format("CompanyProfile.aspx?ID={0}", My_Variable) %>'>
    <%# Eval("Name") %>
</a>

EDIT : You're using static controls that doesn't need to be server controls. So you can use HTML elements and response.write for your global variable like that :

<img id="Image1" style="height:56px;width:64px;" src='<%= My_Variable %>' />
Canavar
Please review my Updated code: i actually need to change the Image URL.........
Etienne
Nope, does not work.....
Etienne
A: 

you are not really have the db-code in codebehind right? this is just as an example? your site will be hacked within five minutes...

My_Variable have to be declared as a field not as a local variable in a function. Also databinding cannot see private fields.

oh now I see you need to change the variable on every item right?

i suggest you create a dataobject to contain the data from db and set all relevant data on each object and then databind the repeater on the list. I realize I should show some code here but it is 2 years I wrote vb last so it would be wildly inaccurate anyway.

zzzuperfly
What do you mean with the db-code in codebehind? I am not using my actual table names and values if that is what u are asking?
Etienne
A: 

To set the image url you must use a data binding expression. Also, you will need to make your variable 'My_Variable' a public class member.

<asp:Repeater ID="blogRepeater" runat="server">
    <ItemTemplate> 
        <br />       
        <asp:Image ID="Image1" runat="server" Height="56px" ImageUrl='<%# My_Variable %>' Width="64px" />
        <asp:HyperLink ID="HyperLink2" runat=server NavigateUrl='<%#Eval("Company_ID", "CompanyProfile.aspx?ID={0}")%>'><%#Eval("Name")%></asp:HyperLink>
        <br /> 
    </ItemTemplate>
    <SeparatorTemplate>
       <hr />
    </SeparatorTemplate>
</asp:Repeater>
Phaedrus