Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onSubmit for a webform 1

Status
Not open for further replies.

link9

Programmer
Nov 28, 2000
3,387
US
Hello all --

So I'm trying to get my own custom client side validation script to fire onSubmit of a webform. Here's the declaration of the form:

<form method=post id=theForm runat=server onSubmit=&quot;test();&quot;>

and the script:
function test(){
alert();
}

Inside the form, I have a datagrid, which is editable -- and I want the function to fire (and eventually do validation) when the form is submitted --

This would be onSort, onEdit, onUpdate, all of the events that do the postback, right? Well, the function won't fire, and I can't seem to figure out why.

Can someone shine the light for me?

thanks,
paul
penny1.gif
penny1.gif
 
I just tried to mimic whatyou're doing, and I couldn't get the datagrid buttons to fire the script when they were LINK buttons, but I could if I switched it to PUSH buttons.

which would make sense: hyperlinks don't trigger a postback, a button click would.

hth

Jack
 
Thanks, Jack.

A question, though... why would a hyperlink not trigger a postback? I mean... when the linkbutton is clicked, it calls a function which does the postback:

document.theForm.submit()

so why would an onSubmit function not fire?

is it the scripting version of a submit, .submit() that doesn't fire the script?

That seems a little weak to me -- not to mention something I've never observed before, although off the top of my head, I can't remember if I ever had something like that working in ASP classic.

thoughts?
penny1.gif
penny1.gif
 
When you put the buttons on a form, if you look at the source you'll see that they all turn to type=submit (when I viewed source on my test page, my 2 cmd buttons and all the buttons in my datagrid all were of type submit)

But if you put a LinkButton on a form, the source displays it as just that: a hyperlink ( <a href=&quot;&quot;></a>)

The code that teh link calls though isn't the
document.theform.submit(), its
javascript:__doPostBack('LinkButton1','')

But a postback isn't explicitly calling the submit, its just posting back the page.

I seem to remember this from my asp class in school as well: the submit needs a control of type submit to fire it, and I'm not sure that a hyperlink can be considered of type submit.

Jack
 
But a postback isn't explicitly calling the submit, its just posting back the page.

posting back the page is submitting -- that's what a postback is.

Here's an example of that function,

function __doPostBack(eventTarget, eventArgument) {
var theform = document.theForm;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}

See the last line? That does the same thing as pressing a submit button...

I guess that's why I just don't get why it wouldn't fire the same events as pressing the submit button.
penny1.gif
penny1.gif
 
I have to disagree: posting back does not automatically mean that you are calling the forms submit.

Consider the following:
A drop down list is added to the page. The drop down list, when rendered by asp.net, calls the SAME __doPostBack FUNCTION that the link button uses. When it is clicked, teh test code does not fire.

The onSubmit of the form can ONLY be triggered (again, from my understanding) by a control of type=Submit.

I would think that the javascript: __dopostback is used by asp.net to enable the code behind functions to fire, but is an apple to the forms onSubmit orange.

jack
 
And the mystery deepens...

DevGuru supports your argument: Form1.submit() should trigger the onSubmit of the form.

However, I tried to hardcode some html controls into the page, and I put in onClick=&quot;javascript:Form1.submit()&quot; to see if it would fire. For both the drop down list and the link boxes (hyperlinks), neither showed the alert when explicitly told to trigger the form.submit().

Hmmm....

btw, you know you COULD just use the validator controls that come with asp.net instead of writing your own validation script
;)

Jack
 
Ok, I think we're splitting hairs here, but OK ---

What I'm saying here is this:
.submit() submits the form.

Do you disagree with that statement? If not, then that's what the function does, right? That is the same function that your ASP.NET writes, is it not?

Ok, then I guess I'm back to the original question of -- I just don't see the difference (although there's no question that there is one) between clicking a submit button that submits the form, and calling .submit(), which also submits the form.

Submitting the form simply means that you snatch up all the data that's on the page, wrap it all up in a nice neat package, and send it to wherever the action of the form says to send it.

Now, when you click to sort a datagrid, you are sending data -- i.e. a form submit. If you click edit (be it a link button or a push button), then you are sending data -- again, a form submit.

postback -- postforward -- postsideways. It doesn't matter. Those are just words that describe what the action of the form is:

postback -- submits to itself
etc...

In ASP classic, I used postbacks all the time. I didn't call them postbacks, but I used them. I simply pointed the form to itself, and away we went. And what's killing me is that 99.9% of my &quot;postbacks&quot; were done using the .submit(). It was my method of choice, and I just don't ever remember noticing that calling this:

<a href=&quot;javascript:submitMe();&quot;>click me</a>

function submitMe(){
document.forms[0].submit()
}

didn't trigger an onsubmit() function. I recall it acting just like:

<input type=submit value=clickMe name=clickMe>

or whatever.

**sigh**

There has to be a simple explanation for this behavior. It just doesn't make good sense why the click of a button would act differently that the programmatic equivalent. I mean, what the hell is the button calling? It has to be calling some bit of code somewhere, does it not? I would bet good money that it was .submit(). Very good money.

And more to this point (as if I haven't beat this one to death) -- a link button updates my datagrid just fine, but won't trigger the same client side function as a pushbutton? I'm sorry, that just doesn't make good sense.

I'm not disputing that's the way that it is appearing to act, but there HAS to be more to this that we just don't know. There simply must be.
penny1.gif
penny1.gif
 
Yea, sure. I could use them if I didn't have moronic clients that like to make things so complicated that they would confuse Steven Hawking for crying out loud.

maybe then, yes.
penny1.gif
penny1.gif
 
I agree with you Paul. A very intriguing problem. In the past I know I have done exactly what you are trying - though with asp classic - validation on the submition of a form. I then pointed all my buttons to call the form.submit(). I am looking into this I will let you know what I find. That'l do donkey, that'l do
[bravo]
 
I think what i was saying was that .submit() DOES submit the form, but not the same way that it does when a submit button is clicked.

They're obviously two seperate things (from the observed behaviour anyway)

Hairsplitting aside, you're right: it doesn't make sense that the flow of the script isn't executing. I've posted in the javascript forum, and hopefully we can get some insight from them.

Otherwise, tell your clients to suck it up and learn to like the push buttons
;)

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top