How can save the value of dynamically created textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pooja8389
    New Member
    • Sep 2010
    • 22

    #1

    How can save the value of dynamically created textbox

    Good evening..
    how can i save the value of dynamically created textbox in data base
    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    public partial class frndslambook : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
         SqlCommand com1;
        SqlCommand com;
        SqlDataReader dr1;
        static string[] exp;
         static string exp1;
        static long c;
        TextBox[] textBoxArr = new TextBox[c + 1];  
        protected void Page_Load(object sender, EventArgs e)
        {
           com1 = new SqlCommand("Select * From slambook Where username=@username", con);
         com1.Parameters.Add("@username", SqlDbType.NVarChar).Value = Session["userfrndname"].ToString();
            try
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                dr1 = com1.ExecuteReader();
                if (dr1.Read())
                {
    
                    
                    string exp1 = dr1["slambookfields"].ToString();
                   long c = Convert.ToInt64(exp1.Length);
                    Label[] l1 = new Label[c + 1];//array of lables
                  TextBox[] textBoxArr = new TextBox[c + 1];//array of textboxes
                    
                    exp = (exp1).Trim('^').Split('^');
                    for (int i = 0; i < exp.Length; i++)
                    {
                        Panel frndstextBoxLabelGroup = new Panel();
                        l1[i] = new Label();
                        l1[i].ID = "frndslambooklabel" + i.ToString();
                        l1[i].Text = exp[i].ToString();
                        l1[i].Visible = true;
    
    
                        textBoxArr[i] = new TextBox();
                        textBoxArr[i].ID = "frndslambooktextbox" + i.ToString();
                        textBoxArr[i].Visible = true;
                        //Initializing the TextBox so that it is not rendered in the browser
                        frndstextBoxLabelGroup.ID = "frndstextBoxLabelGroup" + i.ToString();
                        fspfrndslambook.Visible = true;
                        // Pnl_TextBox.Controls.Add(br);
                        frndstextBoxLabelGroup.Controls.Add(l1[i]);
    
                        frndstextBoxLabelGroup.Controls.Add(textBoxArr[i]);
                        fspfrndslambook.Controls.Add(frndstextBoxLabelGroup);
                        
    
                    }
                }
    
    
            }
            catch (Exception exc)
            {
            }
            finally
            {
                con.Close();
            }
    
        }
        protected void fsbfill_Click(object sender, EventArgs e)
        {
            com = new SqlCommand("Insert Into slambookans (slambookfields_ans,sender,receiver) Values(@slambookfields_ans,@sender,@receiver)", con);
           for (int i = 0; i < exp.Length; i++)
            //{
                //textBoxArr[i] = new TextBox();
             com.Parameters.Add("@slambookfields_ans", SqlDbType.NVarChar).Value =textBoxArr[i].Text;
              
            }
            com.Parameters.Add("@sender", SqlDbType.NVarChar).Value = Session["username"].ToString();
            com.Parameters.Add("@receiver", SqlDbType.NVarChar).Value = Session["userfrndname"].ToString();
                 try
                {
                    if (con.State == ConnectionState.Closed)
                        con.Open();
                    int i=com.ExecuteNonQuery();
                   if(i!=0)
                   Response.Write("Success");
                   else
                    Response.Write("Fail");
                 }
            catch(Exception exc)
            {
            }
            finally
            {
                con.Close();
            }
        }
    }
    in the above code the textboxArr is dynamically created..and i want to save the value of that button at fsbfill_click.h ow can i do this?i want to save the value in one tuple of database.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I'm not exactly sure where to start...

    Actually, maybe you should start by reading the How to use dynamic controls in ASP.NET article.

    You are experiencing problems in your application because you are not considering how the ASP.NET-life-cycle effects how controls and events are treated.

    -Frinny

    Comment

    • danp129
      Recognized Expert Contributor
      • Jul 2006
      • 323

      #3
      Only creating the controls when page.isPostBack is false would be a good start.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Actually you Must create the controls every time the page is loaded or else you will not be able to use them.

        You should be doing this in the Page Init event though...so that events and properties (etc) can be loaded for the controls when the ViewState is loaded. Doing this in your Page Load event is not going to help you because that event occurs after the ViewState is loaded (all of the controls are created).



        -Frinny

        Comment

        • danp129
          Recognized Expert Contributor
          • Jul 2006
          • 323

          #5
          Oh yeah, it's been a while since I've last done that :P

          Comment

          Working...