评论DataList
(外部)和回复DataList
(内部)。
在内部DataList
内,有一个LinkButton
从TextBox
获取文字。
如何访问Add
的{{1}}事件?
.aspx代码
LinkButton
答案 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
}
}