Friday, January 26, 2007

How to combile and work with server side event and client side event

How to fire server side event from client side
Firstly ,Install fire bug add on or install any good text editor that is check for html code.Now start to create
Our web page.crate Test.aspx file and then add one asp Label control and two html control.like this.

Test.aspx
< id="Text1" type="text">
< id="Button1" type="button" value="Start" runat="server" onserverclick="Button1_ServerClick">
< id="lblmsg" runat="server" width="151px">


In button click event ,write like this.

protected void Button1_ServerClick(object sender, EventArgs e)
{
lblmsg.Text = "Hello !";
}

If you finish to create like this ,please run it and look source from browser-->view-->source.Now ,you will see like
this.If you have fire bug ,use it.

< !--other tag and script-- >
< type="text/javascript">

< /script >

< id="Text1" type="text">
< language="javascript" onclick="__doPostBack('Button1','')" name="Button1" type="button" id="Button1" value="Start">
< id="lblmsg" style="display:inline-block;width:151px;"> < /span >


So,Do you remember that we add three control to this form.Now find this control and check their id.Please record it.What you see in our button ,Do you see onlick event and __doPostBack method.record it.
And then goback to our aspx file.And repair like this .In head section you need to add one function

< type="text/javascript">
function ff(){
if(document.getElementById('Text1').value=="developer"){
__doPostBack('Button1','');
}
else {
alert("sorry!");
}
}
< /script >

before body close you need to add this .

< type="text/javascript">
document.getElementById('Button1').onclick =ff;
< /script >


Now ,Please run your code!what you see !
download source code
- Test.aspx
- Test.aspx.cs

No comments:

Google Ad