Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to get the value of a dynamically created textBox in asp.net C#?

How to get the value of a dynamically created textBox in asp.net C#?

Scheduled Pinned Locked Moved ASP.NET
csharpquestionasp-nettutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    susanna floora
    wrote on last edited by
    #1

    I have Created textbox, dropDownList Dynamically at run time.Now how do i get the textbox value and dropDown value in ButtonClick.

    C 1 Reply Last reply
    0
    • S susanna floora

      I have Created textbox, dropDownList Dynamically at run time.Now how do i get the textbox value and dropDown value in ButtonClick.

      C Offline
      C Offline
      chester_it21
      wrote on last edited by
      #2

      Can you give your sample code, for us its logic flow analysis, or you did not make his code..??

      S 1 Reply Last reply
      0
      • C chester_it21

        Can you give your sample code, for us its logic flow analysis, or you did not make his code..??

        S Offline
        S Offline
        susanna floora
        wrote on last edited by
        #3

        TextBox txt = new TextBox(); txt.ID = ID; txt.Text = DateTime.Now.ToString(); PlcHolder.Controls.Add(txt); DropDownList ddl = new DropDownList(); ddl.ID = ID; string strSql = "SELECT Code,[Name] FROM SDTS_GSTYPE"; DataTable dtBranch = getDataTable(strSql); ddl.DataSource = dtBranch; ddl.AutoPostBack = true; ddl.DataTextField = "Name"; ddl.DataValueField = "Code"; ddl.SelectedIndex = 0; ddl.DataBind(); PlcHolder.Controls.Add(ddl); This is the code i have written to create a textbox.Now how do i get the textbox value and selected value of dropDownlist in my buttonClick event.

        C 1 Reply Last reply
        0
        • S susanna floora

          TextBox txt = new TextBox(); txt.ID = ID; txt.Text = DateTime.Now.ToString(); PlcHolder.Controls.Add(txt); DropDownList ddl = new DropDownList(); ddl.ID = ID; string strSql = "SELECT Code,[Name] FROM SDTS_GSTYPE"; DataTable dtBranch = getDataTable(strSql); ddl.DataSource = dtBranch; ddl.AutoPostBack = true; ddl.DataTextField = "Name"; ddl.DataValueField = "Code"; ddl.SelectedIndex = 0; ddl.DataBind(); PlcHolder.Controls.Add(ddl); This is the code i have written to create a textbox.Now how do i get the textbox value and selected value of dropDownlist in my buttonClick event.

          C Offline
          C Offline
          chester_it21
          wrote on last edited by
          #4

          sorry, long time to replay...... first, The first, you should know basic first Page Postback ... every time you create dynamic controls in codebehind, even when you do click on a control button, the page is always postback, and automatically you will never get the value out of control, even control was not there at all when you try to use the event FinControl ... so, you have to declare the event re-create control when page IsPostBack. This sample code to start what you want .. this is just a beginning to open its forward your logic .. you can add it from the code sample I gave .....

          protected void Page_Load(object sender, EventArgs e)
          {
          if(!Page.IsPostBack) //this first time page Load/isPostback when call this page
          {
          GenerateControlSelf();//for generate custom control in holder(as PlaceHolder)
          }
          else //second Page.isPostback
          {
          // you must declare again your custom control in second Page.isPostback
          var txt = new TextBox { ID = "tbx", Text = DateTime.Now.ToString(CultureInfo.InvariantCulture), ClientIDMode = ClientIDMode.Static };
          holder.Controls.Add(txt);
          var ddl = new DropDownList { ID = "ddlx" };

                      var nextYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) + 1;
                      var lastYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) - 10;
                      for (var n = lastYear; n < nextYear; n++)
                      {
                          ddl.Items.Add(n.ToString(CultureInfo.InvariantCulture));
                      }
          
          
                      ddl.AutoPostBack = true;
                      ddl.SelectedIndex = 0;
                      ddl.DataBind();
                      holder.Controls.Add(ddl);
                  }
          
              }
              private void GenerateControlSelf()
              {
                  
                  var txt = new TextBox { ID = "tbx", Text = DateTime.Now.ToString(CultureInfo.InvariantCulture) ,ClientIDMode = ClientIDMode.Static};
                  holder.Controls.Add(txt);            
                  var ddl = new DropDownList {ID = "ddlx"};
          
                  var nextYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) + 1;
                  var lastYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) - 10;
                  for (var n = lastYear; n < nextYear; n++)
                  {
                      ddl.Items.Add(n.ToString(CultureInfo.Inva
          
          S 1 Reply Last reply
          0
          • C chester_it21

            sorry, long time to replay...... first, The first, you should know basic first Page Postback ... every time you create dynamic controls in codebehind, even when you do click on a control button, the page is always postback, and automatically you will never get the value out of control, even control was not there at all when you try to use the event FinControl ... so, you have to declare the event re-create control when page IsPostBack. This sample code to start what you want .. this is just a beginning to open its forward your logic .. you can add it from the code sample I gave .....

            protected void Page_Load(object sender, EventArgs e)
            {
            if(!Page.IsPostBack) //this first time page Load/isPostback when call this page
            {
            GenerateControlSelf();//for generate custom control in holder(as PlaceHolder)
            }
            else //second Page.isPostback
            {
            // you must declare again your custom control in second Page.isPostback
            var txt = new TextBox { ID = "tbx", Text = DateTime.Now.ToString(CultureInfo.InvariantCulture), ClientIDMode = ClientIDMode.Static };
            holder.Controls.Add(txt);
            var ddl = new DropDownList { ID = "ddlx" };

                        var nextYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) + 1;
                        var lastYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) - 10;
                        for (var n = lastYear; n < nextYear; n++)
                        {
                            ddl.Items.Add(n.ToString(CultureInfo.InvariantCulture));
                        }
            
            
                        ddl.AutoPostBack = true;
                        ddl.SelectedIndex = 0;
                        ddl.DataBind();
                        holder.Controls.Add(ddl);
                    }
            
                }
                private void GenerateControlSelf()
                {
                    
                    var txt = new TextBox { ID = "tbx", Text = DateTime.Now.ToString(CultureInfo.InvariantCulture) ,ClientIDMode = ClientIDMode.Static};
                    holder.Controls.Add(txt);            
                    var ddl = new DropDownList {ID = "ddlx"};
            
                    var nextYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) + 1;
                    var lastYear = Convert.ToInt32(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) - 10;
                    for (var n = lastYear; n < nextYear; n++)
                    {
                        ddl.Items.Add(n.ToString(CultureInfo.Inva
            
            S Offline
            S Offline
            susanna floora
            wrote on last edited by
            #5

            Thank you very much. Its working.

            C 1 Reply Last reply
            0
            • S susanna floora

              Thank you very much. Its working.

              C Offline
              C Offline
              chester_it21
              wrote on last edited by
              #6

              here i am wellcaom..be happy, do not ever hesitate to ask questions and share your knowledge if you already master ..

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups