遍历获取ASP.NET页面控件的名称及值
2007-02-11 13:05
330 查看
逛csdn的时候碰见有人在求助这个问题,特分享一下经验。
如果直接用Page.Control 获取的到只是最顶层的页面元素,而真正的拖拉放上去的文本框或Label之类的控件,还隐藏在这些顶层页面元素的里面,所以需要再次遍历。
函数及使用方法如下,结果保存在这里选择了HashTable的方式。
protected void Page_Load(object sender, EventArgs e)
Hashtable getAllControlValue( object PageOrUserControl )
void getControlValue(Control ctrIn,Hashtable ht)
{
foreach (Control ctr in ctrIn.Controls)
{
Type controlType = ctr.GetType();
switch (controlType.ToString())
{
case "System.Web.UI.WebControls.TextBox":
TextBox controlTextBoxObj = (TextBox)ctr;
string controlTextBoxName = controlTextBoxObj.ID;
string controlTextBoxValue = controlTextBoxObj.Text;
ht.Add(controlTextBoxName, controlTextBoxValue);
break;
case "System.Web.UI.WebControls.Label":
Label controlLabelObj = (Label)ctr;
string controlLabelName = controlLabelObj.ID;
string controlLabelValue = controlLabelObj.Text;
ht.Add(controlLabelName, controlLabelValue);
break;
//case "其他类型":
// 其它类型 controlTextBoxObj = (其它类型)ctr;
// string controlTextBoxName = controlTextBoxObj.ID;
// string controlTextBoxValue = controlTextBoxObj.Text;
// ht.Add(controlTextBoxName, controlTextBoxValue);
// break;
default:
break;
}
}
}
如果直接用Page.Control 获取的到只是最顶层的页面元素,而真正的拖拉放上去的文本框或Label之类的控件,还隐藏在这些顶层页面元素的里面,所以需要再次遍历。
函数及使用方法如下,结果保存在这里选择了HashTable的方式。
protected void Page_Load(object sender, EventArgs e)
Hashtable getAllControlValue( object PageOrUserControl )
void getControlValue(Control ctrIn,Hashtable ht)
{
foreach (Control ctr in ctrIn.Controls)
{
Type controlType = ctr.GetType();
switch (controlType.ToString())
{
case "System.Web.UI.WebControls.TextBox":
TextBox controlTextBoxObj = (TextBox)ctr;
string controlTextBoxName = controlTextBoxObj.ID;
string controlTextBoxValue = controlTextBoxObj.Text;
ht.Add(controlTextBoxName, controlTextBoxValue);
break;
case "System.Web.UI.WebControls.Label":
Label controlLabelObj = (Label)ctr;
string controlLabelName = controlLabelObj.ID;
string controlLabelValue = controlLabelObj.Text;
ht.Add(controlLabelName, controlLabelValue);
break;
//case "其他类型":
// 其它类型 controlTextBoxObj = (其它类型)ctr;
// string controlTextBoxName = controlTextBoxObj.ID;
// string controlTextBoxValue = controlTextBoxObj.Text;
// ht.Add(controlTextBoxName, controlTextBoxValue);
// break;
default:
break;
}
}
}
相关文章推荐
- asp.net 中遍历页面里某种控件(Textbox)、获取当前页面的名称
- 遍历获取ASP.NET页面控件的名称及值 后台调用前台javascript方法报错:“缺少对象”的解决方法 xml与DataSet的互转换类
- 遍历获取ASP.NET页面控件的名称及值
- 如何在Asp.Net下遍历指定页面上的所有控件
- 遍历ASP.NET页面上的TextBox控件,并为其添加鼠标事件
- ASP.Net 如何让后台的cs页面获取到前台的html控件的值
- asp.net遍历页面所有的按钮(或控件)
- asp.net如何获取页面动态生成的服务器控件
- 遍历ASP.NET页面控件
- ASP.NET 页面控件遍历实现(转载+修改)
- ASP.NET页面中获取服务器控件的客户端ID
- ASP.NET 页面控件遍历实现(转载+修改)
- asp.net cs 后台如何获取前端页面控件的内容
- asp.net脚本获取不到id,服务器控件id生成html页面id控制
- 如何在ASP.NET下遍历指定页面上所有控件
- asp.net 遍历aspx页面中的TextBox控件,并设置属性为只读
- 如何在ASP.NET下遍历指定页面上所有控件
- 如何在ASP.NET下遍历指定页面上所有控件[原创]
- ASP.NET 跳转页面数据的获取 (使用html控件)