如何在嵌套的Repeater中获取LinkBut​​ton文本

时间:2013-06-03 19:18:37

标签: c# asp.net .net

我在嵌套的Repeater中获取LinkBut​​ton文本时遇到问题

<div>
        <asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
                <ItemTemplate>
                    <div class="resourcesResult">
                        <asp:HiddenField ID="hf_resID" runat="server" Value='<%#  Eval("Id") %>' />
                        <a href='<%#  Eval("pageID") %>'><%#  Eval("name") %></a>
                        <br />
                        <asp:Literal ID="litSummary" runat="server" Text='<%#  Eval("summary") %>'></asp:Literal>
                        <br />

                        <asp:Repeater ID="rp_tagsTopics" runat="server">
                            <ItemTemplate>
                                <h6>
                                    <asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%#  Container.DataItem %>' OnClick="LinkButton1_Click" > <%#  Container.DataItem %></asp:LinkButton>
                                </h6>
                            </ItemTemplate>
                        </asp:Repeater>

                    </div>
                </ItemTemplate>
        </asp:Repeater>
</div>




protected void LinkButton1_Click(object sender, EventArgs e)
    {

        LinkButton lnkBtnTags = (LinkButton)rp_tagsTopics.FindControl("lnkBtnTags");
        Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
    }

2 个答案:

答案 0 :(得分:1)

或者您可以通过指定LinkBut​​ton的CommandName和CommandArgument参数来使用ItemCommand事件

<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%#  Container.DataItem %>' OnClick="LinkButton1_Click" CommandName="Redirect" CommandArgument='<%# Container.DataItem %>' > <%#  Container.DataItem %></asp:LinkButton>

在处理程序中使用参数:

protected void rp_tagsTopics_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    if( e.CommandName == "Redirect" )
    {
        Response.Redirect("~/WebsofWonder.aspx?tag=" + e.CommandArgument);
    }
}

答案 1 :(得分:0)

您应该使用sender处理程序中的LinkButton_Click参数来访问实际点击的LinkButton实例,并引发了Click事件:

protected void LinkButton1_Click(object sender, EventArgs e)
{

    // Use sender instead of trying to find the control within the Repeater
    LinkButton lnkBtnTags = (LinkButton) sender;
    Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}