日期:2014-05-18  浏览次数:20520 次

怎么激发嵌套在DataList里的控件的事件
我有一个DataList
里面嵌套了一个 TextBox1,和一个Button1
我要获取 Button1_Click事件
在这个事件里面要对TextBox1 是否有内容进行判定,有责进行下一步操作,没有责提醒

下面是我的代码
前台代码:
<asp:DataList ID="DataList1" runat="server" BackColor="White" GridLines="Both">
<ItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server" Width="600px" CommandName="TextBox1" ></asp:TextBox>
  <asp:Button ID="Button1" runat="server" CommandName="Button1" Text="提交"/>
</ItemTemplate>
</asp:DataList>

后台代码:
protected void DataList11_ItemCommand(object source DataListCommandEventArgs e)
{
  if(Button1==e.CommandName)
  {
  Label1.Text="点击Button1按钮"  
  TextBox txt=(TextBox)e.Item.FindControl("TextBox1")
  if(""==txt.Text)
  {
  Label2.Text=="TextBox1的内容为空"
  //Do
  }
  }
}

我这段代码好像无法实现,连Label1的Text都没变

希望大家能给我解答解答,不胜感激

------解决方案--------------------
我想我看懂了 你没有给数据绑定控件加ItemCommand事件
<asp:DataList ID="DataList1" runat="server" BackColor="White" GridLines="Both">
改成<asp:DataList ID="DataList1" runat="server" BackColor="White" GridLines="Both" OnItemCommand="DataList11_ItemCommand">