Loading controls dynamically in asp.net Part 110

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET Text Articles

All ASP .NET Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video we will discuss about
1. Adding controls dynamically
2. Tips and tricks to maintain the state of dynamically added controls across postback

To maintain the state of controls across postback
1. Add the controls in the page load event, and set their visibility to false.
2. Based on the DropDownList selection, show/hide the dynamically added controls

For the HTML and code samples used in the demo please visit my blog at the following link

Code Behind Code
protected void Page_Load(object sender, EventArgs e)
{
TextBox TB = new TextBox();
TB.ID = "TB1";
PlaceHolder1.Controls.Add(TB);
TB.Visible = false;

DropDownList DDL = new DropDownList();
DDL.ID = "DDL1";
DDL.Items.Add("New York");
DDL.Items.Add("New Jersey");
DDL.Items.Add("Washington DC");
PlaceHolder1.Controls.Add(DDL);
DDL.Visible = false;

if (DropDownList1.SelectedValue == "TB")
{
TB.Visible = true;
}
else if (DropDownList1.SelectedValue == "DDL")
{
DDL.Visible = true;
}
}

protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "TB")
{
Response.Write(((TextBox)PlaceHolder1.FindControl("TB1")).Text);
}
else if (DropDownList1.SelectedValue == "DDL")
{
Response.Write(((DropDownList)PlaceHolder1.FindControl("DDL1")).SelectedItem.Text);
}
}
Рекомендации по теме
Комментарии
Автор

Million likes. This is useful even after 10 years of its post and will be useful for years to come.

googlepandit
Автор

thank you. you are a lifesaver again. had to create 100 question survey page and this placeholder function saved me a lot of time

StreamBits
Автор

I always watching your videos thank you sire

hamidfilali
Автор

hello venkat, once more, thanks so much for your great tutorials. finally, i found an easier way to get started with asp.net.
i also have a question for this tutorial 110.
i can see that the control status can be kept during PostBack by moving the control load to Page_Load but then, what would be the benefit of loading the controls 'dynamically' ???
since the controls are existing anyway, we could as well place the controls in design time and during runtime switch the visibility upon user clicks. we would see the same behavior.

mergenlideki
Автор

Hi Venkat, your videos are great. ^^

I just wanted to ask: why don't you just put normal controls for the city dropdownlist and the postal code textbox without any placeholder and just set the visibility of the controls to true or false as desired? You would not having any problem keeping page information of controls on postback and don't have to create the control dynamically. Am I wrong?

jerds_
Автор

Hello Venket !

I always watching your videos, and it's really helped me lot.
Could you please post a video which can Insert/Update/Delete Data using Dynamic multiple TexBoxes/Buttons or any .net controls.
I'm tried lot to search in Google, no proper post available.

Thanks

seeworld
Автор

How about event handlers for the dynamically added controls?

vpfaiz
Автор

Hi, Can you please have a video of adding a validation to dyanamically generated controls? Thanks

asimpoudel
Автор

can we select different different sql table( like class-3 table no 3, class-4 table no 4) and enlist student name by using asp.net?

parthaceo
Автор

Nice video. I am just struggling with the dynamic control, your video helped but still I ma not able to resolve my problem, here is my query. I have to generate 15 rows, each row containing 7 controls (chkbx/date/textbox/combo) and events associated with each control. I have created a ASP:Table in aspx page and creating 15 rows dynamically through the code. but when i select number or rows from another
dropdown list to show only number or rows, i am unable to get the values of displayed rows.

SatishDsatkav
Автор

Venkat, I have been struggling with this for some weeks now. I am just unable to maintain the state of a drpdownlist on postback. I've tried using sessions, viewstate, application state.. all don't work.. please help. My web application web form makes use of a master page written in C#

TheINTERLECT
Автор

I want to create 5 Textbox each time I Click on Add button. But there is one problem. First time i click on button, it adds five textboxes, but when I press second time Button, it over writes the previous texboxes. Please help me....

MrComputex
Автор

can u help in finding web services video ??

dheerajkeerthi
Автор

Hello sir 
How can i make website like www.vistaprint.com
online text editing and other functionality
can u help me??

AnuragSharma-feil