UserControl not responding to button clicks and other events

M

Mike Griggs

Hi all

I've got a WebForm (WebForm1.aspx), to which I've added a PlaceHolder
(ph1). I'm programmatically adding a UserControl (MGUserControl.ascx)
to it, using LoadControl(). The control displays correctly, but if I
try and click on any of the buttons within the control, the events
never reach the event handlers in MGUserControl.ascx.

I've tried the following:

1. Setting an ID for the control (both declaratively and
programmatically, after the ph1.Controls.Add() call)
2. Setting a className in the HTML of MGUserControl.ascx
3. Adding a <% @reference %> link to WebForm1.aspx

and I think some other things, that I can't remember now. I'm
stumped. I know it's possible, but I'm just missing *something*.

I've RAR'd up the test project that I created, and put it at
http://server.griggs.org.uk/TestControls.rar. It's only 14k -
download that and have a peek, easier than me cutting and pasting all
of the code here.

I'd appreciate any help you can give :)

cheers
Mike
 
T

Teemu Keiski

Using LoadControl. Did you check that you are loading the control in
Page_Load at the latest and especially on postback (sorry I don't have the
time to check your complete code now, hopefully this gives hints)?

I also know about slight problem with PlaceHolders, try the same so that
you'd use some other control than PlaceHolder (for example Panel or just
server-side SPAN etc) to test if they have impact on it.
 
M

Mike Griggs

Teemu Keiski said:
Using LoadControl. Did you check that you are loading the control in
Page_Load at the latest and especially on postback (sorry I don't have the
time to check your complete code now, hopefully this gives hints)?

I also know about slight problem with PlaceHolders, try the same so that
you'd use some other control than PlaceHolder (for example Panel or just
server-side SPAN etc) to test if they have impact on it.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

Hi Teemu

The control is being loaded in response to a button click in the
containing WebForm:

(WebForm1.aspx)
private void Button1_Click(object sender, System.EventArgs e)
{
Control c = LoadControl("~/MGControl.ascx");
Panel1.Controls.Add(c);
c.ID = "MGControl";
}

and it's showing correctly on the page. The control fails to receive
the events generated by its own buttons. e.g., there is this event
handler in the .ascx:

(MGControl.ascx)

private void controlButton_Click(object sender, System.EventArgs e)
{
Label2.Text = DateTime.Now.ToShortTimeString();
}

I've just tried adding it to a Panel instead, but that made no
difference unfortunately :)

Is there something else I need to do in the Page_Load of the .ascx
file?

Thanks
Mike
 
R

Robert Koritnik

The problem that you're having is. At the point when you load your user
control it's ok. Ok. After all the processing is done, your user control's
state is saved in the viewstate and you get the page on the client side. So
far so good.

The problem is with your postback. The user control postbacks the form and
the events get executed. If you want to persist view state (like values in
your textboxes etc.) you should load your control in the OnInit phase,
because state gets loaded into controls right after that event and BEFORE
page load. if you want your events to get fired, the latest moment to load
your control is the PageLoad event, because after page load the postback
data gets processed and first all change events get fired after those the
clicks etc...

Well even if you really load your control on the postback, your're doing it
too late. Taht's why none of the events happens.

Server actually saves nothing when the page executes. Every request (may be
GET or POST) starts the whole process from the beginning. You may thought
that when you load your control that it stays inside. Well it doesn't
because of that. You have to take care that your dinamicly loaded controls
get loaded on the postback at the correct moment. And you must assure that
naming stays the same as it was at the moment it was dinamicly loaded. So
either set the ID's by yourself or make shure the control is loaded at the
same moment it was the first time, so automatic unique name creation would
give it same ID's. You can check that in the control tree in the trace
information (you have to turn it on - check the documentation)...
 
M

Mike Griggs

Robert Koritnik said:
The problem that you're having is. At the point when you load your user
control it's ok. Ok. After all the processing is done, your user control's
state is saved in the viewstate and you get the page on the client side. So
far so good.

The problem is with your postback. The user control postbacks the form and
the events get executed. If you want to persist view state (like values in
your textboxes etc.) you should load your control in the OnInit phase,
because state gets loaded into controls right after that event and BEFORE
page load. if you want your events to get fired, the latest moment to load
your control is the PageLoad event, because after page load the postback
data gets processed and first all change events get fired after those the
clicks etc...

Well even if you really load your control on the postback, your're doing it
too late. Taht's why none of the events happens.

Server actually saves nothing when the page executes. Every request (may be
GET or POST) starts the whole process from the beginning. You may thought
that when you load your control that it stays inside. Well it doesn't
because of that. You have to take care that your dinamicly loaded controls
get loaded on the postback at the correct moment. And you must assure that
naming stays the same as it was at the moment it was dinamicly loaded. So
either set the ID's by yourself or make shure the control is loaded at the
same moment it was the first time, so automatic unique name creation would
give it same ID's. You can check that in the control tree in the trace
information (you have to turn it on - check the documentation)...

Robert

Thanks for your response. I had play around and added this to
WebForm1.aspx:

private static bool b = false;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Control c = LoadControl("~/MGControl.ascx");
c.ID = "MGControl";
if (b)
Panel1.Controls.Add(c);
}

with the Button1_Click() function setting b = true. So, when the
button is clicked, the Page_Load() of WebForm1.aspx re-loads the
Control.

Which appears to work! A little strange, but such is life.

Thanks for your help everyone.

Mike
 
R

Robert Koritnik

When you'll get used to how Asp.Net actually works it will make perfect
sence to you too. It's not strange, you just have to know the concept.
Eventhough web development seems like Windows developement it's still whole
lot different. Probably we all had problems adopting the idea.

Right guys? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top