我是靠谱客的博主 乐观紫菜,最近开发中收集的这篇文章主要介绍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);
}
利用递归寻找控件,那样控件层次放的再深也能找到

最后

以上就是乐观紫菜为你收集整理的C# 遍历控件的全部内容,希望文章能够帮你解决C# 遍历控件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部