简体   繁体   中英

Specifying Image Button image URL in code behind of asp.net page?

I've been trying to figure out how to do this for a while now, because the ID of the Image Button I created ('titanBtn') is not recognised in my code behind file. Which would allow me to simply write 'titanBtn.ImageUrl =" but alas, the code behind says it doesn't exist.

All I am trying to do with this code, is take a string read from the Db, that specifies the filename of the image (the appropriate images are named after the element type of each object), and puts this in a concatenated string that specifies the ImageUrl.

<asp:Repeater ID="titanRptr" runat="server">
    <ItemTemplate>
        <table>
            <tr class="tr1">
                <td><%# Eval("TitanName") %></td>
                <td>
                    <asp:ImageButton ID="titanBtn" runat="server" Width="100px" Height="100px" OnClick="titanBtn_Click" />
                </td>
                <td>Level: <%# Eval("char_lvl")%>
                    <br />
                    Step: <%# Eval("step")%>
                    <br />

                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

code behind

using (SqlCommand cmd2 = new SqlCommand("getElement", conn))
                    {
                        cmd2.CommandType = CommandType.StoredProcedure;
                        cmd2.Parameters.AddWithValue("@uid", titanElement);
                        using (SqlDataAdapter sda = new SqlDataAdapter(cmd2))
                        {
                            SqlDataReader reader;
                            conn.Open();
                            reader = cmd2.ExecuteReader();
                            while (reader.Read())
                            {

                                titanImage = Convert.ToString(reader["Element"]);
                                titanBtn.ImageUrl = "images/" + titanImage+ ".gif"; //"The name 'titanBtn' does not exist in the current context"
                                titanRptr

                            }
                            conn.Close();
                        }
                    }

You can access the ImageButton of each item in the ItemDataBound event:

protected void titanRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ImageButton titanBtn = e.Item.FindControl("titanBtn") as ImageButton;
    ...
}

However, if you could get the Element data directly in the data source of the repeater, you could do this in the markup:

<asp:ImageButton ID="titanBtn" runat="server" ImageUrl='<%# "images/" + Eval("Element") + ".gif" %>' ... />

Your ImageButton is inside a Repeater so cannot be accessed by ID.

However, you can use FindControl to get this control and there are (at least) two ways you can go about this.

On your Repeater you can attach an event to the ItemDataBound ( <asp:Repeater ID="titanRptr" runat="server" OnItemDataBound="titanRptr_ItemDataBound"> ) and then do tasks for each row of the repeater as it is data bound. For example...

protected virtual void titanRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        ImageButton titanBtn = (ImageButton)e.Item.FindControl("titanBtn");
        //etc etc
        //get any values you need (eg)
        int something = (int)DataBinder.Eval(e.Item.DataItem, "something");

Or you can loop through the repeater after it has been databound and access each row and find the control, eg...

foreach (RepeaterItem row in titanRptr.Items)
{
    ImageButton titanBtn = (ImageButton)row.FindControl("titanBtn");
    //etc etc

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM