2017-05-19 43 views
0

下面是ASPX控制动态文本框的FindControl是显示空ASP.net

<asp:UpdatePanel ID="pnlAnswer2" runat="server" EnableViewState="false"> 
      <ContentTemplate> 

      </ContentTemplate> 
      <Triggers> 
      <asp:PostBackTrigger ControlID="btnNext" /> 
      <asp:PostBackTrigger ControlID="btnPrevious" /> 
      </Triggers> 
    </asp:UpdatePanel>  

我使用动态表来显示动态文本框。除了所有的文本框,我已经为他们插入了动态ID。在这里工作

private void LoadQuestion2(string questionSetID, int page) 
{ 
     int recPerPage = 5; 
     int fromRec = (page - 1) * recPerPage; 
     DataTable dtQuestion; 

     string sql = "SELECT * FROM SETUP_QUESTION WHERE QUESTIONSET_ID = '" + questionSetID + "' ORDER BY CAST(QUESTION_NO AS UNSIGNED) ASC LIMIT " + fromRec + "," + recPerPage; 
     dtQuestion = objDBInterface.getResults(sql); 

     foreach (DataRow row in dtQuestion.Rows) 
     { 
      CreateLabelQuestionNo(mag.nullDB2String(row, "QUESTION_NO"), mag.nullDB2String(row, "QUESTION_ID"), mag.nullDB2String(row, "QUESTIONSET_ID")); 
     } 
    } 
    private void CreateLabelQuestionNo(string questionNo, string questionid, string questionSetID) 
{ 

     TextBox txt = new TextBox(); 
     txt.ID = "txtScore" + questionNo; 
     txt.Text = "1"; 
     txt.CssClass = "txt_standard"; 
     txt.TextMode = TextBoxMode.SingleLine; 
     txt.Style.Add("width", "50px"); 
     txt.Attributes.Add("runat", "server"); 

     Table tb = new Table(); 
     tb.ID = "tbscore"; 
     tb.Attributes.Add("runat", "server"); 

     tb.BorderWidth = Unit.Pixel(0); 
     for (int i = 1; i <= 1; i++) 
     { 
      TableRow tr = new TableRow(); 
      TableCell td3 = new TableCell(); 
      td3.Controls.Add(txt); 
      td3.Style.Add("padding-top", "15px"); 
      tr.Cells.Add(td3); 
      tb.Rows.Add(tr); 
     } 
     pnlAnswer2.ContentTemplateContainer.Controls.Add(tb);  
    }  

对于动态文本框当前是用户输入,当按钮保存的onclick文本框中无法找到控制始终显示为空。

private void SaveScore(string questionSetID, int page) 
    { 
     int recPerPage = 5; 
     int fromRec = (page - 1) * recPerPage; 
     DataTable dtQuestion; 
     string value = ""; 
     string sql = "SELECT * FROM SETUP_QUESTION WHERE QUESTIONSET_ID = '" + questionSetID + "' ORDER BY CAST(QUESTION_NO AS UNSIGNED) ASC LIMIT " + fromRec + "," + recPerPage; 
     dtQuestion = objDBInterface.getResults(sql);   
     foreach (DataRow row in dtQuestion.Rows) 
     { 
      string textbox1 = "txtScore" + mag.nullDB2String(row, "QUESTION_NO"); 
      TextBox tbox = pnlAnswer2.ContentTemplateContainer.FindControl(textbox1) as TextBox; 

      string insertInputSQL = "INSERT INTO QUESTION_SCORE_JUDGE VALUES (NULL, '" 
         + Convert.ToDouble(tbox.Text) + "', NULL)"; 

        objDBInterface.ExecSQL(insertInputSQL); 
     } 
    } 

我能知道是什么问题

+0

动态控件需要重新创建每个页面加载,并包括一个回发。所以确保始终启动'CreateLabelQuestionNo'。在这里看到这个问题。 http://stackoverflow.com/questions/44040851/my-textbox-added-programmatically-in-code-behind-after-page-reload-losess-values/44041285#44041285 – VDWWD

回答

0

谢谢我有解决这个问题。我把功能重新创建文本框在页面加载正在工作

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 

     } 
     else 
     { 

       for (int i = 1; i <= 5; i++) 
       { 
        if (pnlAnswer2.ContentTemplateContainer.FindControl("tbscore" + i) == null) 
        { 
         this.CreateTable(i); 
        } 
       } 
     } 
    } 
    private void CreateTable(int id) 
    { 
     TextBox txt = new TextBox(); 
     txt.ID = "txtScore" + id; 
     txt.CssClass = "txt_standard"; 
     txt.TextMode = TextBoxMode.SingleLine; 
     txt.Style.Add("width", "50px"); 
     txt.Attributes.Add("runat", "server"); 

     Table tb = new Table(); 
     tb.ID = "tbscore" + id; 
     tb.Attributes.Add("runat", "server"); 

     tb.BorderWidth = Unit.Pixel(0); 

     for (int i = 1; i <= 1; i++) 
     { 

      TableRow tr = new TableRow(); 

      TableCell td3 = new TableCell(); 

      td3.Controls.Add(txt); 
      td3.Style.Add("padding-top", "15px"); 

      tr.Cells.Add(td3); 

      tb.Rows.Add(tr); 
     } 

     pnlAnswer2.ContentTemplateContainer.Controls.Add(tb); 
     tb.Visible = false; 



    }