Saturday, March 24, 2012

Please help about Javascript for Asp.net Button

I have to validate so many controls at a time.I dont know what to Write in ControltoValidateProperty property in this case.So I went for writing Javascript function.

I wrote a function in javascript called "validateform" which checks all controls and validates the form.

Now when User clicks Submit Button I want to use this function.
The Problem is When i included that code in Button.attributes.add(...),Its giving me errormessages but also executing the server side code for button.

please help me

thxHow are you calling the Javascript function, from an OnClick event in the submit button or from an onSubmit call from the form. In either case you need to return false from the javascript function if a field does not validated. This will prevent the form from posting back and executing the server side code. If you could provide a code example, it would be easier for me to determine what is going wrong.
this is sample code

javascript :


function va()
{
if(document.all.p_title1.value=="")
{
alert("Team Name required");
return false;
}
else
return true;
}

Page Load :

ContinueButton.Attributes.Add("onclick","va();");

Button Server Event :


private void ContinueButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Redirect("SelectMaterial.aspx");

}

thx
I noticed the other day that if you don't specify that you want a value returned in the onclick event as well as returning the value from the function that the form postsback to the server regardless of the Javascript function return value, with that said, all you need to do is change you attributes.add statement like so.


ContinueButton.Attributes.Add("onclick","return va();");

That should solve the postback problem.
thank you very much.
It worked

0 comments:

Post a Comment