2

I have used an expandable list view in my code. I have used a placeholder in my .aspx file and using the code in .aspx.cs file load dynamic textboxes. I have give unique ID for generating textbox but i do not know how to get the value entered in generated textboxes. Any help would be highly appreciated.

When I tried to get the user entered values in textboxes using SQ1.text it didn't work. How can I get the values entered in dynamically generated textboxes?

.aspx file

<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 file

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 Answers2

1

You can use FindControl on plhMainItem to get the TextBoxes with ID

int index = 1;
TextBox tb = null;
do
{
   tb = plhMainItem.FindControl("SQ"+i++) as TextBox;
   if(tb != null)
   {
        // You can access the value of TextBox using Text property, tb.Text
   }
} while(tb != null)
Adil
  • 146,340
  • 25
  • 209
  • 204
0

One way is to direct use the Request.Form that get's all posted data from the page. To make it work you need also to add the name on the control.

Here is how you can do it.

    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"];
Aristos
  • 66,005
  • 16
  • 114
  • 150