Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to do a Postback with Client Side Object

0.00/5 (No votes)
14 Jul 2004 1  
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