Adding UserControl to Page
-
Hi, I have one
default.aspx
page and many.ascx user controls
that are displayed insidedefault.aspx
page. There is always only one.ascx user control
in thedefault page
(placeholder) at one time. This page is added dynamicly inPage_Load
of the default.aspx :protected void Page_Load(object sender, EventArgs e)
{
this.LoadContent();
}public void LoadContent()
{
this.placeholderContent.Controls.Clear();
this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
}during
Page_Load
of currently loaded user control, it might happen that depending on user action this control requires to be changed to different.ascx user control
, soLoadContent
(indefault.aspx
) is called to dump current control and load new one. Problem occurs that when anew control
is loaded after anold control
was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!zilo
-
Hi, I have one
default.aspx
page and many.ascx user controls
that are displayed insidedefault.aspx
page. There is always only one.ascx user control
in thedefault page
(placeholder) at one time. This page is added dynamicly inPage_Load
of the default.aspx :protected void Page_Load(object sender, EventArgs e)
{
this.LoadContent();
}public void LoadContent()
{
this.placeholderContent.Controls.Clear();
this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
}during
Page_Load
of currently loaded user control, it might happen that depending on user action this control requires to be changed to different.ascx user control
, soLoadContent
(indefault.aspx
) is called to dump current control and load new one. Problem occurs that when anew control
is loaded after anold control
was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!zilo
I thnk Page Load is too late to do this, you need to do it in the loadviewstate event for events to fire properly.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi, I have one
default.aspx
page and many.ascx user controls
that are displayed insidedefault.aspx
page. There is always only one.ascx user control
in thedefault page
(placeholder) at one time. This page is added dynamicly inPage_Load
of the default.aspx :protected void Page_Load(object sender, EventArgs e)
{
this.LoadContent();
}public void LoadContent()
{
this.placeholderContent.Controls.Clear();
this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
}during
Page_Load
of currently loaded user control, it might happen that depending on user action this control requires to be changed to different.ascx user control
, soLoadContent
(indefault.aspx
) is called to dump current control and load new one. Problem occurs that when anew control
is loaded after anold control
was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!zilo
You have to use the LoadControl method of the page class. This way, the "state" of the user control reflects the "state" of the page. For more information search with google for "dynamically add usercontrol asp.net"
-^-^-^-^-^-^-^-^-^-^-^- no code is free of bugs
-
I thnk Page Load is too late to do this, you need to do it in the loadviewstate event for events to fire properly.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
You have to use the LoadControl method of the page class. This way, the "state" of the user control reflects the "state" of the page. For more information search with google for "dynamically add usercontrol asp.net"
-^-^-^-^-^-^-^-^-^-^-^- no code is free of bugs
-
You have to use the LoadControl method of the page class. This way, the "state" of the user control reflects the "state" of the page. For more information search with google for "dynamically add usercontrol asp.net"
-^-^-^-^-^-^-^-^-^-^-^- no code is free of bugs
Hi, I've seen many tutorials and they all have one thing in common. They are very simple and do not cover this problematics deeply enough. I did not learn anything new. I would appreciate your help if you have any experience in dynamic loading and changing user controls. thanx
zilo
-
I thnk Page Load is too late to do this, you need to do it in the loadviewstate event for events to fire properly.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi, I have one
default.aspx
page and many.ascx user controls
that are displayed insidedefault.aspx
page. There is always only one.ascx user control
in thedefault page
(placeholder) at one time. This page is added dynamicly inPage_Load
of the default.aspx :protected void Page_Load(object sender, EventArgs e)
{
this.LoadContent();
}public void LoadContent()
{
this.placeholderContent.Controls.Clear();
this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
}during
Page_Load
of currently loaded user control, it might happen that depending on user action this control requires to be changed to different.ascx user control
, soLoadContent
(indefault.aspx
) is called to dump current control and load new one. Problem occurs that when anew control
is loaded after anold control
was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!zilo