Loading user controls dynamically Part 109

  • دعنا نشارك هذا :
ترتيب الدرس : 109
المدة : 00:14:10
التحكم :
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/01/loading-user-controls-dynamically-part.html

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.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

Slides
http://csharp-video-tutorials.blogspot.com/2013/08/part-109-loading-user-controls.html

All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html

All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

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
this.Page.Controls.Add(LoadControl("~/UserControls/CalendarUserControl.ascx"));

For the HTML and code samples used in the demo please visit my blog at the following link
http://csharp-video-tutorials.blogspot.com/2013/01/loading-user-controls-dynamically-part.html

Webform1.aspx.cs Code
protected void Page_Load(object sender, EventArgs e)
{
CalendarUserControl calendarUserControl
(CalendarUserControl)LoadControl("~/UserControls/CalendarUserControl.ascx");
calendarUserControl.ID"CU1";
calendarUserControl.DateSelected +
new DateSelectedEventHandler(calendarUserControl_DateSelected);
calendarUserControl.CalendarVisibilityChanged +
new CalendarVisibilityChangedEventHandler(calendarUserControl_CalendarVisibilityChanged);
PlaceHolder1.Controls.Add(calendarUserControl);
}

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

protected void calendarUserControl_DateSelected(object sender, DateSelectedEventArgs e)
{
Response.Write(e.SelectedDate.ToShortDateString());
}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(((CalendarUserControl)PlaceHolder1.FindControl("CU1")).SelectedDate);
}

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

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

y
Sep 23, 2020 07:32:26
youtube