简体   繁体   中英

How can access Button in Inner DataList?

Comments DataList (Outer) and Replies DataList (Inner).

While in Inner DataList there is a LinkButton that gets text from TextBox .

How I can access Add event of that LinkButton ?

.aspx code

<asp:DataList ID="dlComments" runat="server">
    <asp:DataList ID="dlReplies" runat="server">
        <asp:TextBox ID="txtReply" TextMode="MultiLine" Rows="3" Width="100%" runat="server"></asp:TextBox>
        <asp:LinkButton ID="lbEdit" CommandName="Add" runat="server">Edit</asp:LinkButton>
    </asp:DataList>
</asp:DataList>

在此处输入图片说明

You can add the OnItemCommand to the nested DataList.

<asp:DataList ID="dlReplies" runat="server" OnItemCommand="dlReplies_ItemCommand">

And then in code behind

protected void dlReplies_ItemCommand(object source, DataListCommandEventArgs e)
{
    //check for the correct commandname
    if (e.CommandName == "Add")
    {
        //use findcontrol to find the textbox and cast it back to one
        TextBox tb = e.Item.FindControl("txtReply") as TextBox;

        Label1.Text = tb.Text;
    }
}

Add commandName="click" on you linkbutton.

 private void DataList1_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) {
        if ((e.Item.ItemType == ListItemType.AlternatingItem) 
                    || (e.Item.ItemType == ListItemType.Item)) {
            DataList dtl2 = (DataList)(e.Item.FindControl("dlReplies"));
            dtl2.ItemCommand += new System.EventHandler(this.dtl2_ItemCommand);
        }
    }

    protected void dtl2_ItemCommand(object sender, System.Web.UI.WebControls.DataListCommandEventArgs e) {
        if (e.CommandName == "click") {
            // 'do what you want here
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM