Accessing JavaScript function in codebehind

V

Vinod Kumar

Hi All,

Can I access a Javascript function written in an ASP.NET from the
codebehind (C#) of that page? If yes, kindly give the code snippet to
do this.

Thanks and Regards


Vinod Kumar
 
H

Hermit Dave

what do you mean access the javascript function....
you can write your own custom javascript function from code behind using
RegisterClientScriptBlock.

to pass values to code behind... write you custom functions and populate
some hidden fields... before post

--
Regards,

HD

in code behind .cs file within Page

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
// Form the script that is to be registered at client side.
String scriptString = "<script language=JavaScript> function CancelMe()
{";
scriptString += "window.history.back(); }</script>";

if(!this.IsClientScriptBlockRegistered("clientScriptCancel"))
this.RegisterClientScriptBlock("clientScriptCancel", scriptString);

if(!Page.IsPostBack)
...............
 
V

Vinod Kumar

Hi Hermit Dave,

Thank you for your reply.

What I mean by accessing Javascript function from code behind is, I
already have written some Javascript function in the ASP.NEt page (not
thru codebehind), say

<Script language="JavaScript>
function LoadData()
{
some javascript code....
}
</SCRIPT>

Now can I access this LoadData() function from the code behind?

Thanks in advance.


Vinod Kumar
 
H

Hermit Dave

Vinod,

To my knowledge, Javascript exists on the client side and has no meaning on
server-side... or the code behind...
if you need to execute say the loadData on 'OnLoad' event on browser i would
say add attribute in code behind which will emit something like <body....
Onload='somefunction();'> to the client browser
or hard code it to the aspx script if you know the function name and that it
is going to be registered

Others might have some work around for this. See what they say..
 
A

Alvin Bruney

There is a way - a hack really - to access it using "server side code". But
the code must run in the aspx page as server-side script blocks. Here is a
complete test example:

<body MS_POSITIONING="GridLayout">
<script>
function test(){
alert('hello world');
}
</script>
<form id="Form1" method="post" runat="server">
<%
Response.Write("<script>test()</script>");
%>
</form>
</body>

This qualifies as a partial solution only because it isn't truly code
behind. Some situations, though rare, require that sort of programming. I'd
recommend that you modify your architecture to use attributes or script
registration like Hermit suggested. It's a cleaner, more object oriented way
of programming which makes for easier maintenance since programmers
examining codebehind aren't normally aware of server side script blocks in
an aspx page. If you absolutely must, consider documenting that sort of
behavior thoroughly.
 
V

Vinod Kumar

Hi Hermit Dave/Alvin Bruney,


Thank you very much for your replies. I will try out the solutions you
have suggested.


Thanks and Regards,


Vinod Kumar
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top