By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,077 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,077 IT Pros & Developers. It's quick & easy.

Placeholder and Events

P: n/a
I have a drop-down list, a radio button list and a submit button. I'm
adding these controls to a table and I'm adding the table to a Placeholder.
I'm adding it to the Placeholder because I don't know exactly where the
table will be located on the page until runtime. Before the form control
table is added to the Placeholder, I'm adding a whole bunch of tables to the
Placeholder. This is a flowchart program and I have multiple action boxes
and decision boxes on the page.

When I add the form controls to the Placeholder, the events for the
drop-down list and radio button list stop working. It's as if they
dissapear into a black hole and after the SelectedIndexChange event is fired
for the controls, the program looks for the event but can't find it so it
skips over it. Interestingly, the submit button's event still works.

Is there a way to get my drop-down list and radio button list events to
start working again?

Is there a way to do this without using a Placeholder? I could do
this.Controls.Add but I think it would be added outside of the </HTML> tag
and the form controls have to be within the <HTML> part.
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
blue,

This might be a problem with the dynamically created objects not being
recreated on post back. For an object's events to fire it must exist. That
means that if you create an object dynamically it must be recreated on post
back in order for it's events to fire.

My own web site, www.aboutfortunate.com, uses this technique and I had to
create some subroutines to place controls to load into viewstate and then
retrieve and recreate those same controls on post back.

I've placed the code I created in the code library on the site. It might
help you out to take a look at it. Just click the code library link and use
the search box to search for: "Dynamic Controls" the title of the entry you
want is: "Reload dynamically created user controls in order to use their
viewstate".

(Using viewstate of a dynamically created control has the same difficulty as
getting the control's event to fire.)

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"blue" <bl**@arizona.edu> wrote in message
news:e2*************@TK2MSFTNGP10.phx.gbl...
I have a drop-down list, a radio button list and a submit button. I'm
adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't know exactly where the
table will be located on the page until runtime. Before the form control
table is added to the Placeholder, I'm adding a whole bunch of tables to the Placeholder. This is a flowchart program and I have multiple action boxes
and decision boxes on the page.

When I add the form controls to the Placeholder, the events for the
drop-down list and radio button list stop working. It's as if they
dissapear into a black hole and after the SelectedIndexChange event is fired for the controls, the program looks for the event but can't find it so it
skips over it. Interestingly, the submit button's event still works.

Is there a way to get my drop-down list and radio button list events to
start working again?

Is there a way to do this without using a Placeholder? I could do
this.Controls.Add but I think it would be added outside of the </HTML> tag
and the form controls have to be within the <HTML> part.

Nov 17 '05 #2

P: n/a
"S. Justin Gengo" <> wrote in message
news:ur**************@TK2MSFTNGP12.phx.gbl...
blue,

This might be a problem with the dynamically created objects not being
recreated on post back. For an object's events to fire it must exist. That
means that if you create an object dynamically it must be recreated on post back in order for it's events to fire.

My own web site, www.aboutfortunate.com, uses this technique and I had to
create some subroutines to place controls to load into viewstate and then
retrieve and recreate those same controls on post back.


Justin,

Thanks for the help. I'm a little confused about your coding example. This
is partly because I'm not familiar with VB.NET.

<snip>
If CType(ViewState("ReloadControl"), String)> "" Then
</snip>

Is the "ReloadControl" part supposed to be the control that I'm testing to
see if it exists? So, if my submit button was called btnSubmit, I would put
"btnSubmit" there instead? Do I have to use the CType part? I think that's
a VB thing. I am just creating an object equal to ViewState["btnSubmit"] (I
think it has to have square brackets for C#) and if it's null I try to add
it back. The problem is, it always returns null, even on the first
page_load.

<snip>
plchldrContent.Controls.Add(LoadControl(ControlToL oad))
</snip>

What is "plchldrContent"? Is that your PlaceHolder?

My PlaceHolder is called "PlaceHolder1". I tried
"PlaceHolder1.Controls.Add(LoadControl("btnSubmit" ));" and it crashed with
"User control source files must have a .ascx file extension.".

Thanks,
blue

Nov 17 '05 #3

P: n/a
blue,

In my example I'm reloading a single control.

Now my responses to your questions (in-line):
<snip>
If CType(ViewState("ReloadControl"), String)> "" Then
</snip>

Is the "ReloadControl" part supposed to be the control that I'm testing to
see if it exists? So, if my submit button was called btnSubmit, I would put "btnSubmit" there instead?
No, you would not put btnSubmit in there. When I load the control for the
first time, I store the path to the control I just loaded in view state:

'---controlToLoad hold the path to the control (i.e.
"controls/Biography.ascx")
ViewState("ReloadControl") = controlToLoad

If that value exists in viewstate on a post back I have everything I need to
reload the control.

Do I have to use the CType part? I think that's
a VB thing.
CType() '---Cast As Type (Casts the object stored in viewstate as a string.)

I am just creating an object equal to ViewState["btnSubmit"] (I
think it has to have square brackets for C#) and if it's null I try to add
it back. The problem is, it always returns null, even on the first
page_load.
'---This would not be used on first page load at all.

'---Load your control the first time the same way you always do.

'---Just after you load your control (or just before for that matter) store
the path to the ' control you're loading in view state.

MyPlaceHolder.Controls.Add(LoadControl("ControlsFo lder/MyControl.ascx"))

ViewState("btnSubmit") = "ControlsFolder/MyControl.ascx"
'---Next in the page load subroutine add in code that fires on post back
only.
' Then check if there is viewstate for your control. If there is reload
it.
' In VB.Net it would look like:

If Not IsPostBack Then
'---Get path from viewstate.
Dim PathToControl As String = ViewState("btnSubmit")

'---Check if the control should be reloaded (if path exists it should be
reloaded).
If PathToControl > "" Then
MyPlaceHolder.Controls.Add(LoadControl())
End If
End If
Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"blue" <bl**@arizona.edu> wrote in message
news:%2******************@tk2msftngp13.phx.gbl... "S. Justin Gengo" <> wrote in message
news:ur**************@TK2MSFTNGP12.phx.gbl...
blue,

This might be a problem with the dynamically created objects not being
recreated on post back. For an object's events to fire it must exist. That means that if you create an object dynamically it must be recreated on post
back in order for it's events to fire.

My own web site, www.aboutfortunate.com, uses this technique and I had to create some subroutines to place controls to load into viewstate and then retrieve and recreate those same controls on post back.


Justin,

Thanks for the help. I'm a little confused about your coding example.

This is partly because I'm not familiar with VB.NET.

<snip>
If CType(ViewState("ReloadControl"), String)> "" Then
</snip>

Is the "ReloadControl" part supposed to be the control that I'm testing to
see if it exists? So, if my submit button was called btnSubmit, I would put "btnSubmit" there instead? Do I have to use the CType part? I think that's a VB thing. I am just creating an object equal to ViewState["btnSubmit"] (I think it has to have square brackets for C#) and if it's null I try to add
it back. The problem is, it always returns null, even on the first
page_load.

<snip>
plchldrContent.Controls.Add(LoadControl(ControlToL oad))
</snip>

What is "plchldrContent"? Is that your PlaceHolder?

My PlaceHolder is called "PlaceHolder1". I tried
"PlaceHolder1.Controls.Add(LoadControl("btnSubmit" ));" and it crashed with
"User control source files must have a .ascx file extension.".

Thanks,
blue

Nov 17 '05 #4

P: n/a
"S. Justin Gengo" <sj*****@aboutfortunate.com> wrote in message
news:eq**************@TK2MSFTNGP12.phx.gbl...
blue,

In my example I'm reloading a single control.

Now my responses to your questions (in-line):


Justin,

I found a C# example that I used to fix the problem:
http://msdn.microsoft.com/library/de...imevisualc.asp

I couldn't have done it without your help.

Thanks,

Cindy
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.