dynamically loaded control event only reached on 2nd postback

K

kw

My aspx dynamically loads an ascx into a placeholder. The ascx has an
event. When I click on the submit linkbutton in the ascx, the event does
not fire. But if I click it a second time, it fires.

I put in tracing, and the OnInit (which loads the event handler) is executed
on the initial load and also on the 2nd. It just makes no sense to me. Any
ideas?

Thanks a zillion!!

Dan
 
S

S. Justin Gengo

Dan,

The solution for this one isn't obvious.

Make certain that you're specifying your control's ID. That means do this
inside of the control's page load:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Me.ID = "MyControl1"

End Sub


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
K

kw

AMAZING!!! You were right! I'm still not quite sure why this should be
as it is obvious from my situation that something is very wrong about this.
See, the same ascx is being loaded by a treeview to display various objects
represented by different ID's.

On the face of it, it looks like polymorphism is out the window and now I
have to manually create copies of the ascx under different names so I can
assign them the appropriate ID in the instantiation....unless there is a way
to communicate that to the ascx via the viewstate, querystring or some other
means.

What do you suggest?

To make this concrete, here is where things are now:
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID="MyControl1";

But it looks like this is what it should be:
//ASPX
private void TreeViewControl_SelectedIndexChange(object sender,
System.EventArgs e){
TreeNode n = TreeViewControl.GetNodeFromIndex(e.NewNode);
ViewState["ContentId"]=n.ID; //or some other node dependent variation
Content.Controls.Add(LoadControl(n.ID+".ascx"));
....
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID=ViewState["ContentId"];

That looks about right, right? The idea being that ID has to be assigned in
the ASCX Page_Load and that ViewState is the best means of communication.
 
S

S. Justin Gengo

Dan,

That sounds right. Unfortunately, I can't confirm. You'll just have to try a
few combinations if the way you've described doesn't work. I'm not even
positive that the ID need be defined in the page load routine. I think you
could also define it in the Page Init.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
kw said:
AMAZING!!! You were right! I'm still not quite sure why this should be
as it is obvious from my situation that something is very wrong about this.
See, the same ascx is being loaded by a treeview to display various objects
represented by different ID's.

On the face of it, it looks like polymorphism is out the window and now I
have to manually create copies of the ascx under different names so I can
assign them the appropriate ID in the instantiation....unless there is a way
to communicate that to the ascx via the viewstate, querystring or some other
means.

What do you suggest?

To make this concrete, here is where things are now:
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID="MyControl1";

But it looks like this is what it should be:
//ASPX
private void TreeViewControl_SelectedIndexChange(object sender,
System.EventArgs e){
TreeNode n = TreeViewControl.GetNodeFromIndex(e.NewNode);
ViewState["ContentId"]=n.ID; //or some other node dependent variation
Content.Controls.Add(LoadControl(n.ID+".ascx"));
...
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID=ViewState["ContentId"];

That looks about right, right? The idea being that ID has to be assigned in
the ASCX Page_Load and that ViewState is the best means of communication.



S. Justin Gengo said:
Dan,

The solution for this one isn't obvious.

Make certain that you're specifying your control's ID. That means do this
inside of the control's page load:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Me.ID = "MyControl1"

End Sub


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
me.
Any
 
K

kw

Well, it didn't work.

ViewState["CtrlId"] is set prior to LoadControl but in the UserControl,
ViewState["CtrlId"] is null.

but what does (sort of) work is using a public property in the Page, setting
the ID into that property prior to LoadControl, and in the control
Page_Load:

MyPage j = (MyPage)this.Page;
this.ID=j.ContentID;

Which is a bullshit solution because the UserControl is now specific to this
one page...but it works.


S. Justin Gengo said:
Dan,

That sounds right. Unfortunately, I can't confirm. You'll just have to try a
few combinations if the way you've described doesn't work. I'm not even
positive that the ID need be defined in the page load routine. I think you
could also define it in the Page Init.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
kw said:
AMAZING!!! You were right! I'm still not quite sure why this should be
as it is obvious from my situation that something is very wrong about this.
See, the same ascx is being loaded by a treeview to display various objects
represented by different ID's.

On the face of it, it looks like polymorphism is out the window and now I
have to manually create copies of the ascx under different names so I can
assign them the appropriate ID in the instantiation....unless there is a way
to communicate that to the ascx via the viewstate, querystring or some other
means.

What do you suggest?

To make this concrete, here is where things are now:
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID="MyControl1";

But it looks like this is what it should be:
//ASPX
private void TreeViewControl_SelectedIndexChange(object sender,
System.EventArgs e){
TreeNode n = TreeViewControl.GetNodeFromIndex(e.NewNode);
ViewState["ContentId"]=n.ID; //or some other node dependent variation
Content.Controls.Add(LoadControl(n.ID+".ascx"));
...
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID=ViewState["ContentId"];

That looks about right, right? The idea being that ID has to be
assigned
in
the ASCX Page_Load and that ViewState is the best means of communication.
 
S

S. Justin Gengo

Dan,

Maybe you could set a property in the parent page's context and then pull
the id from the context object...

ViewState variables are pulled from the page as it's built. So that didn't
work because, until the page is posted the first time, viewstate hasn't been
set yet. But if you add a variable to the page's context object:

Context("CtrlId") = "Control1";

That should get set immediately and make your control usable across multiple
pages again... (As long as the page set's the control id in the context
object of course.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
kw said:
Well, it didn't work.

ViewState["CtrlId"] is set prior to LoadControl but in the UserControl,
ViewState["CtrlId"] is null.

but what does (sort of) work is using a public property in the Page, setting
the ID into that property prior to LoadControl, and in the control
Page_Load:

MyPage j = (MyPage)this.Page;
this.ID=j.ContentID;

Which is a bullshit solution because the UserControl is now specific to this
one page...but it works.


S. Justin Gengo said:
Dan,

That sounds right. Unfortunately, I can't confirm. You'll just have to
try
a
few combinations if the way you've described doesn't work. I'm not even
positive that the ID need be defined in the page load routine. I think you
could also define it in the Page Init.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
should
be
now
I
have to manually create copies of the ascx under different names so I can
assign them the appropriate ID in the instantiation....unless there is
a
way
to communicate that to the ascx via the viewstate, querystring or some other
means.

What do you suggest?

To make this concrete, here is where things are now:
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID="MyControl1";

But it looks like this is what it should be:
//ASPX
private void TreeViewControl_SelectedIndexChange(object sender,
System.EventArgs e){
TreeNode n = TreeViewControl.GetNodeFromIndex(e.NewNode);
ViewState["ContentId"]=n.ID; //or some other node dependent variation
Content.Controls.Add(LoadControl(n.ID+".ascx"));
...
//ASCX
private void Page_Load(object sender, System.EventArgs e){
this.ID=ViewState["ContentId"];

That looks about right, right? The idea being that ID has to be
assigned
in
the ASCX Page_Load and that ViewState is the best means of communication.



Dan,

The solution for this one isn't obvious.

Make certain that you're specifying your control's ID. That means do this
inside of the control's page load:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Me.ID = "MyControl1"

End Sub


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
My aspx dynamically loads an ascx into a placeholder. The ascx
has
an to
me.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top