Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET
Article

How to do a Postback with Client Side Object

Rate me:
Please Sign up or sign in to vote.
2.44/5 (6 votes)
14 Jul 2004 128.9K   25   4
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 :-
ASP.NET
<input type=button value="Click Me" id="htmlClientButton" 
 onclick="javascript:doButtonPostBack();" 
 runat="server" NAME="htmlClientButton">

Add the onclick client event inside the <head> tag

ASP.NET
<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

ASP.NET
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
ASP.NET
<input type=button value="Click Me" id="htmlClientButton" 
 onclick="javascript:doButtonPostBack();" 
runat="server" NAME="htmlClientButton" style="VISIBILITY: hidden">

Attach a Client Side Method Call

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

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

ASP.NET
<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


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalgood article but Pin
wu_jian83016-Jul-04 18:32
wu_jian83016-Jul-04 18:32 
GeneralRe: good article but Pin
Neil de Weerdt18-Jul-04 20:24
Neil de Weerdt18-Jul-04 20:24 
GeneralRe: good article but Pin
mark4asp18-Oct-07 22:02
mark4asp18-Oct-07 22:02 
GeneralA better way Pin
Los Guapos15-Jul-04 10:08
Los Guapos15-Jul-04 10:08 

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

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