NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Client-Side Java Script Generation

Unfortunately, not all HTML controls can generate form submits back to the server. To enable UI controls other than standard buttons and image buttons to participate in the postback framework, Web Forms supports a custom client-JavaScript eventing architecture that can also be used to initiate form postbacks to the server.

This is accomplished using two hidden form fields and a small script function that are rendered on demand (by the HTML form control) to the client. The hidden form fields indicate which server control should be posted to, and optionally specify an argument to be passed. The small JavaScript function is used to set the hidden form field contents and initiate the actual form submit to the server.

For example, the following HTML code shows some sample output from a page with two linkbutton server controls that support postback events from HTML anchor style output:

<script language="JavaScript">
   function __doPostBack(eventTarget, eventArgument) {
     var theForm = document.HtmlForm0;
     theForm.__EVENTTARGET = eventTarget;
     theForm.__EVENTARGUMENT = eventArgument;
     theForm.submit();
   }
</script>

<form name="Form1">
   <input type="hidden" name="__EVENTTARGET" value="">
   <input type="hidden" name="__EVENTARGUMENT" value="">

   Click below links to cause postbacks:

   <a href="jscript:__dopostback('linkbutton1', 'Click')"> LinkButton1 </a>   
   <a href="jscript:__dopostback('linkbutton2', 'Click')"> LinkButton2 </a>
</form>

When a postback event on the page occurs, the page framework will examine the control tree hierarchy for a server control whose UniqueId property matches that specified in the __EVENTTARGET hidden field. The page framework will then query the target control for the IPostBackEventHandler interface and, if found, invokes the IPostBackEventHandler.RaisePostBackEvent method, passing the eventArgument field as an argument. For example:

public class MyLinkButton : Control, IPostBackEventHandler {

   private EventHandler   clickHandler;

   // Register delegate to external "OnServerClick" event listener, and then notify
   // the page instance that a client-side postback script block needs to be generated
   public void AddOnServerClick(EventHandler value) {
     clickHandler = (EventHandler) Delegate.Combine(clickHandler, value);
     Page.RegisterPostBackScript();
   }

   // Process and raise appropriate incoming events
   public void RaisePostBackEvent(String eventArgument) {
     if (clickHandler != null) 
        clickHandler(this, EventArgs.Empty);
   }

   // Generate html button and use appropriate helper method to get call back script
   public override void Render(HTMLTextWriter output) {
     output.Write("<a href="javascript:" + Page.GetPostBackEventReference(this)" + ">");
     output.Write("    " + this.UniqueId + " \n");
     output.Write("</a>");
   }
}

Note that the client-side JavaScript function is not generated by default during page rendering. Rather, a control must explicitly indicate that it wants this to be rendered by calling the Page.RegisterPostBackScript method. They can then use the Page.GetPostBackEventReference helper methods to generate calls to the function (with appropriate parameters) at run time.