Many users dynamically Add the user control in the button click event. After the page is returned, the user control disappears or the button event in the user control cannot be triggered. The following is a solution:
(1)
First, add a user control: webusercontrol1.ascx.
Add a button: public system. Web. UI. webcontrols. Button UCB; text is "user control button" (This button will be used in the page, so it is set to Public)
<Asp: button id = "UCB" text = "user control button" runat = "server"> </ASP: button>
Add code for the button's click event:
Response. Write (I. tostring ());
I ++;
Declare static int I = 0 in the class;
In this way, you can click the button once to see that the count is increased by 1.
(2)
Add a page
Place a placeholder with two buttons
<Asp: placeholder id = "placeholder1" runat = "server"> </ASP: placeholder>
<Asp: button id = "button1" runat = "server" text = "loading User Controls"> </ASP: button>
<Asp: button id = "button2" runat = "server" text = "getting user control values"> </ASP: button>
Add an operation to load a user control:
Private void adduc ()
{
Webusercontrol1 UC = (webusercontrol1) page. loadcontrol ("webusercontrol1.ascx ");
UC. ID = "UC ";
This. placeholder1.controls. Add (UC );
}
Add a click event for the first button:
Private void button#click (Object sender, system. eventargs E)
{
If (viewstate ["adduc"] = NULL)
{
Adduc ();
Viewstate ["adduc"] = 1;
}
}
We add a flag in the view status to indicate whether the user control has been loaded by clicking the button. Of course, if the user control has been loaded and then clicked, it does not need to be loaded again.
The key is to load the user control during page. ispostback.
(If this is not done, the user control disappears during the return process)
If (page. ispostback)
{
If (viewstate ["adduc"]! = NULL)
{
Adduc ();
}
}
Click button1 below to test whether the user control is loaded? You can also click the button in the user control to trigger the event.
Add a click event for button2 to read the title of the BTN button in the user control. The Dynamically Loaded user control can interact with the page as well as the user control loaded during design:
Private void button2_click (Object sender, system. eventargs E)
{
If (viewstate ["adduc"]! = NULL)
{
Response. Write (webusercontrol1) page. findcontrol ("UC"). UCB. Text );
}
}
It is noted that the ID is specified when the user control is loaded, which can be conveniently found through findcontrol.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service