您的位置:首页 > 编程语言 > C#

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);
}
利用递归寻找控件,那样控件层次放的再深也能找到
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: