在数据列表中查找控件 [英] Finding a control in datalist

查看:62
本文介绍了在数据列表中查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将控件(TextBoxes)放在了数据列表控件的标题模板中.谁能告诉我如何在运行时找到该控件(代码)?

(我实际上是想访问用户在文本框中写的值)

我什至知道如何在GridView中做到这一点

I have placed controls(TextBoxes) in the header template of the Datalist control. Can any body tell me how to find that control at run time(the code)?

(I actually want to access the values written in the text boxes by the user)

I even know how to do it in GridView

String empnam =((TextBox)  (GridView1.FooterRow.FindControl("TextBox1"))).Text;



我想要一个等效的对象.



I want an equivalent one to it.

推荐答案

您可以在datagrid的ItemDataBound 事件中访问它.要绑定的第一项是标题项.

You can access it at the ItemDataBound event of datagrid. First item to bound is header item.

if(if(e.Item.ItemType == ListItemType.Header))
{
    String empnam =((TextBox)(e.Item.Cells[0].Controls[1]).Text;
}


我宁愿建议在DataListItemDataBound 事件中使用FindControl()进行一些防御性编程:
I would rather suggest to use FindControl() with a bit of defensive programming, in the ItemDataBound event of DataList:

if (e.Item.ItemType == ListItemType.Header)
{
    TextBox txtEmp = e.Item.FindControl("TextBox1") as TextBox;
    if (txtEmp != null)
    {
        String empnam = txtEmp.Text;
    }
}


这篇关于在数据列表中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆