Fetching Value From a runtime textbox
-
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 pageI 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.
-
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 pageI 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.
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. :)
-
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. :)
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. [:|]