Adding Textbox dynamically when the value of Drop down list changes
-
Hi, I am working with asp.net page.I have a drop down list which displays values(like 1,2,3,4....).Now what i want is based on these values we must generate a textbox. E.g: Dropdown list value = 1 1 Textbox must be generated. value = 2 2 Textboxes must be generated...So on... I have added a place holder and tried with Events of Dropdown List... But i am unable to generate any text boxes...Please help Thanks in Advance Fast
-
Hi, I am working with asp.net page.I have a drop down list which displays values(like 1,2,3,4....).Now what i want is based on these values we must generate a textbox. E.g: Dropdown list value = 1 1 Textbox must be generated. value = 2 2 Textboxes must be generated...So on... I have added a place holder and tried with Events of Dropdown List... But i am unable to generate any text boxes...Please help Thanks in Advance Fast
First set the AutoPostBack property True for DropDownlist and handle the SelectedIndexChange Event.
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int txtCount = int.Parse(DropDownList1.SelectedValue)
for(int i=0;i<;txtcount;i++; )
{
TextBox txtBox = new TextBox()
//Assign id and some customization for each textbox
....
.....
placeHolder.Controls.Add(txtBox)
}
}Be careful, there is no Undo Button(Ctrl+Z) in life.
-
Hi, I am working with asp.net page.I have a drop down list which displays values(like 1,2,3,4....).Now what i want is based on these values we must generate a textbox. E.g: Dropdown list value = 1 1 Textbox must be generated. value = 2 2 Textboxes must be generated...So on... I have added a place holder and tried with Events of Dropdown List... But i am unable to generate any text boxes...Please help Thanks in Advance Fast
I suggest you to put the textbox in a template column inside a GridView, and bind the gridview with a datasource which will have necessary rows. If 1 is selected, make a 1 row data source and bind it to the grid. ASP.NET will maintain viewstate for the texboxes along with parent control, GridView.
Navaneeth How to use google | Ask smart questions
-
First set the AutoPostBack property True for DropDownlist and handle the SelectedIndexChange Event.
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int txtCount = int.Parse(DropDownList1.SelectedValue)
for(int i=0;i<;txtcount;i++; )
{
TextBox txtBox = new TextBox()
//Assign id and some customization for each textbox
....
.....
placeHolder.Controls.Add(txtBox)
}
}Be careful, there is no Undo Button(Ctrl+Z) in life.
That approach will generate the texboxes, but since it is created after viewstate events, ASP.NET will not maintain viewstate for it. Thus the values entered in the texboxes will not be persisted across postback. :)
Navaneeth How to use google | Ask smart questions
-
That approach will generate the texboxes, but since it is created after viewstate events, ASP.NET will not maintain viewstate for it. Thus the values entered in the texboxes will not be persisted across postback. :)
Navaneeth How to use google | Ask smart questions
Thanks for correcting my mistake.... I haven't try that way so I wasn't sure just give him a hint.
Be careful, there is no Undo Button(Ctrl+Z) in life.
-
Thanks for correcting my mistake.... I haven't try that way so I wasn't sure just give him a hint.
Be careful, there is no Undo Button(Ctrl+Z) in life.
You are welcome. Happy to help :)
Navaneeth How to use google | Ask smart questions
-
I suggest you to put the textbox in a template column inside a GridView, and bind the gridview with a datasource which will have necessary rows. If 1 is selected, make a 1 row data source and bind it to the grid. ASP.NET will maintain viewstate for the texboxes along with parent control, GridView.
Navaneeth How to use google | Ask smart questions
That is a good one...but i am also trying to store this data which is entered in textboxes into a database..How can i handle this... Eg: I am working on registration in Hotel: I can select values from drop-down list(From 1 to 8) and dynamically creating the text boxes. I need to enter user information of all the residents. So,If there are 2 text boxes and user enters information how can i bind and store into database...like the number of people keeps on changing right dynamically..... My database have name field,address field.. All the members required to enter the names but on same address... How to store these names into database as they are in random number. Thanks,
-
That is a good one...but i am also trying to store this data which is entered in textboxes into a database..How can i handle this... Eg: I am working on registration in Hotel: I can select values from drop-down list(From 1 to 8) and dynamically creating the text boxes. I need to enter user information of all the residents. So,If there are 2 text boxes and user enters information how can i bind and store into database...like the number of people keeps on changing right dynamically..... My database have name field,address field.. All the members required to enter the names but on same address... How to store these names into database as they are in random number. Thanks,
fasttoshiba wrote:
So,If there are 2 text boxes and user enters information how can i bind and store into database...like the number of people keeps on changing right dynamically.....
Loop through the gridview rows, find each texbox and save value to database.
Navaneeth How to use google | Ask smart questions
-
That approach will generate the texboxes, but since it is created after viewstate events, ASP.NET will not maintain viewstate for it. Thus the values entered in the texboxes will not be persisted across postback. :)
Navaneeth How to use google | Ask smart questions
-
Think textbox doesnt use viewstate for maintaining the text across postbacks Even if the viewstate is false,text will be still maintained during postbacks Correct me if i am wrong.
Sundar_R wrote:
Even if the viewstate is false,text will be still maintained during postbacks
You can read This [^] article.
cheers, Abhijit CodeProject MVP My Recent Article : Exploring Session in ASP.Net