C# 遍历控件
2007-10-19 08:06
197 查看
private DataSet GetData(ControlCollection oCtrls)
{
string[] strArray = new string[10];
string strTxtID = "";
Control oCtrl = oCtrls[0];
Control oCtrlTmp = null;
#region dtRtnResult
DataTable dtRtnResult = new DataTable("dtRtnResult");
dtRtnResult.Columns.Add("RtnType");
dtRtnResult.Columns.Add("RtnNo");
dtRtnResult.Columns.Add("RtnItem");
dtRtnResult.Columns.Add("RtnMat");
dtRtnResult.Columns.Add("RtnPrice");
dtRtnResult.Columns.Add("RtnQty");
dtRtnResult.Columns.Add("RtnAmount");
dtRtnResult.Columns.Add("RtnSumAmount");
for (int i = 0; i < 10; i++)
{
DataRow drTmp = dtRtnResult.NewRow();
//MoveType
strTxtID = "drpRtnType_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnType"] = ((DropDownList)oCtrlTmp).SelectedItem.Text;
//txtRtnNo_
strTxtID = "txtRtnNo_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnNo"] = ((TextBox)oCtrlTmp).Text;
//txtRtnItem_
strTxtID = "txtRtnItem_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnItem"] = ((TextBox)oCtrlTmp).Text;
//lblRtnMat_
strTxtID = "lblRtnMat_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnMat"] = ((Label)oCtrlTmp).Text;
//lblRtnPrice_
strTxtID = "lblRtnPrice_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnPrice"] = ((Label)oCtrlTmp).Text;
//txtRtnQty_
strTxtID = "txtRtnQty_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnQty"] = ((TextBox)oCtrlTmp).Text;
//lblRtnAmount_
strTxtID = "lblRtnAmount_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnAmount"] = ((Label)oCtrlTmp).Text;
//lblRtnSumAmount_
strTxtID = "lblRtnSumAmount_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnSumAmount"] = ((Label)oCtrlTmp).Text;
dtRtnResult.Rows.Add(drTmp);
}
protected void SetData(ControlCollection oCtrls)
{
string[] array = new string[4];
foreach (Control oCtrl in oCtrls)
{
int i=1;
while(i<=4)
{
string sID = "TextBox" + i.ToString();
Control oCtrl2 = oCtrl.FindControl(sID);
if ((oCtrl2 != null) && (oCtrl2 is TextBox))
{
((TextBox)oCtrl2).Text = array[i - 1].ToString();
}
else
{
SetData(oCtrl.Controls);
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SetData(this.Page.Controls);
}
利用递归寻找控件,那样控件层次放的再深也能找到
{
string[] strArray = new string[10];
string strTxtID = "";
Control oCtrl = oCtrls[0];
Control oCtrlTmp = null;
#region dtRtnResult
DataTable dtRtnResult = new DataTable("dtRtnResult");
dtRtnResult.Columns.Add("RtnType");
dtRtnResult.Columns.Add("RtnNo");
dtRtnResult.Columns.Add("RtnItem");
dtRtnResult.Columns.Add("RtnMat");
dtRtnResult.Columns.Add("RtnPrice");
dtRtnResult.Columns.Add("RtnQty");
dtRtnResult.Columns.Add("RtnAmount");
dtRtnResult.Columns.Add("RtnSumAmount");
for (int i = 0; i < 10; i++)
{
DataRow drTmp = dtRtnResult.NewRow();
//MoveType
strTxtID = "drpRtnType_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnType"] = ((DropDownList)oCtrlTmp).SelectedItem.Text;
//txtRtnNo_
strTxtID = "txtRtnNo_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnNo"] = ((TextBox)oCtrlTmp).Text;
//txtRtnItem_
strTxtID = "txtRtnItem_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnItem"] = ((TextBox)oCtrlTmp).Text;
//lblRtnMat_
strTxtID = "lblRtnMat_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnMat"] = ((Label)oCtrlTmp).Text;
//lblRtnPrice_
strTxtID = "lblRtnPrice_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnPrice"] = ((Label)oCtrlTmp).Text;
//txtRtnQty_
strTxtID = "txtRtnQty_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnQty"] = ((TextBox)oCtrlTmp).Text;
//lblRtnAmount_
strTxtID = "lblRtnAmount_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnAmount"] = ((Label)oCtrlTmp).Text;
//lblRtnSumAmount_
strTxtID = "lblRtnSumAmount_" + i.ToString();
oCtrlTmp = oCtrl.FindControl(strTxtID);
drTmp["RtnSumAmount"] = ((Label)oCtrlTmp).Text;
dtRtnResult.Rows.Add(drTmp);
}
protected void SetData(ControlCollection oCtrls)
{
string[] array = new string[4];
foreach (Control oCtrl in oCtrls)
{
int i=1;
while(i<=4)
{
string sID = "TextBox" + i.ToString();
Control oCtrl2 = oCtrl.FindControl(sID);
if ((oCtrl2 != null) && (oCtrl2 is TextBox))
{
((TextBox)oCtrl2).Text = array[i - 1].ToString();
}
else
{
SetData(oCtrl.Controls);
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SetData(this.Page.Controls);
}
利用递归寻找控件,那样控件层次放的再深也能找到
相关文章推荐
- ASP.NET(C#)中遍历所有控件
- C# 遍历控件或类的属性(设置取得)其值
- C# 控件遍历
- C#递归遍历窗体所有textbox控件并设置textbox事件的方法
- C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
- C#如何遍历动态生成的textbox,Combobox 等控件
- c# 遍历控件
- C#遍历DataSet控件实例总结
- C#遍历枚举,并绑定至类似DropDownList的控件上。
- C#、VB遍历控件并赋值
- C# 遍历控件 示例
- c# 遍历页面所有控件,设置panel隐藏
- [转载]C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
- 用C#如何遍历一个窗体中的某一种控件
- C# 遍历文件夹图片并用ListView控件展示
- C# 遍历窗体上控件方法
- asp.net(C#)遍历页面上所有的TextBox控件并给它赋值
- C#遍历窗体所有控件或某类型所有控件
- c#控件遍历
- C# 遍历窗体上控件方法