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);
}
利用递归寻找控件,那样控件层次放的再深也能找到
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值