【问题标题】:Dynamic textbox FindControl is show null ASP.net动态文本框 FindControl 显示为空 ASP.net
【发布时间】:2017-05-19 01:40:37
【问题描述】:

下面是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);    
  }    

目前动态文本框是供用户输入的,当按钮保存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);
        }
    }

我能知道是什么问题吗

【问题讨论】:

  • 需要在每次页面加载时重新创建动态控件,其中包括回发。所以请确保CreateLabelQuestionNo 总是被解雇。在这里看到这个问题。 stackoverflow.com/questions/44040851/…

标签: c# asp.net updatepanel


【解决方案1】:

谢谢你,我已经解决了这个问题。我在页面加载时重新创建文本框的功能正在工作

    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;



    }

【讨论】:

    猜你喜欢
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2016-10-15
    • 2023-03-31
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多