Submitting form with multiple onclick events

S

Samuel Hon

Hi

I'm building a custom control which basically takes data from the user
and then submits it. I would use the validator controls but if I
understand correctly (I've been working with .Net for 2 weeks) you
cant 'embed' them in the custom controls. Someone correct me if I'm
wrong (and please point to an example :) )

So, to get around this, I'm using Javascript. In my control, I'm doing

Writer.WriteAttribute("OnClick", "javascript:checkData();" &
Page.GetPostBackEventReference(Me, "Data"))

for an Input item so I get something like

<input type="button" value="Submit"
OnClick="javascript:checkData();__doPostBack('MyControl','Data')" />

The checkData() function does this:

function checkLogin()
{
if (document.Data.item1.value == "" )
{
alert("Please enter item 1.");
return false;
}

if (document.Data.item2.value == "" )
{
alert("Please enter item 2.");
return false;
}

if (document.Data.item1.value != "" && document.Data.item2.value !=
"" )
{
return true;
}

}

My query is, how do I get the click event to fire correctly so that i
get alerts when there is a problem, and it passes through and submits
the form if there isnt a problem?

Thanks

Sam
 
N

Natty Gur

Hi,

Your client side script should use cancelBubble and returnValue to
disable event default process and raise alert if error found.

function CallServer()
{
if validate
{
__doPostBack('MyControl','Data');
}
else
{
alert("Error");
window.event.cancelBubble = true;
window.event.returnValue = false;
}
}


HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
S

Samuel Hon

Natty Gur said:
Hi,

Your client side script should use cancelBubble and returnValue to
disable event default process and raise alert if error found.

function CallServer()
{
if validate
{
__doPostBack('MyControl','Data');
}
else
{
alert("Error");
window.event.cancelBubble = true;
window.event.returnValue = false;
}
}


HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377

Thanks Natty

This means I have to create inline javascripts which I'd rather not do
if I can help it. Just as luck would have it, after spending an 'hour
or two' trying to find a solution before posting, 5 mins after posting
I find a solution :( Always the way...

Anyhow, just for reference purposes, what I've done is to validate on
OnMouseDown and postback on OnClick. Works a treat
 
P

Peter Blum

Hi Samuel,

Its unfortunate that you had to rework validation because of some incorrect
information about validators. The ASP.NET validators work fine within custom
controls. They have one key limitation related to their "location": the
ControlToValidate must be associated with the ID of a control in the same
"naming container". If your custom control impliments INamingContainer,
ControlToValidate can only point to other controls within your custom
control. If not, it can refer to other controls in the same naming container
that your custom control appears. Some types of naming containers: Page,
UserControl, and rows in DataGrid and DataList.

If you wanted to support validation without naming container limits, I wrote
a replacement to Microsoft's validators that overcomes that limitation among
numerous others: Professional Validation And More.
http://www.peterblum.com/vam/home.aspx.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
S

Samuel Hon

Peter Blum said:
Hi Samuel,

Its unfortunate that you had to rework validation because of some incorrect
information about validators. The ASP.NET validators work fine within custom
controls. They have one key limitation related to their "location": the
ControlToValidate must be associated with the ID of a control in the same
"naming container". If your custom control impliments INamingContainer,
ControlToValidate can only point to other controls within your custom
control. If not, it can refer to other controls in the same naming container
that your custom control appears. Some types of naming containers: Page,
UserControl, and rows in DataGrid and DataList.

If you wanted to support validation without naming container limits, I wrote
a replacement to Microsoft's validators that overcomes that limitation among
numerous others: Professional Validation And More.
http://www.peterblum.com/vam/home.aspx.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
<snip>

Thanks Peter for clearing that up, I will re-investigate

Sam
 

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
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top