Values from Textbox
-
Hi all, I am using this code to create textbox in asp.net pages and adding it to the panel control Page load Event
TextBox txtbox;
for (int i = 1; i < 10; i++)
{
txtbox = new TextBox();
txtbox.ID = "mytextbox" + i;
txtbox.AutoPostBack = false;
Panel1.Controls.Add(txtbox);
}ViewSource
<div id="Panel1"> <input name="mytextbox1" type="text" id="mytextbox1" /><input name="mytextbox2" type="text" id="mytextbox2" /><input name="mytextbox3" type="text" id="mytextbox3" /><input name="mytextbox4" type="text" id="mytextbox4" /><input name="mytextbox5" type="text" id="mytextbox5" /><input name="mytextbox6" type="text" id="mytextbox6" /><input name="mytextbox7" type="text" id="mytextbox7" /><input name="mytextbox8" type="text" id="mytextbox8" /><input name="mytextbox9" type="text" id="mytextbox9" /> </div>
and want to retrive the textbox values in a button click event. But it not iterarting throgh the textbox collectionprotected void Button2_Click(object sender, EventArgs e)
{IterateThroughChildren(this); } void IterateThroughChildren(Control parent) { foreach (Control c in parent.Controls) { if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && c.ID == null) { TextBox txtbox = (TextBox)c; } if (c.Controls.Count > 0) { IterateThroughChildren(c); } } }
Thanks in Advance
-
Hi all, I am using this code to create textbox in asp.net pages and adding it to the panel control Page load Event
TextBox txtbox;
for (int i = 1; i < 10; i++)
{
txtbox = new TextBox();
txtbox.ID = "mytextbox" + i;
txtbox.AutoPostBack = false;
Panel1.Controls.Add(txtbox);
}ViewSource
<div id="Panel1"> <input name="mytextbox1" type="text" id="mytextbox1" /><input name="mytextbox2" type="text" id="mytextbox2" /><input name="mytextbox3" type="text" id="mytextbox3" /><input name="mytextbox4" type="text" id="mytextbox4" /><input name="mytextbox5" type="text" id="mytextbox5" /><input name="mytextbox6" type="text" id="mytextbox6" /><input name="mytextbox7" type="text" id="mytextbox7" /><input name="mytextbox8" type="text" id="mytextbox8" /><input name="mytextbox9" type="text" id="mytextbox9" /> </div>
and want to retrive the textbox values in a button click event. But it not iterarting throgh the textbox collectionprotected void Button2_Click(object sender, EventArgs e)
{IterateThroughChildren(this); } void IterateThroughChildren(Control parent) { foreach (Control c in parent.Controls) { if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && c.ID == null) { TextBox txtbox = (TextBox)c; } if (c.Controls.Count > 0) { IterateThroughChildren(c); } } }
Thanks in Advance
I think you have a bit of a logic error, c.ID will never be null. You also have an unnecessary recursion with the if(c.Controls.Count > 0) All you need is
foreach(TextBox txt in Panel1.Controls) { }
only two letters away from being an asset
-
I think you have a bit of a logic error, c.ID will never be null. You also have an unnecessary recursion with the if(c.Controls.Count > 0) All you need is
foreach(TextBox txt in Panel1.Controls) { }
only two letters away from being an asset
I got the error as
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.
-
I got the error as
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.
TextBox txtbox = (TextBox)c; You are trying to cast every control in the collection to a TextBox, including the LIteralControl, of course you will get this error. A Literal is not a TextBox :rolleyes: foreach(TextBox txt in Panel1.Controls) This only gets you the TextBox controls in the collection
only two letters away from being an asset
-
TextBox txtbox = (TextBox)c; You are trying to cast every control in the collection to a TextBox, including the LIteralControl, of course you will get this error. A Literal is not a TextBox :rolleyes: foreach(TextBox txt in Panel1.Controls) This only gets you the TextBox controls in the collection
only two letters away from being an asset
It is throwing the error in the previous line itself :omg: