madhuri
madhuri

Reputation:

Accessing C# variable in javascript

I'm going to design a website for DMS (educational domain) in C#.NET, which I am new to.

In one of my ASPX pages, I want to disable a menu, which is in JavaScript, according to accessright.

The accessright stored in database table login as one attribute in SQL server, and I want to retrieve that accessright to one C# variable and want to access that variable in JavaScript.

If there is another possible approach please tell.

Upvotes: 4

Views: 27792

Answers (5)

Elham Es
Elham Es

Reputation: 21

I found this during my problem with passing server silde variable to a Javascript variable. The best way to do this is using eval() javascript method:

function GetMyValue(){      
    eval("var someVar = " + parseInt<%=this.MyInteger%>"));
    //Do something     
} 

Use it and enjoy it. ;)

Upvotes: 2

plinth
plinth

Reputation: 49179

In our code base we implemented a technology called RemoteInvoke, whereby javascript can call methods on the server without a postback. Effectively, it is call-by-name with dynamic binding on the server side.

The name of the method and its javascript arguments get bundled up and passed to the server, the server unrolls them and finds the best matching method to call. The rules are that you can only call methods with the RemoteInvokable attribute and the allowable data types are numbers, strings and booleans. The code that handles the call delegation will match on signature doing approximate matching where possible (ie, if you pass a floating point number as an arg, it will favor a method with that name and signature, but if there is none, it will find one with a matching integral argument.

Accessing a server side variable can be implemented through RemoteInvoke.

Upvotes: 0

Binoj Antony
Binoj Antony

Reputation: 16186

Well there is one shortcut we used to use during the ASP days, this still works in ASP.NET
In the page codebehind declare a public property say public int MyInteger
In the aspx put this

<script>
    function GetMyValue()
    {
     var someVar = <%=this.MyInteger%>;
     //Do something
    }
</script>

Upvotes: 10

Gustavo
Gustavo

Reputation: 359

This is a sample code I quickly googled. http://www.eggheadcafe.com/community/aspnet/7/63763/using-c-variable-in-java.aspx It should point you in the right direction. strvariable is your C# variable.

    <INPUT id="hd" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 144px" type="hidden"  value="<%=strvariable %>">

<script language="javascript">
function Button_Click()
{
    alert(document.getElementById('hd').value); 
}
</script>

Upvotes: 1

Canavar
Canavar

Reputation: 48088

You can not access server side variables in client-side code. Only way is to use an AJAX library or to inject your variables to page as client side script.

Inject your variable's value to page like that :

string script = string.Format("var myVariable = '{0}';", vaiable.ToString());
if (!ClientScript.IsClientScriptBlockRegistered("myScript"))
{
    ClientScript.RegisterClientScriptBlock(typeof(_Default), "myScript", script, true);
}

and in your javascript functions use myVariable as your server side variable.

But I thing you should start with Client Side or Server Side concept.

Upvotes: 0

Related Questions