2016-06-23 5 views
2

내 코드에서 확장 가능한 목록보기를 사용했습니다. 내 .aspx 파일에서 자리 표시자를 사용하고 .aspx.cs 파일의 코드를 사용하여 동적 텍스트 상자를로드했습니다. 나는 고유 한 ID를 텍스트 상자를 생성하지만 나는 생성 된 텍스트 상자에 입력 된 값을 얻는 방법을 모르겠다. 어떤 도움을 주시면 감사하겠습니다.자리 표시 자에서 동적으로 생성 된 텍스트 상자의 값을 가져옵니다.

SQ1.text을 사용하는 입력란에 사용자가 입력 한 값을 가져 오려고 시도했을 때 작동하지 않았습니다. 어떻게 동적으로 생성 된 텍스트 상자에 입력 된 값을 얻을 수 있습니까?

.aspx.cs이 파일

<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 파일

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); 
      } 
     } 
+1

FindControl ("IdofControl");을 사용하여 액세스하려고 했습니까? – Lucky

+0

@Lucky it worked –

답변

1

당신은 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) 
+0

완벽하게 작동했습니다. 귀하의 답변에 감사드립니다. –

+0

당신은 오신 것을 환영합니다. – Adil

0

하나를 사용하여 텍스트 상자를 얻을 수 plhMainItem에의 FindControl을 사용할 수 있습니다 방법은 우리를 지시하는 것입니다. 페이지에서 모든 게시 된 데이터를 가져 오는 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"]; 
관련 문제