views:

1424

answers:

4

I'm new in c#.net. I'm going to design a website for DMS(educational domain)in C#.net. So in one of my Aspx page I want to disable 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.

A: 

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.

Canavar
that is simply not true! ever thought of <%= this.VAR %> ??
Andreas Niedermair
Why not true ? I use it everyday, and nothing wrong with this. <%= this.VAR %> way is some kind of spaghetty code. I don't like it.
Canavar
A: 

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>
Gustavo
+5  A: 

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>
Binoj Antony
i like this more than the solution by Scarlet Garden. Why? With your solution you don't need to rebuild if you want to change something!
Andreas Niedermair
A: 

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.

plinth
This looks promising, but is probably more than the poster needs or cares to implement. :\
Mufasa