Loading user controls dynamically Part 109

  • دعنا نشارك هذا :
ترتيب الدرس : 109
المدة : 00:14:10
التحكم :
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.


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 loading user controls dynamically. Normally, we drag and drop user controls on the webform, at design time. However, there could be several reasons in real time, for loading user controls dynamically. For example, depending on logged in user preferences like age, gender, location etc, we may want to load different user controls. Along the same line, based on Admin and Non-Admin users, different user controls should be loaded.

In most of the internet articles, I read that, if we want, the dynamically created controls, to maintain viewstate across postback, the controls should be loaded in Page_Init() event of the webform. However, I used the Page_Load() event, but the controls are still able to retain their state across postback.

Let us add the CalendarUserControl to the webform. Drag and drop, PlaceHolder control, where we want the controls to be on the page. If we don't use the PlaceHolderControl, and if we try to add controls using the following code, we may get a runtime HttpException.
Control 'CU1_txtDate' of type 'TextBox' must be placed inside a form tag with runatserver

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

Webform1.aspx.cs Code
protected void Page_Load(object sender, EventArgs e)
CalendarUserControl calendarUserControl
calendarUserControl.DateSelected +
new DateSelectedEventHandler(calendarUserControl_DateSelected);
calendarUserControl.CalendarVisibilityChanged +
new CalendarVisibilityChangedEventHandler(calendarUserControl_CalendarVisibilityChanged);

protected void calendarUserControl_CalendarVisibilityChanged(object sender, CalendarVisibilityChangedEventArgs e)
Response.Write("Calende Visible" + e.IsCalendarVisible.ToString());

protected void calendarUserControl_DateSelected(object sender, DateSelectedEventArgs e)

protected void Button1_Click(object sender, EventArgs e)

In the next video session, we will discuss about dynamically loading standrad asp.net user controls.

سيتم تفعيل التعليقات قريبا

Sep 23, 2020 07:32:26