0

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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <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;">&nbsp;<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>

                    &nbsp;
                </div>


 </ItemTemplate>

Could you tell me How to get LinkButton ID to my code behind file?

Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37

2 Answers2

2

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">
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • I want a onlick event, but i cannot get repeater1_itemcommand as onlick event – facebook-100002270512731 Dec 26 '14 at 18:22
  • @facebook-100002270512731 - You need to associate a `CommandName` to your link button and then write your logic in `ItemCommand` event, check this link: http://stackoverflow.com/questions/14861690/how-to-use-linkbutton-in-repeater-using-c-sharp-with-asp-net-4-5 – Rahul Singh Dec 26 '14 at 18:31
  • I [only want this](https://scontent-a-ams.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/10676371_1504823446473898_7054992759456936682_n.jpg?oh=14f071a725eb3e737b205afc713d110f&oe=54FDAAF4), but it is not happening – facebook-100002270512731 Dec 26 '14 at 19:08
0
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

Dgan
  • 10,077
  • 1
  • 29
  • 51
  • thanks, where should i paste this foreach code? should i use both codes? – facebook-100002270512731 Dec 26 '14 at 17:48
  • @facebook-100002270512731 you can use second 1 if you want to save your values to your databases `lbl_Click` – Dgan Dec 26 '14 at 17:52
  • could you tell me [what is this error](https://scontent-a-ams.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/10885072_1504804756475767_8990827318624925776_n.jpg?oh=4540b7a708df485bc2c2119d16eef4f1&oe=552CB1B8), i paste 2nd code on my cs file – facebook-100002270512731 Dec 26 '14 at 18:02
  • @facebook-100002270512731 ohh...remove just `as` keyword now updated answer – Dgan Dec 26 '14 at 18:05
  • friend actually i want , when someone click linkemail ,The text on linkemail should be a session, but your code some errors. Unable to cast object of type 'System.Web.UI.WebControls.RepeaterItem' to type 'System.Web.UI.WebControls.Repeater' – facebook-100002270512731 Dec 26 '14 at 18:19
  • I only [want this](https://scontent-a-ams.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/10676371_1504823446473898_7054992759456936682_n.jpg?oh=14f071a725eb3e737b205afc713d110f&oe=54FDAAF4), could you give me suitable code, I am so tired finding that – facebook-100002270512731 Dec 26 '14 at 19:13