C# 遍历控件

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);
}
利用递归寻找控件,那样控件层次放的再深也能找到

posted on 2007-10-19 09:51  LongSky  阅读(385)  评论(0编辑  收藏  举报

导航