i have a code behind function "GenerateTreeView" which populates a tree control on the form load event.
say i have the following jscript function. which simply hides/shows the tree view. is it possible to also call a codebehind function GenerateTreeView within the javascript function?
something along these lines?
function ToggleTreeView()
{
if (document.getElementById("treeDiv").style.visibility ==
"visible")
HideTreeView();
else
/// id like to call GenerateTreeView here before showing the tree
the reason i ask this is because im having a bit of a problem with a treeview and IFrame i am using on a page
basically depending on what node the user clicks a page is then loaded into the IFrame where the user can then add new or update existing data relating to that node.
When the user adds/updates the data within the IFrame i need to then be able to refresh the treeview on the parent page. At the moment, the only solution i have is to force the Parent page to postback & therefore repopulate the treeview. but
this causes the page within the IFrame to be lost as it returns to the default page.
i cant reload the previous page into the IFrame either as i require a lot of data of the selected node, but this has since been lost with the postback.
// For demo purposes, I'll just add it to a button
// not letting the Button PostBack by returning false...
this.Button1.Attributes.Add("onclick", "JavaScript:serverSideCallback(); return false;");
}
// The server-side method to call...
private void callbackFromClientSide()
{
this.Response.Write("callbackFromClientSide() executed<br>");
}
Could you explain a wee bit more what exactly is happening here
i tried what you have above and also with an HTML button with the onclick="JavaScript:serverSideCallback(); return false;" but on both occassions it causes the page to postback.
i've converted it from C# to VB.net, but this shouldnt be an issue
The problems is that Codebehind classes are executed on the server and Javascript is executed on the client ....
Causing a postback allows the codebehind to execute and is a possible solution.
With javascript you could manipulate the rendered html controls directly on the client, but you would have to know the id's of the individual controls. The javascript has no knowledge of the Server control but can certainly see the resulting html.
i am using AJAX , & the data i am getting by using XmlHttpRequest is in XML. now i want to call some code behind function to process that data . so plz help me by telling how to call the code behind function from javascript!
Gambit
Participant
900 Points
229 Posts
can you call a codebehind function from within javascript?
Oct 21, 2005 09:26 AM|LINK
say i have the following jscript function. which simply hides/shows the tree view. is it possible to also call a codebehind function GenerateTreeView within the javascript function?
something along these lines?
function ToggleTreeView()
{
if (document.getElementById("treeDiv").style.visibility == "visible")
HideTreeView();
else
/// id like to call GenerateTreeView here before showing the tree
ShowTreeView();}
orzeh
Contributor
4993 Points
897 Posts
Re: can you call a codebehind function from within javascript?
Oct 21, 2005 11:59 AM|LINK
It`s possible with AJAX, but it`s easier to do this in "normal" way.
orzeh
Gambit
Participant
900 Points
229 Posts
Re: can you call a codebehind function from within javascript?
Oct 24, 2005 10:06 AM|LINK
the reason i ask this is because im having a bit of a problem with a treeview and IFrame i am using on a page
basically depending on what node the user clicks a page is then loaded into the IFrame where the user can then add new or update existing data relating to that node.
When the user adds/updates the data within the IFrame i need to then be able to refresh the treeview on the parent page. At the moment, the only solution i have is to force the Parent page to postback & therefore repopulate the treeview. but this causes the page within the IFrame to be lost as it returns to the default page.
i cant reload the previous page into the IFrame either as i require a lot of data of the selected node, but this has since been lost with the postback.
how hard would it be to achieve this via AJAX?
Cheers,
Craig
Tauseef
Member
85 Points
17 Posts
Re: can you call a codebehind function from within javascript?
Oct 24, 2005 05:43 PM|LINK
Please let us know, how can we call code behind function in javascript.
TAM
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: can you call a codebehind function from within javascript?
Oct 24, 2005 06:16 PM|LINK
Try this out. It calls the callbackFromClientSide() method from client-side JavaScript.
private void Page_Load(object sender, System.EventArgs e)
{
if ( this.IsPostBack )
{
string eventArgument = this.Request["__EVENTARGUMENT"];
if ( eventArgument != null && eventArgument == "ServerSideCallback" )
{
callbackFromClientSide();
}
}
string scriptKey = "ServerSideCallbackScript";
// Add the JavaScript to the page...
if ( !this.IsClientScriptBlockRegistered(scriptKey) )
{
StringBuilder sbScript = new StringBuilder();
sbScript.Append("\n");
sbScript.Append("<script language=\"JavaScript\" type=\"text/javascript\">\n");
sbScript.Append("<!--\n");
sbScript.Append("function serverSideCallback()\n");
sbScript.Append("{\n");
sbScript.Append(" " + this.GetPostBackEventReference(this, "ServerSideCallback") + ";\n");
sbScript.Append("}\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");
this.RegisterClientScriptBlock(scriptKey, sbScript.ToString());
}
// For demo purposes, I'll just add it to a button
// not letting the Button PostBack by returning false...
this.Button1.Attributes.Add("onclick", "JavaScript:serverSideCallback(); return false;");
}
// The server-side method to call...
private void callbackFromClientSide()
{
this.Response.Write("callbackFromClientSide() executed<br>");
}
NC...
Gambit
Participant
900 Points
229 Posts
Re: can you call a codebehind function from within javascript?
Oct 26, 2005 10:11 AM|LINK
Could you explain a wee bit more what exactly is happening here
i tried what you have above and also with an HTML button with the onclick="JavaScript:serverSideCallback(); return false;" but on both occassions it causes the page to postback.
i've converted it from C# to VB.net, but this shouldnt be an issue
sbScript is being written as follows
"
<script language="JavaScript" type="text/javascript">
<!--
function serverSideCallback()
{
__doPostBack('','ServerSideCallback');
}
// -->
</script>
"
Cheers,
Craig
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: can you call a codebehind function from within javascript?
Oct 26, 2005 11:25 AM|LINK
1. Do a PostBack
2. Use Ajax.
The simplest way is doing the PostBack and that's what I posted.
NC...
Gambit
Participant
900 Points
229 Posts
Re: can you call a codebehind function from within javascript?
Oct 26, 2005 01:50 PM|LINK
delyk
Member
70 Points
16 Posts
Re: can you call a codebehind function from within javascript?
Oct 26, 2005 07:03 PM|LINK
The problems is that Codebehind classes are executed on the server and Javascript is executed on the client ....
Causing a postback allows the codebehind to execute and is a possible solution.
With javascript you could manipulate the rendered html controls directly on the client, but you would have to know the id's of the individual controls. The javascript has no knowledge of the Server control but can certainly see the resulting html.
er_riteshsha...
Member
25 Points
5 Posts
Re: can you call a codebehind function from within javascript?
Nov 08, 2005 06:41 AM|LINK
hi
i am using AJAX , & the data i am getting by using XmlHttpRequest is in XML. now i want to call some code behind function to process that data . so plz help me by telling how to call the code behind function from javascript!
ritesh