Custom Control loads a user control; Postback events are not triggered

半腔热情 提交于 2019-12-11 07:46:37

问题


I have a custom control (compiled as a DLL) which loads a user control. (i.e, the custom control does a LoadControl) In the user control is a button and a textbox. I wire up the button's click event.

I type in a value into the text box. When I click the button, the page does a postback. My user control knows that a postback occured because Page.IsPostBack = true. However, the click event of the button is never fired and my text box has also lost the value that I typed in.

Anyone have any thoughts as to what might be going on?

EDIT: I did a test on this and took SharePoint out of the picture; I was able to reproduce it so I removed all references to SharePoint.


回答1:


If you are dynamically loading the user control, you have to reload it on each page load (postback or not) in order for the .net processor to know where to wire up the submit event.




回答2:


One way to load the User Control is to override CreateChildControl, call base.CreateChildControls and then call your LoadControl method. If you need to place the UserControl is a specific location, place a PlaceHolder on the page and add your control to the place holders control collection.

You can also just add the user control directly to the markup.

Register the control as such:

<%@ Register Src="~/path/ControlName.ascx" TagName="tagName" TagPrefix="myPrefix" %>

and then add it in as follows:

<myPrefix:tagName ID="myId" runat="server"/>



回答3:


If you are databinding, you need to check that you are only doing so on !Page.IsPostBack when you databind, you wipe out any "saved" state from the postback.




回答4:


Sounds like you're not recreating the control on postback. You will need to add the control during Page_Init for the view state to be loaded.



来源:https://stackoverflow.com/questions/682269/custom-control-loads-a-user-control-postback-events-are-not-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!