Urgent : Unsure of when to call a function within a user control

J

John

Hi all,

This really is quite an urgent matter.

I have a page with multiple, dynamically-loaded user controls and when a
user clicks on a button, the whole form is submitted. Now at this stage I
know I need to call a function that will save data but I'm not sure exactly
when to call this function.

I've tried two ways and both seem to have 'gotcha's':

1. In the Page_Load event of the PAGE, I call LoadControl to load the user
control (I need to do this to restore viewstate)
2. Immediately after the LoadControl call, I call myusercontrol.Save() but
the viewstate has not been restored since the Page_Load event of the user
control hasn't fired.

so I then tried :

1. In the Page_Load event of the PAGE, I call LoadControl to load the user
control (I need to do this to restore viewstate)
2. Immediately after the LoadControl call, I populate a session variable to
use in the Page_Load event of my user control.
3. The Page_Load event of my user control fires (because the Page_Load event
of my PAGE ended normally)
4. Right at the end of the Page_Load event of my user control, I call the
Save method contained within but during the save, I don't have access to the
controls on my form (agaion, a viewstate problem).

Exactly where am I supposed to call the public Save method of my user
control?

I hope I've explained clearly as this needs a good, proper solution asap.
Thankyou!

Regards
John.
 
W

William F. Robertson, Jr.

Why don't you wire the Save method of your user control to the click
function that is submitting the form? That way, after you LoadControl, the
event will be wired and when the click event is sent, it will run the Save
method?

bill
 
S

S. Justin Gengo

John,

I'm also using dynamically loaded user controls and I also restore the
controls to access view state properties when the form is submitted.

I then call my save, delete, etc. functions using the OnBubbleEvent.

Any button in a user control automatically "bubbles" it's click event (and
the others too...) up to the page the control is hosted on.

Here's a small sample to get you going...

Protected Overrides Function OnBubbleEvent(ByVal source As Object, ByVal
args As System.EventArgs) As Boolean
'---Get the object that triggered the event
Select Case (source.GetType.ToString)
Case "System.Web.UI.WebControls.Button"
'---Get the button
Dim Button As Button = CType(source,
System.Web.UI.WebControls.Button)

'---Find out which button was clicked
Select Case Button.Id
Case "Submit"
'---Call the submit sub
Call MySubmitSub()

Case "Delete"
'---Call the delete sub
Call MyDeleteSub()

End Select

Case "System.Web.UI.WebControls.ImageButton"
'---Find out which imagebutton was clicked

End Select
End Function

I hope this helps.

Justin
 
J

John

O.K. thanks. I hear what you're saying but the form is submitted via a
javascript. I display a confirm box and if the user clicks OK, I use the
form.submit() method. What do I do then?
 
S

S. Justin Gengo

Why are you using form.submit()?

Instead why don't you attach the javascript confirm to the regular submit
button?

Button.Attributes.Add("onclick", "javascript:if (!confirm('Submit?') return
false;")

This way you would be able to use the submit button's onclick event.

--
S. Justin Gengo, MCP
Web Developer / Programmer

Free Code Library At:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 

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
474,089
Messages
2,570,602
Members
47,222
Latest member
jspanther

Latest Threads

Top