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 Repeater
control sorts out the IDs for you, you don't need to care what they are.
If you mean that each Hyperlink
is an image, then you need to handle the ItemDataBound
event of the Repeater
. In the markup, you give the Hyperlink
an ID. In the ItemDataBound
event handler, you use the FindControl
method on the Item
object you get from the event argument, passing the ID of the Hyperlink
. This will give you the actual hyperlink control. Then just set the image.
For example:
void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HyperLink hyperLink = e.Item.FindControl("HappyLink") as HyperLink;
if (hyperLink != null)
{
// do something with the hyperlink
}
}
In the FindControl
method, you just use the ID you set in the markup. The use of e.Item
ensures you get the hyperlink from the right row of the repeater.
精彩评论