event handling in a user control

G

Guest

Hi,

I have created a web page which includes a place holder. I also have a
dropdown list in that webpage. when I select one of the choices in that
dropdown list, It will load a user control into the place holder. This is
done dynamically based on the choice they selected. This user control has a
datagrid in it that supports paging. When I click on the next or prev buttons
of the datagrid in a user control it should display the next page results.
but it is not doing so. I am not knowing how to call the PageIndexChanged
event of the user control from the webpage. could anyone show me a code
sample to do that? I am using VB.NET for this.

Thanks,
Sridhar.
 
G

Guest

Dynamically loading controls must be done during the Page.Init event handling
(page initialization stage) otherwise they would lose they ViewState and
their events would not fire. So if you added a control based on a
SelectedIndexChanged, it is added late in the page lifecycle and you have to
persist a condition that allows you to reload it during the page.init event
handling, otherwise no response would happen to any of its events.

For a simple demo on this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

For a clear explanation of the page life cycle:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp
 
K

Karl Seguin

Dynamically added controls need to be readded on postback. If you don't
add it back, how can an event ever be fired for it, it doesn't exist!.

You can use Denis Bauer's DynamicControlsPlaceholder (free):
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
which automatically re-adds dynamic controls on postback, or you can do it
yourself. Here's a dummy example:

sub Page_Load()
if Page.IsPostBack AndAlso not ViewState("controlToReload) is nothing then
plc.Controls.Add(cstr(ViewState(controlPath)))
end if
end sub

sub selectedindexchange(..)
string controlPath = "mycontrol.ascx"
plc.Controls.Add(Page.LoadControl(controlPath))
ViewState.Add("controlToReload", controlPath)
end sub
 
K

Karl Seguin

Philip:
This question gets asked all the time, imma keep ur links and pass them off
as my own helpfulness next time! HAR!

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Phillip Williams said:
Dynamically loading controls must be done during the Page.Init event
handling
(page initialization stage) otherwise they would lose they ViewState and
their events would not fire. So if you added a control based on a
SelectedIndexChanged, it is added late in the page lifecycle and you have
to
persist a condition that allows you to reload it during the page.init
event
handling, otherwise no response would happen to any of its events.

For a simple demo on this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

For a clear explanation of the page life cycle:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Sridhar said:
Hi,

I have created a web page which includes a place holder. I also have a
dropdown list in that webpage. when I select one of the choices in that
dropdown list, It will load a user control into the place holder. This
is
done dynamically based on the choice they selected. This user control has
a
datagrid in it that supports paging. When I click on the next or prev
buttons
of the datagrid in a user control it should display the next page
results.
but it is not doing so. I am not knowing how to call the PageIndexChanged
event of the user control from the webpage. could anyone show me a code
sample to do that? I am using VB.NET for this.

Thanks,
Sridhar.
 
G

Guest

Hi
Thanks for the reply. But the problem is I am getting the control name
dynamically and loading it dynamically. In that case how would I know which
events shoud I add?

Thanks,
Sridhar.

Phillip Williams said:
Dynamically loading controls must be done during the Page.Init event handling
(page initialization stage) otherwise they would lose they ViewState and
their events would not fire. So if you added a control based on a
SelectedIndexChanged, it is added late in the page lifecycle and you have to
persist a condition that allows you to reload it during the page.init event
handling, otherwise no response would happen to any of its events.

For a simple demo on this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

For a clear explanation of the page life cycle:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Sridhar said:
Hi,

I have created a web page which includes a place holder. I also have a
dropdown list in that webpage. when I select one of the choices in that
dropdown list, It will load a user control into the place holder. This is
done dynamically based on the choice they selected. This user control has a
datagrid in it that supports paging. When I click on the next or prev buttons
of the datagrid in a user control it should display the next page results.
but it is not doing so. I am not knowing how to call the PageIndexChanged
event of the user control from the webpage. could anyone show me a code
sample to do that? I am using VB.NET for this.

Thanks,
Sridhar.
 
G

Guest

If your control is an ascx that you load using the LoadControl method
http://msdn.microsoft.com/library/d...webuitemplatecontrolclassloadcontroltopic.asp
then you have to redo that LoadControl method in the Page_init upon postback.
This should take care of the all the event handling that you have defined in
your ascx control.
--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Sridhar said:
Hi
Thanks for the reply. But the problem is I am getting the control name
dynamically and loading it dynamically. In that case how would I know which
events shoud I add?

Thanks,
Sridhar.

Phillip Williams said:
Dynamically loading controls must be done during the Page.Init event handling
(page initialization stage) otherwise they would lose they ViewState and
their events would not fire. So if you added a control based on a
SelectedIndexChanged, it is added late in the page lifecycle and you have to
persist a condition that allows you to reload it during the page.init event
handling, otherwise no response would happen to any of its events.

For a simple demo on this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

For a clear explanation of the page life cycle:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Sridhar said:
Hi,

I have created a web page which includes a place holder. I also have a
dropdown list in that webpage. when I select one of the choices in that
dropdown list, It will load a user control into the place holder. This is
done dynamically based on the choice they selected. This user control has a
datagrid in it that supports paging. When I click on the next or prev buttons
of the datagrid in a user control it should display the next page results.
but it is not doing so. I am not knowing how to call the PageIndexChanged
event of the user control from the webpage. could anyone show me a code
sample to do that? I am using VB.NET for this.

Thanks,
Sridhar.
 
G

Guest

Hi Karl,

Yes you are right regarding this question being frequently asked. (I am
sorry I did not understand the abbreviations you used in your post: imma and
HAR)

Regards,

Phillip

Karl Seguin said:
Philip:
This question gets asked all the time, imma keep ur links and pass them off
as my own helpfulness next time! HAR!

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Phillip Williams said:
Dynamically loading controls must be done during the Page.Init event
handling
(page initialization stage) otherwise they would lose they ViewState and
their events would not fire. So if you added a control based on a
SelectedIndexChanged, it is added late in the page lifecycle and you have
to
persist a condition that allows you to reload it during the page.init
event
handling, otherwise no response would happen to any of its events.

For a simple demo on this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

For a clear explanation of the page life cycle:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Sridhar said:
Hi,

I have created a web page which includes a place holder. I also have a
dropdown list in that webpage. when I select one of the choices in that
dropdown list, It will load a user control into the place holder. This
is
done dynamically based on the choice they selected. This user control has
a
datagrid in it that supports paging. When I click on the next or prev
buttons
of the datagrid in a user control it should display the next page
results.
but it is not doing so. I am not knowing how to call the PageIndexChanged
event of the user control from the webpage. could anyone show me a code
sample to do that? I am using VB.NET for this.

Thanks,
Sridhar.
 
G

Guest

Thank you so much. It is working now.

Karl Seguin said:
Dynamically added controls need to be readded on postback. If you don't
add it back, how can an event ever be fired for it, it doesn't exist!.

You can use Denis Bauer's DynamicControlsPlaceholder (free):
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
which automatically re-adds dynamic controls on postback, or you can do it
yourself. Here's a dummy example:

sub Page_Load()
if Page.IsPostBack AndAlso not ViewState("controlToReload) is nothing then
plc.Controls.Add(cstr(ViewState(controlPath)))
end if
end sub

sub selectedindexchange(..)
string controlPath = "mycontrol.ascx"
plc.Controls.Add(Page.LoadControl(controlPath))
ViewState.Add("controlToReload", controlPath)
end sub
 
K

Karl Seguin

imma = im going to
and HAR is my pirate laugh...HAR HAR!

Karl ;)

--
MY ASP.Net tutorials
http://www.openmymind.net/


Phillip Williams said:
Hi Karl,

Yes you are right regarding this question being frequently asked. (I am
sorry I did not understand the abbreviations you used in your post: imma
and
HAR)

Regards,

Phillip

Karl Seguin said:
Philip:
This question gets asked all the time, imma keep ur links and pass them
off
as my own helpfulness next time! HAR!

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Phillip Williams said:
Dynamically loading controls must be done during the Page.Init event
handling
(page initialization stage) otherwise they would lose they ViewState
and
their events would not fire. So if you added a control based on a
SelectedIndexChanged, it is added late in the page lifecycle and you
have
to
persist a condition that allows you to reload it during the page.init
event
handling, otherwise no response would happen to any of its events.

For a simple demo on this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

For a clear explanation of the page life cycle:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Hi,

I have created a web page which includes a place holder. I also have
a
dropdown list in that webpage. when I select one of the choices in
that
dropdown list, It will load a user control into the place holder.
This
is
done dynamically based on the choice they selected. This user control
has
a
datagrid in it that supports paging. When I click on the next or prev
buttons
of the datagrid in a user control it should display the next page
results.
but it is not doing so. I am not knowing how to call the
PageIndexChanged
event of the user control from the webpage. could anyone show me a
code
sample to do that? I am using VB.NET for this.

Thanks,
Sridhar.
 
G

Guest

No piracy at all. :) I believe all information posted on the newsgroup is
free to share. You are quite welcome to do so.

Regards,
Phillip

Karl Seguin said:
imma = im going to
and HAR is my pirate laugh...HAR HAR!

Karl ;)

--
MY ASP.Net tutorials
http://www.openmymind.net/


Phillip Williams said:
Hi Karl,

Yes you are right regarding this question being frequently asked. (I am
sorry I did not understand the abbreviations you used in your post: imma
and
HAR)

Regards,

Phillip

Karl Seguin said:
Philip:
This question gets asked all the time, imma keep ur links and pass them
off
as my own helpfulness next time! HAR!

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Dynamically loading controls must be done during the Page.Init event
handling
(page initialization stage) otherwise they would lose they ViewState
and
their events would not fire. So if you added a control based on a
SelectedIndexChanged, it is added late in the page lifecycle and you
have
to
persist a condition that allows you to reload it during the page.init
event
handling, otherwise no response would happen to any of its events.

For a simple demo on this concept:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

For a clear explanation of the page life cycle:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Hi,

I have created a web page which includes a place holder. I also have
a
dropdown list in that webpage. when I select one of the choices in
that
dropdown list, It will load a user control into the place holder.
This
is
done dynamically based on the choice they selected. This user control
has
a
datagrid in it that supports paging. When I click on the next or prev
buttons
of the datagrid in a user control it should display the next page
results.
but it is not doing so. I am not knowing how to call the
PageIndexChanged
event of the user control from the webpage. could anyone show me a
code
sample to do that? I am using VB.NET for this.

Thanks,
Sridhar.
 

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,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top