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. Fetching Value From a runtime textbox

Fetching Value From a runtime textbox

Scheduled Pinned Locked Moved ASP.NET
helptoolsdiscussion
3 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.
  • M Offline
    M Offline
    mani_iips
    wrote on last edited by
    #1

    I have created a textbox on button click event : protected void btnCreateTextBox_Click(object sender, EventArgs e) { TextBox txtDynamic = new TextBox(); txtDynamic.ID = "txtDynamic"; pnlContainer.Controls.Add(txtDynamic); } and when the page loads, i click on the button to load the textbox at runtime, it works fine. The problem in in fetching the value of the textbox when the user clicks on the "save" button. I am trying this, but it doesnt works. Since after the postback, there is NO existence of the runtime textbox. protected void btnSaveTextBox_Click(object sender, EventArgs e) { Response.Write(GetDynamicTextValue()); } private string GetDynamicTextValue() { foreach (Control ctrl in pnlContainer.Controls) { if (ctrl.GetType() == typeof(TextBox)) { return ((TextBox)ctrl).Text; } } return string.Empty; } Here is body of my aspx page I understand the problem is related to page life cycle, but am not being able to resolve it. Could somebody please explain me the same OR write the "GetDynamicTextValue()" method for me. Any help/referrence/discussion will be of real help. Am really stucked into an application that needs this utility. Thanks a lot for reading.

    M 1 Reply Last reply
    0
    • M mani_iips

      I have created a textbox on button click event : protected void btnCreateTextBox_Click(object sender, EventArgs e) { TextBox txtDynamic = new TextBox(); txtDynamic.ID = "txtDynamic"; pnlContainer.Controls.Add(txtDynamic); } and when the page loads, i click on the button to load the textbox at runtime, it works fine. The problem in in fetching the value of the textbox when the user clicks on the "save" button. I am trying this, but it doesnt works. Since after the postback, there is NO existence of the runtime textbox. protected void btnSaveTextBox_Click(object sender, EventArgs e) { Response.Write(GetDynamicTextValue()); } private string GetDynamicTextValue() { foreach (Control ctrl in pnlContainer.Controls) { if (ctrl.GetType() == typeof(TextBox)) { return ((TextBox)ctrl).Text; } } return string.Empty; } Here is body of my aspx page I understand the problem is related to page life cycle, but am not being able to resolve it. Could somebody please explain me the same OR write the "GetDynamicTextValue()" method for me. Any help/referrence/discussion will be of real help. Am really stucked into an application that needs this utility. Thanks a lot for reading.

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      Try the following code ~ ArrayList al = new ArrayList (); protected void Page_Load(object sender, EventArgs e) { if ( !Page.IsPostBack ) { Cache["myCtls"] = ""; } else { if ( Cache["myCtls"].ToString() != "" ) { try { al = (ArrayList)Cache["myCtls"]; foreach ( object o in al ) { if ( o.GetType () == typeof (TextBox) ) { TextBox txt = (TextBox)o; pnlContainer.Controls.Add (txt); } } } catch ( Exception ex ) { Console.WriteLine ("Why?? Why? babe!" + ex.Message); } } } } protected void btnCreateTextBox_Click(object sender, EventArgs e) { TextBox txtDynamic = new TextBox (); txtDynamic.ID = "txtDynamic" + al.Count ; pnlContainer.Controls.Add (txtDynamic); al.Add(txtDynamic); Cache["myCtls"] = al; } protected void btnSaveTextBox_Click(object sender, EventArgs e) { Response.Write (GetDynamicTextValue ()); } private string GetDynamicTextValue() { string str = string.Empty; foreach ( object o in al ) { if ( o.GetType () == typeof (TextBox) ) { str = str + ((TextBox)o).Text; } } return str; } I have tested in my side and it works well... Hope it helps.....

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

      M 1 Reply Last reply
      0
      • M Michael Sync

        Try the following code ~ ArrayList al = new ArrayList (); protected void Page_Load(object sender, EventArgs e) { if ( !Page.IsPostBack ) { Cache["myCtls"] = ""; } else { if ( Cache["myCtls"].ToString() != "" ) { try { al = (ArrayList)Cache["myCtls"]; foreach ( object o in al ) { if ( o.GetType () == typeof (TextBox) ) { TextBox txt = (TextBox)o; pnlContainer.Controls.Add (txt); } } } catch ( Exception ex ) { Console.WriteLine ("Why?? Why? babe!" + ex.Message); } } } } protected void btnCreateTextBox_Click(object sender, EventArgs e) { TextBox txtDynamic = new TextBox (); txtDynamic.ID = "txtDynamic" + al.Count ; pnlContainer.Controls.Add (txtDynamic); al.Add(txtDynamic); Cache["myCtls"] = al; } protected void btnSaveTextBox_Click(object sender, EventArgs e) { Response.Write (GetDynamicTextValue ()); } private string GetDynamicTextValue() { string str = string.Empty; foreach ( object o in al ) { if ( o.GetType () == typeof (TextBox) ) { str = str + ((TextBox)o).Text; } } return str; } I have tested in my side and it works well... Hope it helps.....

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

        M Offline
        M Offline
        mani_iips
        wrote on last edited by
        #3

        Thanks a lot for the reply. I understand what the code is doing is, its loading the textbox again in each page load so as to keep its existence thru the round trips. The solution is perfectly fine but i didnt wanted that to happen. I dont want to reload the textbox once the "save" button is clicked. To be pertinent, I want to know is there a way to "manuallly" catch the values stored in the PostBackData... i know abt _doPostback event, but then how to write a "generic" method which can keep track on the values of a desired control. [:|]

        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