从代码隐藏访问datalist headertemplate中的控件
我的应用程序中有一个datalist,其headertemplate有一个标签。现在我需要从codebehind访问标签。我怎么能这样做..
代码 :
注意 :我需要从headertemplate访问lable lblcat ..
像这样用你的OnItemDataBound
附加OnItemDataBound
事件
...
并像这样定义它
protected void Dlitems_ItemDataBound(Object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { Label lblCat = (Label)e.Item.FindControl("lblcat"); lblCat.Text = "Changed!"; } }
上面的代码是正确的,但是你需要将代码添加回代码并正确定义它,因为用户没有点击任何按钮或链接,所以除非用户点击链接或按钮,否则我们不希望显示更改。 如下:
protected void dlData_ItemDataBound(object sender, DataListItemEventArgs e) { try { if (Page.IsPostBack) { if (e.Item.ItemType == ListItemType.Header) { Label lblCat = (Label)e.Item.FindControl("lblcat"); lblCat.Text = "Changed!"; } } } catch (Exception ex) { throw; } }
快乐的编程