开发者

add user control dynamically

开发者 https://www.devze.com 2023-03-01 12:53 出处:网络
i have a page a three buttons, when开发者_如何学C user clicks 1st control i want to display 1st user control, for button2 second user control and so on... how can i achieve this in page_init as we don

i have a page a three buttons, when开发者_如何学C user clicks 1st control i want to display 1st user control, for button2 second user control and so on... how can i achieve this in page_init as we dont knw which button got clicked


you can find the button ID with this trick in Page_Init event

string buttonID = Request["__EVENTTARGET"].ToString();

In buttonID in buttonID variable you will find the client ID of the button..


It depends on how you want to display them.

It is easier to have the controls hidden when not needed and only visible when required.

If you want to add them dynamically the easiest way to add them is to add the control to a placeholder when you need them. eg

ASP

   <asp:PlaceHolder id="myPlaceHolder" runat="server"></asp:PlaceHolder>

Code behind (C#)

myControl = new UserControl();
myPlaceHolder.Controls.Add(myControl);

But you also need to remember that to be able to access the control on postback you need to re-add it on submit/postback.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号