you dont need to put any tag,we have a string variable('s')that keeps JavaScript codes,
Code:
//JavaScript Syntax
var txtbox1=document.getElementById('TextBox1');//finds first textbox
var txtbox2=document.getElementById('TextBox2');//finds second textbox
I supposed the ID's of your textboxes were TextBox1 and TextBox2(in code-behind) then we test
value property(JavaScript property) of every textbox according to your condition,
Code:
//JavaScript Syntax
if (txtbox1.value=='' && txtbox2.value=='' || txtbox1.value!='' && txtbox2.value!='')
{window.alert('Validation not acceptable.');return false;}
I return false because I dont want Button_Click event to be fired off,
now I should add this javascript code to
onclick attribute somewhere like Page_Load,
Code:
//C# Syntax
//s keeps that javascript code I mentioned
Button.Attributes.Add("onclick",s);
There are two points here,
1-Button_Click is a server-side event for the Button,
2-onclick attribute is a client-side event for the same Button,
when an user clicks the Button first client-side event check the condition,
if condition is true(invalid inputs)the onclick attribute shows a po pup window,
and
return false so Button_Click is not fired off any more,
but if condition is false(valid inputs),Button_Click is fired,
(put codes come after a successful validation in your Button_Click event)
Code:
//Final Code
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string s="";
s+="var txtbox1=document.getElementById('TextBox1');var txtbox2=document.getElementById('TextBox2');";
s+="if (txtbox1.value=='' && txtbox2.value=='' || txtbox1.value!='' && txtbox2.value!=''){window.alert('Validation not acceptable.');return false;}";
Button.Attributes.Add("onclick",s);
}
}
private void Button_Click(object sender, System.EventArgs e)
{
//codes are executed on the server-side after a successful validation
}
this method works just like this page,look at the bottom of this page,
if you try to send an empty message a po pup window notices you,
and stops sending your request to server.
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.