Reputation: 13
I am creating a social network site, I cant seem to get the "LinkEmail" in the code behind, I need this to function as I then use it to post to the database.
The LinkEmail
is being dynamically generated in the first repeater, I need a way to grab that value.
at the moment I am getting this error in the browser:
Compiler Error Message: CS0103: The name 'LinkEmail' does not exist in the current context
this is aspx code
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<div style="border-top: thin none #91ADDD; border-bottom: thin none #91ADDD; padding: 10px; width: 548px; margin-top: 10px; right: 10px; left: 10px; border-left-width: thin; margin-left: 15px; background-color: #F6F6F6; border-left-color: #91ADDD; border-right-color: #91ADDD;">
<br />
<div style="width: 58px; height: 40px">
<asp:Image ID="Image2" runat="server" Height="59px" ImageAlign="Top" ImageUrl="~/Profile/Image/Default.png" Width="55px" />
</div>
<div style="width: 307px; margin-left: 65px; margin-top: -60px">
<asp:Label ID="Label6" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="#3b5998"><%#Eval("YourName") %> </asp:Label>
</div>
<div id="status" style=" width: 461px; margin-left: 78px; margin-top: 11px;"> <asp:Label ID="Label7" runat="server" Font-Italic="False" ForeColor="Black" Font-Size="Medium"><%#Eval("Birthday") %> </asp:Label>
<asp:LinkButton ID="LinkEmail" runat="server" OnClick="lbl_Click"><%#Eval("Email") %></asp:LinkButton>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</div>
</ItemTemplate>
Could you tell me How to get LinkButton ID to my code behind file?
Upvotes: 0
Views: 6957
Reputation: 10285
foreach(RepeaterItem item in Repeater1.Items)
{
LinkButton LinkEmail=(LinkButton)item.FindControl("LinkEmail");
//Do here what ever you want
}
using Sender
You get the RepeaterItem
by casting the Link button's NamingContainer
. Then you can use FindControl
to get the reference to your Other Controls.
protected void lbl_Click(object sender,EventArgs e)
{
LinkButton LinkEmail=(LinkButton) sender;
var RepeaterRow=(RepeaterItem)LinkEmail.NamingContainer;
//find your other control like this
Label Label6=(Label) RepeaterRow.FindControl("controlname")
//Do here what ever you want
}
Or
You can use ItemCommand
as Suggested by Rahul singh
Upvotes: 0
Reputation: 21795
You can find it in ItemCommand
event like this:-
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
LinkButton LinkEmail= e.Item.FindControl("LinkEmail") as LinkButton ;
}
You need to associate this event handler with your control like this:-
<asp:Repeater runat="server" ID="Repeater1" OnItemCommand="Repeater1_ItemCommand">
Upvotes: 2