ref: Dynamic Control ID
Does anyone have a working example of creating the ID property of a hyperlink dynamically?
I have a repeater with multiple hyperlinks drawn (3 per row for a survey). I would like to change the image of the hyperlink clicked. The repeater is created from its bound datasource. Cant get it working
EDIT:I used your example and it does change the image, however it changes all the ID=”HappyLink” items instead of the one selected.
// if the happy emoticon was clicked
if (this.Request.QueryString["hyperlink"] == "HappyLink")
{
HyperLink happylink = e.Item.FindControl("HappyLink") as HyperLink;
if (happylink != null)
{
happylink.ImageUrl = "~/images/happy_selected.jpg";
} // if (happylink != null)
} // if (this.Request.QueryString["hyperlink"] == "HappyLink")
I don’t think you need to worry about dynamic IDs. The
Repeatercontrol sorts out the IDs for you, you don’t need to care what they are.If you mean that each
Hyperlinkis an image, then you need to handle theItemDataBoundevent of theRepeater. In the markup, you give theHyperlinkan ID. In theItemDataBoundevent handler, you use theFindControlmethod on theItemobject you get from the event argument, passing the ID of theHyperlink. This will give you the actual hyperlink control. Then just set the image.For example:
In the
FindControlmethod, you just use the ID you set in the markup. The use ofe.Itemensures you get the hyperlink from the right row of the repeater.