如何访问Inner DataList中的Button?

时间:2017-05-28 05:08:08

标签: c# asp.net button datalist asplinkbutton

评论DataList(外部)和回复DataList(内部)。

在内部DataList内,有一个LinkButtonTextBox获取文字。

如何访问Add的{​​{1}}事件?

.aspx代码

LinkButton

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以将OnItemCommand添加到嵌套的DataList。

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

然后在代码背后

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

答案 1 :(得分:-1)

在您的链接按钮上添加commandName="click"

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