Click here to Skip to main content
Licence 
First Posted 14 Jul 2004
Views 89,755
Bookmarked 25 times

How to do a Postback with Client Side Object

By | 14 Jul 2004 | Article
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

About the Author

Neil de Weerdt

Web Developer

South Africa South Africa

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalgood article but Pinmemberwu_jian83018:32 16 Jul '04  
GeneralRe: good article but PinmemberNeil de Weerdt20:24 18 Jul '04  
GeneralRe: good article but Pinmembermark4asp22:02 18 Oct '07  
GeneralA better way PinmemberGreg Ennis10:08 15 Jul '04  

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.

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