I have a repeater control in my aspx page:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<img src="pic/iconnew.jpg"/>
</td>
<td>
<asp:LinkButton ID="linkbtTitle" runat="server" Text='<%#Eval("title")%>' CommandArgument='<%#Eval("id_notic")%>' OnCommand="linkbtTitle_Click" OnClick="Buttonlink_Click" ></asp:LinkButton>
</td>
<td>
<asp:Label ID="LabelTime" runat="server" Text='<%#Eval("time")%>' CssClass="TimeMessage" ></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
I want to set the selected style for linkbutton linkbtTitle
by Buttonlink_Click
event:
<script runat="server">
protected void Buttonlink_Click(object sender, System.EventArgs e)
{
linkbtTitle.ForeColor = System.Drawing.Color.HotPink;
}
</script>
But it has an error, cannot resolve symbol linkbtTitle
, why??? Just because the linkbutton is inside the Repeater so I cannot access it by that way above.
How???, help!!!
A repeater is one of the web-databound controls that, well, it repeats items. So there are normally more than one item. That's why you cannot access it directly. It sits in a different NamingContainer
which is the RepeaterItem
of the repeater.
However, the control that raised an event is always the sender
argument. So you just need to cast it:
protected void Buttonlink_Click(object sender, System.EventArgs e)
{
LinkButton linkbtTitle = (LinkButton ) sender;
linkbtTitle.ForeColor = System.Drawing.Color.HotPink;
}