This is my first ever post in this forum, i have searched everywhere to find a solution, but none worked out for me, so now iam posting here to get your help.
I have to store categories along with their subcategories. the number of subcategories can vary depending on which category that's why iam using dynamic textboxes to get the subcategories. for example, Category A has 3 sub categories so i have a dropdownlist
which stores numbers from 1 to 10, and then the user selects 3 and three textboxes are shown. this works perfectly. but when i want to click the button to save these three textbox values, then it fails.
my code to generate the textboxes:
for (int i = 0; i <= numControls-1; i++)
{
lbl = new Label();
txt = new TextBox();
lbl.Text = "بخش فرعی " + (i).ToString() + " ";
Please find the below code for your requirement. You may have to remove the last comma from the label, which i have not given in the code. You can always perform what ever you want once you get your dynamic controls information using the below code
foreach (Control c in Panel1.Controls)
{
if (!string.IsNullOrEmpty(c.ID))
{
if (c.ID.ToLower().Contains("label"))
{
lblMessage.Text += ((Label)c).Text + ":";
}
if (c.ID.ToLower().Contains("text"))
{
if(!string.IsNullOrEmpty(((TextBox)c).Text))
{
lblMessage.Text += ((TextBox)c).Text + ",";
}
}
}
}
Please click "Mark as Answer" if you think this post answer your question
seema_naz
Member
6 Points
19 Posts
how to get dynamically generated textboxes values and store them in array?
Oct 21, 2012 09:54 AM|LINK
Hello All,
This is my first ever post in this forum, i have searched everywhere to find a solution, but none worked out for me, so now iam posting here to get your help.
I have to store categories along with their subcategories. the number of subcategories can vary depending on which category that's why iam using dynamic textboxes to get the subcategories. for example, Category A has 3 sub categories so i have a dropdownlist which stores numbers from 1 to 10, and then the user selects 3 and three textboxes are shown. this works perfectly. but when i want to click the button to save these three textbox values, then it fails.
my code to generate the textboxes:
for (int i = 0; i <= numControls-1; i++)
{
lbl = new Label();
txt = new TextBox();
lbl.Text = "بخش فرعی " + (i).ToString() + " ";
// txt.Text = "TextBox" + (i).ToString();
lbl.ID = "Label" + i.ToString();
txt.ID = "TextBox" + i.ToString();
pnlDynamic.Controls.Add(lbl);
pnlDynamic.Controls.Add(txt);
//_textBoxes.Add(txt);
pnlDynamic.Controls.Add(new LiteralControl("<br /><br />"));
}
and the code to get the values with the click event of button save:
protected void addCategoryBtn_Click(object sender, EventArgs e)
{
for (int i = 0; i <= numControls - 1; i++)
{
lblmsg.Text = txt.Text;
}
}
I also tried this code but still no result:
//if (IsPostBack)
//{
// foreach (TextBox t in ((List<TextBox>)Session["AddedControls"]))
// {
// //TextBox txtCtrl = (TextBox)pnlDynamic.FindControl("TextBox" + i.ToString());
// //if (txtCtrl != null)
// //{
// // lblmsg.Text = lblmsg.Text + ", " + txtCtrl.Text;
// //}
// lblmsg.Text = t.Text;
// //lblmsg.Text = txt.Text;
// }
//}
Can anyone please help me with this, it's been a week iam working and searching.. Thanks
kaushikmahet...
Contributor
3750 Points
887 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 21, 2012 11:07 AM|LINK
txt=new textbox(); this object to define globally
i hope solve this issue
Remember to click Mark as Answer on the post that helps to others.
seema_naz
Member
6 Points
19 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 22, 2012 04:02 AM|LINK
Hi, thanks for the reply. I have defined it globally like this:
public TextBox txt;
and then have initialized it like this inside the for loop:
txt = new TextBox();
sarathi125
Star
13599 Points
2691 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 22, 2012 06:07 AM|LINK
Hi,
Check this,
http://www.aspdotnet-suresh.com/2012/07/aspnet-save-dynamically-created-control.html
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
seema_naz
Member
6 Points
19 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 22, 2012 07:11 AM|LINK
Hi, thanks, I have been to this blog but hasn't helped me because i want to know where's the problem with this code i posted..
sarathi125
Star
13599 Points
2691 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 22, 2012 07:16 AM|LINK
Hi,
Here is the problem in your code, you assigned some ID to the textboxes, but you not using it to get the values, try like the following code
protected void addCategoryBtn_Click(object sender, EventArgs e) { for (int i = 0; i <= numControls - 1; i++) { lblmsg.Text = "TextBox" + i.ToString().Text; } }Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
seema_naz
Member
6 Points
19 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 22, 2012 08:30 AM|LINK
but
seema_naz
Member
6 Points
19 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 22, 2012 08:42 AM|LINK
I think there's something wrong with the viewstate which i cannot catch.. :/
sureshkumar....
Contributor
2143 Points
504 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 22, 2012 10:04 AM|LINK
Hi Seema_naz,
Please find the below code for your requirement. You may have to remove the last comma from the label, which i have not given in the code. You can always perform what ever you want once you get your dynamic controls information using the below code
foreach (Control c in Panel1.Controls) { if (!string.IsNullOrEmpty(c.ID)) { if (c.ID.ToLower().Contains("label")) { lblMessage.Text += ((Label)c).Text + ":"; } if (c.ID.ToLower().Contains("text")) { if(!string.IsNullOrEmpty(((TextBox)c).Text)) { lblMessage.Text += ((TextBox)c).Text + ","; } } } }Best Regards,
Suresh Kumar Gundala
seema_naz
Member
6 Points
19 Posts
Re: how to get dynamically generated textboxes values and store them in array?
Oct 26, 2012 05:35 PM|LINK
Thank you so much for your code, i will try it and let you know :))