locked
Having problem Insert value use dynamic textbox RRS feed

  • Question

  • User1741664853 posted

    aspx

     <asp:UpdatePanel ID="pnlAnswer2" runat="server" EnableViewState="false">
                                            <ContentTemplate>
    
                                            </ContentTemplate>
                                            <Triggers>
                                                <asp:PostBackTrigger ControlID="btnNext" />
                                                <asp:PostBackTrigger ControlID="btnPrevious" />
                                            </Triggers>
      </asp:UpdatePanel>   

    c# 

    create textbox

    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.ID = "txtScore";
                // txt.Text = GetValue(questionSetID, 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);
                
    }

    c#

    when insert have problem 

    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);
                string contestantid = Session["CONTESTANTID"].ToString();
                string agentid = Session["varSession"].ToString();            
                foreach (DataRow row in dtQuestion.Rows)
                {
                    string questionno = mag.nullDB2String(row, "QUESTION_NO");
                    string questionid = mag.nullDB2String(row, "QUESTION_ID");
                    string questionsetid = mag.nullDB2String(row, "QUESTIONSET_ID");
                    string textbox1 = "txtScore" + mag.nullDB2String(row, "QUESTION_NO");
                    TextBox tbox = pnlAnswer2.ContentTemplateContainer.FindControl(textbox1) as TextBox;
                    if (checkscore(contestantid, agentid, questionno, questionid, questionsetid) != true)
                        {
    
                            string insertInputSQL = "INSERT INTO QUESTION_SCORE_JUDGE VALUES (NULL, '"
                                + questionsetid + "', '"
                                + questionid + "', '"
                                + agentid + "', '"
                                + Convert.ToDouble(tbox.Text) + "', '"
                                + Convert.ToDouble(GetValueQW(questionsetid, questionno)) + "', NULL,'"
                                + contestantid + "', NULL)";
    
                            objDBInterface.ExecSQL(insertInputSQL);
    		      }
    	       }
    	}

    the textbox not getting any value

    Thursday, May 18, 2017 6:17 AM

All replies

  • User-707554951 posted

    Hi jacksontan12345,

    Are you encounter any error in the following code?

    string textbox1 = "txtScore" + mag.nullDB2String(row, "QUESTION_NO");
    TextBox tbox = pnlAnswer2.ContentTemplateContainer.FindControl(textbox1) as TextBox;
    

    As far as I know, Dynamically created controls are lost on PostBack and hence we need to recreate the dynamic controls in the PreInit event of the page.

    Check the following link:

    https://www.aspsnippets.com/Articles/ASPNet-Dynamic-Controls-ViewState-Retain-state-for-dynamically-created-controls-on-PostBack.aspx

    https://www.google.com.sg/#q=maintain+viewstate+for+dynamic+controls+across+the+postback&spf=1495160094333

    Best regards

    Cathy

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Friday, May 19, 2017 2:16 AM
  • User1741664853 posted

    Thank you I have fix the problem. I put the function to recreate the textbox at page load is working

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

    Tuesday, May 30, 2017 12:42 PM