-
July 14th, 2003, 06:36 AM
#1
using a client side event to fire a server side function ?
Well it has been a while since I posted last but been kinda busy. Ok,
I had something come up that I thought I would share. I had to make some menus for an internal web project that I was doing. One of the menu options needed to fire a Server Side function. I was thinking to my self "Self.. how do I do this one?"
give this a try.
This may not be the best way but it worked. Take a server control like a check box, set the AutoPostback = true.
Put a button on the same page name it something like cmdTest and hide it. Then double click on it and create a server side click event
in the event write
Respose.write("Hello World");
now take a button from the HTML controls of the tool box and put it on the page.
set it's onclick event to a client side function
onclick="test()"
When you put the checkbox on the page and set it's AutoPostBack = true a client side function is generated to handel the postback event. the function is named __doPostBack
there is a double underscore in that name so make sure that you type it correctly.
The first parameter that is passed to this function is the name of the Server control, on our case cmdTest the second value is the event to handel, the code will look something like this
<script language="JavaScript">
function test()
{
// the first parameter is the name of the control
// the second is the event to fire
__doPostBack('cmdTest','click');
}
</script>
now when you click your HTML button it will fire your server side function.
Please let me know if there are any problems with the code or if you have any questions or suggestions.
enjoy
Will
Last edited by womalley; July 14th, 2003 at 06:39 AM.
-
July 14th, 2003, 07:16 AM
#2
Good Idea!!
But couple of issues..
First issue is that to have this kind of solution to be worked you should have some server control like check box control with AutoPostBack property set to true, other wise ASP.NET framework will not generate __DoPostBack function and then this solution will not work
In actual case when a page may have a large number of controls this will not be an issue, but still u need a control with autopostback set to true.
Second issue is the signature of the Server side function which u want to call from client side. It must be associated with some server control and having the same signature as the function that the __DoPostBack function is calling. Here you could have kept the server side button visible and could write a client side function for the same server side button
Any way this is a good idea
regards,
Ashish Sheth
-
July 14th, 2003, 07:41 AM
#3
Originally posted by shethashish_a
Good Idea!!
But couple of issues..
First issue is that to have this kind of solution to be worked you should have some server control like check box control with AutoPostBack property set to true, other wise ASP.NET framework will not generate __DoPostBack function and then this solution will not work
In actual case when a page may have a large number of controls this will not be an issue, but still u need a control with autopostback set to true.
Second issue is the signature of the Server side function which u want to call from client side. It must be associated with some server control and having the same signature as the function that the __DoPostBack function is calling. Here you could have kept the server side button visible and could write a client side function for the same server side button
Any way this is a good idea
regards,
Thanks for the reply.
You could look at the html code and write your own postback routine, this was just a quick dirty way to do it but I see what you are saying.
As far as the second part.
You are correct the server side function that you are calling and the name of the button or control must be correct. You could attack a client side event that fired on click of a server control
this.button.attributes.add("onclick","test");
that way when you click the server control it would fire both a client side function and a server side function. How ever if you are using an HTML control that needs to fire a server side function, the best way that I have found is the __doPostBack function
I think the example that I gave before may not have been the best. just to clear this up alittle. Here is a better one
1) A server control checkbox with it's AutoPostBack set to true
2) An HTML button to use for the client side.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|