How to call a codebehind method from clientside javascript

G

Guest

Hi,

I am using .Net 2003 , c# asp.net.
The situation i have is this. I have a asp.net page which in the codebehind
maintains an xml document in memory. The user can click on a button to popup
a windows to select some items, this is done via window.open, I then return
the selected items via the returnValue, when I receive the data back I need
to be able to call into a function in my main page codebehind to update the
xml.

Thus my question is how can a clienttside javascript function call a
function that resides in codebehind, I envisage that it may be something to
with the __doPostBack function.

In addition how do I call the control_SelectedIndexChanged() function from
client side java script.
 
E

Eliyahu Goldin

Rob,

Client is a client and server is a server. They live in two different
worlds. Client can't call any codebehind methods. What it can do is to pass
to the server a sort of info that the server will understand as a request
for an action involving calling a codebehind method.

Now, your question is how to communicate an action with a few parameters
from client to server. I don't recommend using __doPostBack etc. It is a
sort of internal function and as such can change with another ASP.NET
release. It is not even documented in MSDN. The standard and much simpler
solution is to provide hidden <input> fields that you will fill on
clientside with the action code and parameters and to read them on
serverside.

Eliyahu
 
S

Steven Cheng[MSFT]

Hi Rob,

I think Eliyahu 's suggestions are reasonable. Generally if we want to
manually post back the page so as to do some serverside works, we have the
following means:
1. Put some hidden fields (<input type="hidden" .>) on the page which
used to store some flag, then we use document.forms[0].submit() to post
back the page and in the Page_Load event we can call the correct function
we need according to the hidden fields' value.

2. Put an hidden Html input button on the page and use script to call the
button's clientside "click()" method so as to post back the page. For
example:

<input type="button" id="btnHidden" style="display:none" runat="server"
onserverclick"serverside_handler">

we can use the following script to make the button clicked:

var btn = document.getElementById("btnHidden");
btn.click();

"severside_handler" is the codebehind function that hander the button's
server click event.

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Rob,
What you describe really sounds like Remote Scripting (now inaccurately
dubbed "AJAX") , which would be done with XmlHttpRequest. ASP.NET 2.0 has
some of this functionality with Client Script Callbacks.
Peter
 
Joined
Jun 17, 2010
Messages
2
Reaction score
0
thanks. It helped me

thanks. It helped me

Steven Cheng[MSFT];411747 said:
Hi Rob,

I think Eliyahu 's suggestions are reasonable. Generally if we want to
manually post back the page so as to do some serverside works, we have the
following means:
1. Put some hidden fields (<input type="hidden" .>) on the page which
used to store some flag, then we use document.forms[0].submit() to post
back the page and in the Page_Load event we can call the correct function
we need according to the hidden fields' value.

2. Put an hidden Html input button on the page and use script to call the
button's clientside "click()" method so as to post back the page. For
example:

<input type="button" id="btnHidden" style="display:none" runat="server"
onserverclick"serverside_handler">

we can use the following script to make the button clicked:

var btn = document.getElementById("btnHidden");
btn.click();

"severside_handler" is the codebehind function that hander the button's
server click event.

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top