DataList控件的模板列中有一个ID为“txtUserID”的TextBox和ID为“lblInfo”,Text为“已存在”的Label,在TextBox的TextChange事件中要获取它的Text进行判断,若此值存在,则Label可见,否则不可见。
在DataList控件的ItemCreated事件中书写如下代码:
1 protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
2 {
3 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
4 {
5 if (e.Item.FindControl("txtUserID")!= null)
6 {
7 TextBox tb = (TextBox)e.Item.FindControl("txtUserID");
8 tb.TextChanged += new EventHandler(tb_TextChanged);
9 }
10 }
11 }
12
2 {
3 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
4 {
5 if (e.Item.FindControl("txtUserID")!= null)
6 {
7 TextBox tb = (TextBox)e.Item.FindControl("txtUserID");
8 tb.TextChanged += new EventHandler(tb_TextChanged);
9 }
10 }
11 }
12
接着书写一个私有方法“tb_TextChanged( , )”,参数跟正常文本框TextChanged事件的参数一致
1 private void tb_TextChanged(object sender, EventArgs e)
2 {
3 TextBox tb = (TextBox)sender;
4 DataListItem dll = (DataListItem)tb.Parent;
5 Label lbl = (Label)dll.FindControl("lblInfo");
6 if (new UserBLL().checkUserID(tb.Text.Trim()))
7 {
8 lbl.Visible = true;
9 }
10 else
11 {
12 lbl.Visible=false;
13 }
14 }
2 {
3 TextBox tb = (TextBox)sender;
4 DataListItem dll = (DataListItem)tb.Parent;
5 Label lbl = (Label)dll.FindControl("lblInfo");
6 if (new UserBLL().checkUserID(tb.Text.Trim()))
7 {
8 lbl.Visible = true;
9 }
10 else
11 {
12 lbl.Visible=false;
13 }
14 }