Last post Sep 15, 2008 02:17 PM by JohnThompson
Nov 16, 2006 04:18 PM|theongrey|LINK
I am working on a custom CompositeControl that contains several drop-down lists and a refresh button. Everything was going great until I started trying to get the button to postback.
I have read other posts on this forum and other suggesting that I try something like this in my Render method:
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(m_btnRefresh, string.Empty));
where m_btnRefresh is the button that should postback. Well, this creates an OnClick call for the button, but not the __doPostBack client side function. Someone else suggested using Page.GetPostBackEventReference(m_btnRefresh), but that didn't work either.
As I understand it, the button should be able to post back without the _doPostBack client side function. . . but it isn't. What am I missing here?
Nov 16, 2006 06:18 PM|theongrey|LINK
Sorry to double-post, but here is an update on the situation:
[sarcasm]It seems that it helps if the control is placed in a form element in the aspx page[/sarcasm].
With that out of the way, the page DOES post back, but it DOES NOT raise ANY events, no matter what I try. I've tried the MSDN example, to no avail, and some simpler methods like the one explained in this post:
http://forums.asp.net/thread/490060.aspx. No matter what I try, I can't get any events to raise.
Really, all I want to do is handle the event of the Refresh button click inside the CompositeControl. The button's action can be automated inside the control, so I don't want the user to have to deal with it. I don't see any reason why the sample in the
above post isn't working (my code does exactly the same thing, only with more details like database reads, etc). . . unless I'm missing something stupid.
Thanks in advance,
Nov 18, 2006 01:52 PM|joteke|LINK
Can you post some code? Is the Button added to Controls collection at Load event time (at the latest) on postback in order for it to "catch" the events.
Sep 15, 2008 02:17 PM|JohnThompson|LINK
I think I have cracked it. ASP.Net Pages are a bit senial about remembering that they have a CompositeControl. This means that you must put code in the Pages Page_Load event not only to create the control originally, but also everytime it
is posted back. When you re-create the control this way ASP.Net is able to read the ViewState to get that controls values back, so you don't have to re-apply all the data and property settings. Below is a solution that works, but may be a little overwritten,
just to be sure that it works.
The idea is that there are two label and a button. When you press the button once one of the label text changes. The second press of the button and the other label text changes.
FILE 1: JohnsControl.cs in the App_Code folder.
FILE 2: JohnsControlTest.aspx.cs:-
// if (!IsPostBack)
FILE 3: JohnsControlTest.aspx for the Page:-
This requires the default JohnsControlTest.aspx file which is auto generated by Studio, with this panel control added into the <DIV> element: <asp:panel ID="pnlTestPanel" runat="server"></asp:panel>.