Rick Strahl's Weblog
Rick Strahl's FoxPro and Web Connection Weblog
White Papers | Products | Message Board | News |

Firing Server Events from Client Code in Web Connection 5.0


4 comments
April 27, 2006 •
The Web Control framework handles events in two ways:

  • Buttons
    Buttons are special objects and they automatically post back to the server. If no event target is found Web Connection runs through all buttons and checks to see if the button was clicked.

  • __doPostBack() Client Script
    Anything but stock buttons that posts back to the server uses some special client script to post events from the client to the server by writing out event information into a few hidden variables and submitting the form. You should always use this.Page.GetPostbackEventReference to insert this script.

Firing a Button from the Client


Button clicks is completely automatic and built into both HTML and Web Connection natively. There's nothing special about triggering clicks - they just automatically fire events on the server. If you want to trigger a button to post back to the server the easiest is simply to call the specific button's 'click' method in JavaScript code:

type="text/javascript"> function ScriptCode() { document.getElementById('btnSubmit').click(); }

This fires the click of the button and simply submits it. Easy.

Firing a custom Event from the client


But buttons are usually not the target of explicitly triggered events from the client. A more common scenario is that you click an item in a datagrid and want to have it fire an event on the server that doesn't navigate to a new page and instead handles the operation on the current page.

For example, you have a grid display and have a Delete button on the grid. When you click Delete you want to post back to the current form delete the selected record and then redisplay the grid without the deleted record.

You can fire ANY server side control event from the client with this sort of script:

__doPostBack('CtlUniqueId','Event','Parameter');

So if you wanted to fire a list box OnSelectedIndexChanged event:

__doPostBack('lstCustomers','SelectedIndexChanged',this.value);

This will trigger the SelectedIndexChanged event on the lstCustomers control on the server and pass whatever this.value is on the client and pass it to the server as a string value.

If you use code like this from the client make sure that you inlude the following somewhere in your server code explicitly to ensure that the post back script code always gets embedded:

FUNCTION OnLoad THIS.RegisterPostbackScriptCode() ... ENDFUNC

This function ensures that the __doPostBack() client script function as well as the event variables are available on the client.

Let's look at the above example I mentioned of a delete button in a DataGrid:

ID="gdCustomers" runat="server"> runat="server" ID="Company" Expression="Company" > runat="server" ID="WwWebDataGridColumn1" Expression="this.Page.DeleteExpression()" HeaderText="Action" >

The second column is a Delete link. I'm using an Expression that points back to the page to create the output for this column which is going to be a hyperlink that calls the javascript:__doPostBack() function. I'm using an expression because there will be embedded expressions in the string and it's easier to write in VFP than in the VS.NET editor.

FUNCTION DeleteExpression() RETURN [Delete] ENDFUNC FUNCTION DeleteCompany lcResult = Request.Form("__EventParameter") *** Delete Code would go here this.Errordisplay.ShowMessage("you've selected " + lcResult ) ENDFUNC

I created two methods in the Page class. The first generates the Expression to display and as you can see it creates the __doPostBack() function call for each link in the grid. It says:


  • Call the Page Class
  • Fire the DeleteCompany method
  • Pass the PK as a value to the method

The actual HTML generated inside of the grid column looks like this:

href="javascript:__doPostBack('Page','DeleteCompany','32');" >Delete

Note the special name 'Page' in this example. You can use Page or This to specify that you want to fire an event on the Page object. For any other control use its UniqueID value to identify the control (ie. this.txtButton.UniqueID).

When clicked this code now goes out and fires the Page.DeleteCompany method in the page. The mthod then can use Request.Form("__EventParameter") to retrieve the parameter that the client sent. So at this point you'd be ready to delete the record with a PK value of 32. The code above merely echos back the value passed.

Note parameters are always passed and retrieved as strings.

Note that if you generate the script reference on the server (ie. you're creating a control or you're using one of the Page.RegisterClientScriptxxx functions you should NEVER directly reference '__doPostback(...). Instead use this.Page.GetPostbackEventReference() which generates this code for you and does so with an easier interface to boot. The reason for this is that there may be future changes in the way events are called and this standardizes the way the events are generated in client code.

You can also apply this in client code with something like this:

function FireServerEvent()

{  

    <%= this.Page.GetPostbackEventReference(this.txtName.UniqueId,"change") %>;   

    // generates: __doPostback("Ctl0_txtName","change");

}

Posted in:

Feedback for this Weblog Entry


Re: Firing Server Events from Client Code in Web Connection 5.0



Rick Strahl
April 27, 2006

To facilitate this process a little bit more I added a GetPostbackEventReference function to the Page class. So you can use an expression right in the page:

Expression="HREF(this.Page.GetPostbackEventReference('Page','DeleteCompany',TRANS(pk),[Delete],.T.)"

A little easier to do this way.

Re: Firing Server Events from Client Code in Web Connection 5.0



John Long
April 30, 2006

It might be a good idea to put what version will allow these changes. It is sometimes hard to know whether you can use the change right now in the version you have, need to download the version it is in or it will be in the next release.

It is a great addition to calling a method.

Thanks,

John

Re: Firing Server Events from Client Code in Web Connection 5.0



Yael Haion
August 29, 2006

Hi Rick,

I used your example of firing server event from client script: __doPostBack('CtlUniqueId','Event','Parameter');

The event occurs only i cant read the parameter inside the server event ... how can i read it ?

thanks for your help

yael (:

Re: Firing Server Events from Client Code in Web Connection 5.0



Rick Strahl
August 29, 2006

Request.Form("__EventParameter") will give you the parameter value.

 
© Rick Strahl, West Wind Technologies, 2003 - 2024