Avatar of swaroop_d
swaroop_d

asked on 

Javascript function from code behind. of C#.NET

Hello All,
I have to create a javascript function dynamically from a code behind in C#.NET.
How do I achieve this?

To be more clear..I have a master page which has two user controls..lets say left and right.
On a button click event on the left user control...after all the processing of the left control, i am generating some values. Now, I have to pass these values to a javascript function (which i will have to do from codebehind itself). Which means i have to develop a javacript function in code behind which will have values dynamically..different everytime. Now, I need to pass this function to the right user control to display.
What are the ways to achieve all these steps?
I am confused as to where i should put all the code?

Your guidance will help..

Thanks in advance.
JavaScriptC#ASP.NET

Avatar of undefined
Last Comment
swaroop_d
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Avatar of swaroop_d
swaroop_d

ASKER

second link is helpful...but doesnt answer my question specifically.

Could anyone please help?
ASKER CERTIFIED SOLUTION
Avatar of sybe
sybe

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of swaroop_d
swaroop_d

ASKER

@ sybe

Well..yeah..that could be done.
But, where should I write that javascript function?
Actually I am getting values in the codebehind of left user control and I want to run the function to be run on the page load of right user control. (my right user control is a bing map and the js function draws a polyline on the map whose end points are generated on the button click of left user control).
Where should i write the js code?
If the ans is to write it in the code behind of left user control itself, then how to pass it to the map user control?

Thanks
SOLUTION
Avatar of sybe
sybe

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of swaroop_d
swaroop_d

ASKER

@ sybe

I have written the javascript function in a seperate .js file.
I am not able to access the public variables on left user control to the right user control.
Please let me know how to do it.
Avatar of sybe
sybe

Are the controls in separate pages?

Anyway, take the thing step by step. The final product is an HTML page with some scripts. If you do not understand that, how can you write code that writes code that produces the desired HTML?
Avatar of swaroop_d
swaroop_d

ASKER

User controls are on same page.
I have written the scripts file myscript.js with the function myfunction(parameter) which should take parameter value from user control 1 (left). parameter is public in user control 1.
This myfunction(parameter) is called on the click event of button on user control 2 (right) like this onclick=myfunction(<%=parameter%>) . But as I see while debugging, parameter is going null which means it is not taking the value of parameter from uercontrol 1 to uercontrol 2.
As the value is passed null (or blank string in my case) i am getting an exception as function myfunction is not getting executed properly.

So, my question is how to pass values of a public variable in user control 1 to user control 2 ?

Thanks. I appreciate your responses.

Thanks again.
Avatar of sybe
sybe

Does the onclick repost the page? That happens often when using webforms.

If so, the click will just reload the page and not do your javascript function.
Avatar of swaroop_d
swaroop_d

ASKER

I guess the page is getting reposted.
Here is the button code.

<input type="button" runat="server" title="show route" onclick="shape('<%=displaypoints %>')" name="show" />

What can be done in that case?
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

You have done so far what my previouos post link recommends:
http://forums.asp.net/p/1422373/3159122.aspx#5._How_do_I_add_CSS_or_JavaScript_from_the_contents_page_dynamically
That is adding your javascript as a separate file.
To call the parameters, please check this link:
http://forums.asp.net/t/295952.aspx
You are trying to execute your js method in the server (onclick="shape('<%=displaypoints %>')" )
In the link, they added as an attrbiute of the button, so that your return value if required is available to your server code as well.
Avatar of swaroop_d
swaroop_d

ASKER

Hello mas_oz2003:

I went through
https://www.experts-exchange.com/questions/23007569/how-to-call-ASP-NET-function-with-an-input-parameter-from-Javascript.html
and i think it is to write the ASP.NET function, but I need to write an javascript function taking value from the ASP.NET function. Is the solution still the same?? I am really confused and saturated.

Now I am getting the final value in displaypoints and i made this string as a global variable to be accessible from every where.
But, from my knowledge I am loosing the values of this variable when i click the button and so it is passed as (null) causing an exception.
How do i preserve the value of my displaypoints variable.
I am really getting confused.
Please help me out.
Thanks.
SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of sybe
sybe

Remove the runat="server" from this button.

from
<input type="button" runat="server" title="show route" onclick="shape('<%=displaypoints %>')" name="show" />

to
<input type="button" title="show route" onclick="shape('<%=displaypoints %>')" name="show" />
Avatar of swaroop_d
swaroop_d

ASKER

@ mas_oz2003:

I am getting the exception which probably says there are some invalid arguments dor the VEPolyline function...I guess the document.GetElementByID is not getting values of the HiddenField1

This is the javascript function i have...am I correct at getElementById?
       function shape() {
           var mydisplaypoints = document.getElementById("<%= HiddenField1.ClientID %>").value;
            var mypoints = [mydisplaypoints];
            var id = 'I70';
            map = new VEMap('myMap');
           
            map.LoadMap();
            var poly = new VEPolyline(id, mypoints);
            map.AddPolyline(poly);
            map.SetMapView(mypoints);
        }

I am giving values to the HiddenField on a button click for time being ..here is the code for it..

protected void Button2_Click(object sender, EventArgs e)
{
    string mypoints = "";
           mypoints = Session["displaypoints"].ToString();
             HiddenField1.Value = mypoints;
  }
}
I am getting values passed to the HiddenField1 by this method. But these points are not been retrived by the javascript shape().
This is the code for the button
  <input type ="button" ID="Button1" onclick="shape()" Text="Button" />  

Please help me out.

Thanks a lot.
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Replace:
var mydisplaypoints = document.getElementById("<%= HiddenField1.ClientID %>").value;
with
var mydisplaypoints = document.getElementById("HiddenField1").value;
Avatar of swaroop_d
swaroop_d

ASKER

@ mas_oz2003:
With
var mydisplaypoints = document.getElementById("HiddenField1").value;
I get an error  Microsoft JScript runtime error: Object required
Avatar of sybe
sybe

As I see it: the way mas_oz2003 is trying to lead you is very complicated. That is because ASP.Net messes with everything. Asp.Net changes many things on the way from your source-code to the HTML which reaches the client. Ids of form-elements (which run at the server) are changed, and you can not simply use the id which you have in your source-code for use in client-side javascript.

Also, buttons which run at the server will always submit the form, thus losing everything you are trying to accomplish in client-side javascript. So no matter if your script would perfectly work in normal situations, it will not work on an ASP.Net page, because it never gets invoked.

Only with quite complicated tricks you can get such a script to run on the client. That is in addition to all the tricks you are doing already.

Keep it simple. ASP.net isn't meant to produce anything that is running on the client. In fact Asp.Net is meant to make you think that a web application is like a desktop application (which of course isn't true at all). Asp.Net uses all kinds of tricks to fake that. Asp.Net does not want you to know that there is a difference between server-side scripts and client-side scripts.

You are in that trap now. You want to use a client-side-script. Asp.Net does everything possible to prevent that.
SOLUTION
Avatar of kumar754
kumar754
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of swaroop_d
swaroop_d

ASKER

Hello mas_oz2003, sybe, kumar754

Finally I was able to solve my problem.
Well, I defined two string globalvariables and passed them as parameters to my javascript function.
I called the function on button click this way : myfunction('<%=GlobalVariables.var1%>','<%=GlobalVariables.var2%>')
And it worked for me. As I used globalvariables class, I did not required the hiddenfield to pass the data.

I still wonder why the hiddenfield that i tried gave me the runtime javascript error of object required when the hiddenfield was on the same usercontrol page.
I tried using registering clientscript by the code behind but it didnt work for me.

Thank you all very much for all your support , suggestions and time.

Thanks a lot.

SWAROOP
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo