Click here to Skip to main content
11,708,312 members (58,640 online)
Click here to Skip to main content

How to do a Postback with Client Side Object

, 14 Jul 2004 110.8K 25
Rate this:
Please Sign up or sign in to vote.
How to do a Postback with Client Side Object

Introduction

This article shows how to do a Postback with Client Side Object

Using the code

Use of Input Button

Switch to View-HTML-Code and then inside the <form> tag add the following code :-
<input type=button value="Click Me" id="htmlClientButton" 
 onclick="javascript:doButtonPostBack();" 
 runat="server" NAME="htmlClientButton">

Add the onclick client event inside the <head> tag

<script language="javascript">
  function doButtonPostBack(){ 
    alert("Welcome to my input button PostBack");
  }
</script>

Now we can do the Server code. When we added the runat=server the designer created a server object for us to use. Now we just need to attach a method / event for it

this.htmlClientButton.ServerClick += 
  new EventHandler(htmlClientButton_ServerClick);

private void htmlClientButton_ServerClick(object sender, EventArgs e)
{
  /// do some Server Code
  this.Page.Controls.Add(new LiteralControl(''<HR>''));
}

Tablecell PostBack

Do the same as with the button, only difference is that you are going to set the style to hidden
<input type=button value="Click Me" id="htmlClientButton" 
 onclick="javascript:doButtonPostBack();" 
runat="server" NAME="htmlClientButton" style="VISIBILITY: hidden">

Attach a Client Side Method Call

<TD onclick="javascript:doTableCellPostBack();"></TD>    

Now we need to add the onclick script inside the <head> tag. Add the following code

<script language="javascript">
function CellPostBack(){ 
    alert("Hello Cell PostBack "); 
    var theform; 
    if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
        theform = document.forms["Form1"]; 
    }
    else { 
        theform = document.Form1; 
    } 
     theform.__EVENTTARGET.value = "MyCellButton"; // Hiddin Button ID 
    theform.__EVENTARGUMENT.value = null; 
    theform.submit(); 
}    
</script>

That's all.

Conclusion

Feedback is expected and appreciated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Neil de Weerdt
Web Developer
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generalgood article but Pin
wu_jian83016-Jul-04 18:32
memberwu_jian83016-Jul-04 18:32 
GeneralRe: good article but Pin
Neil de Weerdt18-Jul-04 20:24
memberNeil de Weerdt18-Jul-04 20:24 
GeneralRe: good article but Pin
mark4asp18-Oct-07 22:02
membermark4asp18-Oct-07 22:02 
GeneralA better way Pin
Greg Ennis15-Jul-04 10:08
memberGreg Ennis15-Jul-04 10:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 15 Jul 2004
Article Copyright 2004 by Neil de Weerdt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid