动态文本框FindControl显示为null ASP.net

时间:2017-05-19 01:40:37

标签: c# asp.net updatepanel

下面是aspx控件

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

对于动态文本框,当前是供用户输入的,当按钮保存点击时,文本框找不到控件总是显示为空。

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

我可以知道问题是什么

1 个答案:

答案 0 :(得分: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;



    }