Placeholder and Events

  • Thread starter Thread starter blue
  • Start date Start date
B

blue

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.
 
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
 
S. Justin Gengo said:
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(ControlToLoad))
</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
 
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("ControlsFolder/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 said:
S. Justin Gengo said:
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(ControlToLoad))
</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
 
Back
Top