如何在转发器控件内获取链接按钮生成的文本

时间:2014-12-26 19:49:20

标签: c# asp.net repeater

我正在创建一个社交网站,我似乎无法获得" LinkEmail"在后面的代码中,我需要这个功能,因为我然后使用它发布到数据库。

LinkEmail是在第一个转发器中动态生成的,我需要一种方法来获取该值。

目前我在浏览器中收到此错误:

编译器错误消息:CS0103: The name 'LinkEmail' does not exist in the current context

这是aspx代码

 <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>

实际上,当有人点击此链接时,我希望将链接按钮生成的文本作为此photo的会话。你能给我一个合适的代码吗?

根据这张照片amilamunasinha@gmail.com必须是LinkEmail.Text ,,,我想要这样

1 个答案:

答案 0 :(得分:1)

一旦您的Repeater控件在您的网页上,您需要做的第一件事就是注册ItemCommand事件。我更喜欢在页面的OnInit事件中执行此操作,而不是将其作为asp:Repeater标记的属性。

在.cs文件中:

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;
        }
}

在aspx中:

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