469,267 Members | 1,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

dynamically loaded control event only reached on 2nd postback

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
Nov 18 '05 #1
5 3105
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
"kw" <el*****************@hotmail.com> wrote in message
news:eU*************@TK2MSFTNGP12.phx.gbl...
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

Nov 18 '05 #2
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. Justin Gengo" <sjgengo@aboutfortunate[no-spam].com> wrote in message
news:10*************@corp.supernews.com...
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
"kw" <el*****************@hotmail.com> wrote in message
news:eU*************@TK2MSFTNGP12.phx.gbl...
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


Nov 18 '05 #3
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" <el*****************@hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
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" <sjgengo@aboutfortunate[no-spam].com> wrote in message
news:10*************@corp.supernews.com...
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
"kw" <el*****************@hotmail.com> wrote in message
news:eU*************@TK2MSFTNGP12.phx.gbl...
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



Nov 18 '05 #4
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" <sjgengo@aboutfortunate[no-spam].com> wrote in message
news:10*************@corp.supernews.com...
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" <el*****************@hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
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" <sjgengo@aboutfortunate[no-spam].com> wrote in message
news:10*************@corp.supernews.com...
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
"kw" <el*****************@hotmail.com> wrote in message
news:eU*************@TK2MSFTNGP12.phx.gbl...
> 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
>
>



Nov 18 '05 #5
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" <el*****************@hotmail.com> wrote in message
news:OP**************@TK2MSFTNGP09.phx.gbl...
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" <sjgengo@aboutfortunate[no-spam].com> wrote in message
news:10*************@corp.supernews.com...
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" <el*****************@hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
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" <sjgengo@aboutfortunate[no-spam].com> wrote in
message news:10*************@corp.supernews.com...
> 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
> "kw" <el*****************@hotmail.com> wrote in message
> news:eU*************@TK2MSFTNGP12.phx.gbl...
> > 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
> >
> >
>
>



Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Donald Xie | last post: by
5 posts views Thread by karthick raja | last post: by
reply views Thread by Jesper Lund Stocholm | last post: by
1 post views Thread by Alexey Smirnov | last post: by
1 post views Thread by Sami Rehman | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.