Search code examples
c#asp.netrepeater

How to get Link button generated text inside repeater control


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>

Actually i want to get Link button generated text as a session like this photo when someone click this link. could you give me a suitable codes for this.

According to this photo amilamunasinha@gmail.com must be LinkEmail.Text ,,, I want like this


Solution

  • The first thing you have to do, once your Repeater control is on your Web page, is register for the ItemCommand event. I prefer to do this within the OnInit event of the page instead of placing it as an attribute of the asp:Repeater tag.

    In .cs file:

    override protected void OnInit(EventArgs e)
    {
       base.OnInit(e);
       Repeater1.ItemCommand += new RepeaterCommandEventHandler(Repeater1_ItemCommand);
    }
    
    
    
    private void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
            switch (e.CommandName)
            {
                case "mail":
                    string emailId = e.CommandArgument.ToString();
                    break;
            }
    }
    

    In aspx:

    <asp:LinkButton ID="LinkEmail" runat="server" CommandName="mail" CommandArgument='<%# Eval("Email") %>'><%#Eval("Email") %></asp:LinkButton>