获取在占位符中动态生成的文本框的值

时间:2016-06-23 06:57:01

标签: c# asp.net asp.net-placeholder

我在代码中使用了可扩展列表视图。我在.aspx文件中使用了占位符,并使用.aspx.cs文件中的代码加载动态文本框。我已经为生成文本框提供了唯一的ID,但我不知道如何获取在生成的文本框中输入的值。任何帮助都将受到高度赞赏。

当我尝试使用SQ1.text让用户在文本框中输入值时,它无法正常工作。如何获取动态生成的文本框中输入的值?

.aspx文件

<div class="form-group inline clearfix"">
      <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
           <asp:PlaceHolder ID="plhMainItem" runat="server"></asp:PlaceHolder>
      </div>
</div>

.aspx.cs文件

private void LoadData(bool postback)
        {
            List<String> qualifications = new List<String>();

            qualifications.Add("Professional Qualifications");
            qualifications.Add("Special qualifications");
            qualifications.Add("Professional Experience");

            for (int i = 0; i < qualifications.Count; i++)
            {
                HtmlGenericControl newControl = new HtmlGenericControl("div");
                newControl.ID = "pnldefault" + i;
                newControl.Attributes["Class"] = "panel panel-default";

                HtmlGenericControl headraw = new HtmlGenericControl("div");
                headraw.ID = "headRawDiv" + i;
                headraw.Attributes["class"] = "panel-heading";
                headraw.Attributes["role"] = "tab";

                HtmlGenericControl bodyraw = new HtmlGenericControl("div");
                bodyraw.ID = "bodyRawDiv" + i;
                bodyraw.Attributes["class"] = "tbl-body row";

                HtmlGenericControl nameraw = new HtmlGenericControl("div");
                nameraw.ID = "nameRawDiv" + i;
                nameraw.Attributes["class"] = "col-md-7 col-sm-4 col-xs-5 tproname";

                HyperLink deslink = new HyperLink();
                deslink.Attributes["data-toggle"] = "collapse";
                deslink.Attributes["Class"] = "collapsed";
                deslink.Attributes["data-parent"] = "#accordion";
                deslink.Attributes["href"] = "#MainContent_detailsRawDiv" + i;
                deslink.Attributes["aria-expanded"] = "true";
                deslink.Attributes["aria-controls"] = "MainContent_detailsRawDiv" + i;
                deslink.Text = qualifications[i];

                nameraw.Controls.Add(deslink);
                bodyraw.Controls.Add(nameraw);
                headraw.Controls.Add(bodyraw);
                newControl.Controls.Add(headraw);

                HtmlGenericControl details = new HtmlGenericControl("div");
                details.ID = "detailsRawDiv" + i;
                details.Attributes["class"] = "panel-collapse collapse";
                details.Attributes["role"] = "tabpanel";
                details.Attributes["aria-labelledby"] = "MainContent_headRawDiv" + i;

                HtmlGenericControl pnlBody = new HtmlGenericControl("div");
                pnlBody.ID = "pnlBodyDiv" + i;
                pnlBody.Attributes["class"] = "panel-body";

                HtmlGenericControl tableBody = new HtmlGenericControl("div");
                tableBody.ID = "tableBodyDiv" + i;
                tableBody.Attributes["class"] = "tbl-body row";

                HtmlGenericControl tableRawBody = new HtmlGenericControl("div");
                tableRawBody.ID = "tableRawBodyDiv" + i;
                tableRawBody.Attributes["class"] = "tproname";

                HtmlGenericControl ul = new HtmlGenericControl("ul");
                ul.ID = "lstDetails" + i;

                if (qualifications[i] == "Professional Qualifications")
                {
                    for (int j = 1; j <= 3; j++)
                    {
                        TextBox tb = new TextBox();
                        tb.ID = "PQ" + j.ToString();
                        tb.Attributes.Add("placeholder", "Professional Qualifications " + j.ToString());
                        tb.Attributes["class"] = "form-control";
                        tb.Height = 100;
                        ul.Controls.Add(tb);
                    }
                }
                else if (qualifications[i] == "Special qualifications")
                {
                    for (int j = 1; j <= 3; j++)
                    {
                        TextBox tb = new TextBox();
                        tb.ID = "SQ" + j.ToString();
                        tb.Attributes.Add("placeholder", "Special qualifications " + j.ToString());
                        tb.Attributes["class"] = "form-control";
                        tb.Height = 100;
                        ul.Controls.Add(tb);
                    }
                }
                else if (qualifications[i] == "Professional Experience")
                {
                    for (int j = 1; j <= 3; j++)
                    {
                        TextBox tb = new TextBox();
                        tb.ID = "PE" + j.ToString();
                        tb.Attributes.Add("placeholder", "Name of the work place " + j.ToString());
                        tb.Attributes["class"] = "form-control";
                        ul.Controls.Add(tb);
                    }
                }

                tableRawBody.Controls.Add(ul);
                tableBody.Controls.Add(tableRawBody);
                pnlBody.Controls.Add(tableBody);
                details.Controls.Add(pnlBody);
                newControl.Controls.Add(details);

                plhMainItem.Controls.Add(newControl);
            }
        }

2 个答案:

答案 0 :(得分:1)

您可以在plhMainItem上使用FindControl来获取ID为

的TextBox
$auth _data

答案 1 :(得分:0)

一种方法是直接使用从页面获取所有发布数据的Request.Form。要使其工作,您还需要在控件上添加name

以下是如何做到这一点。

    TextBox tb = new TextBox();
            tb.ID = "PQ1" ;
            // need to add the name, so later to get the post back using that.
            tb.Attributes["name"] = "PQ1";

            tb.Attributes.Add("placeholder", "Professional Qualifications 1" );
            tb.Attributes["class"] = "form-control";                        
            tb.Height = 100;

    if (IsPostBack)
        txtDebug.Text = "Value is: " + Request.Form["PQ1"];