从动态创建的控件获取值 [英] Getting values from dynamically created controls

查看:44
本文介绍了从动态创建的控件获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码,在这里我创建一个表控件,该控件具有多个控件,例如文本框等...如何检索用户输入的值.

Following is my code where I create a table control which has multiple control like textbox etc... how to retrieve values entered by the user.

protected void Page_Load(object sender, EventArgs e)
        {
            string DesignFilePath = Request.QueryString["ReportName"].ToString();
            ReportFields dsReportFieldList = new ReportFields();
            dsReportFieldList.ReadXml(DesignFilePath);

            int TotalRows = 0;
            int TotalColumn = 0;
            foreach(ReportFields.tbl_ReportFieldsRow  rf in dsReportFieldList.tbl_ReportFields)
            {
                TotalRows = Convert.ToInt32(rf.TotalRow) ;
                TotalColumn = Convert.ToInt32(rf.TotalColm);
                break;
            }

            Table ds = new Table();
            ds.ID="table1";
            ds.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
            ds.BorderStyle = BorderStyle.Solid;
            ds.BorderWidth = 1;
            ds.BorderColor = System.Drawing.ColorTranslator.FromHtml("#003366");
            ds.Width = 500;
            for (int idx = 0; idx < TotalRows; idx++)
            {
                TableRow tr = new TableRow();
                for (int idx1 = 0; idx1 < TotalColumn; idx1++)
                {
                    TableCell tc = new TableCell();
                    tc.Text = idx.ToString() + "," + idx1.ToString();
                    tr.Cells.Add(tc);
                }
                ds.Rows.Add(tr);
            }

            foreach (ReportFields.tbl_ReportFieldsRow rf in dsReportFieldList.tbl_ReportFields)
            {
                string[] Poss = rf.CellPosition.ToString().Split(',');
                ds.Rows[Convert.ToInt32(Poss[0])].Cells[Convert.ToInt32(Poss[1])].Controls.Add(CreateControlString(rf.sID,rf.sSize,"lable",rf.sCaption));
                ds.Rows[Convert.ToInt32(Poss[0])].Cells[Convert.ToInt32(Poss[1])].Controls.Add(CreateControlString(rf.sID, rf.sSize, rf.sControlType,""));
            }

            //FindControl("ContentPlaceHolder1").Controls.Add(ds);
            ContentPlaceHolder ch = new ContentPlaceHolder();
            ch = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
           
            ch.Controls.Add(ds);

            

            //ViewState.Add("stTable", ds);

        }

推荐答案

每个控件都有一个客户端ID,这意味着在客户端呈现时服务器控件的ID!
给您的控件一个唯一的ID:control.ID ="rpt";
ClientIDMode更改为static,使其ID与渲染页面之前的ID相同!
现在,您可以使用具有此值的客户端脚本访问服务器控件!
通过使用客户端脚本,您可以将值保存到cookie,隐藏字段中,然后从代码中访问该值!
each control have a client ID it means the ID of server control when rendered on client side!
give to your control a unique ID: control.ID = "rpt";
change ClientIDMode to static, it make ID as same as befor rendering page!
now you can access to your server control with client script with this value!
whit client scripting you can save value into cookies, hidden field, and then access from your code to this values!


使用Java脚本获取值并将其保存在隐藏字段中.最后,在后面的代码中检索了它们.
Use java script to get the values and keep them in a hidden fields. In the end retrieved them in code behind.


这篇关于从动态创建的控件获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆