Client-side server-side access and vice versa Asp.Net and javascript

I have a requirement to add server variables on the client side and vice versa. Because I need to set the value on the client side using javascript and access the same in the code behind the page.

I need to use C # .Net and javascript.

Any directions please

+6
source share
5 answers

You can simply write variables in javascript using code blocks ( <%%>):

var mJSVariable = <%:myServerSideVariable%>;

To do the opposite, the easiest way is to write the JS value in the hidden form field on the server side and pick it up on the server side:

<input type="hidden" id="myHiddenId" runat="server" />

// Javascript
var myHidden = document.getElementById("<%:myHiddenId.ClientId%>");
myHidden.value = myJSVariable;

// Code behind
var myJSVariableValue = myHiddenId.Value;
+24
source

.cs , public int myServerSideINT, .aspx,

   <script type="text/javascript">
   function getServerSideVAR()
   {
        var serverVAR=<%= this.myServerSideINT %>;
   }
   </script>

, .

+3

, ASP.NET HiddenField.

JS ( JQuery):
$("input[Name$='_IDofField']").val(<newvalue>);
ASP.NET IDofField.Value.

+1

JavaScript . , , System.Web.UI.ICallbackEventHandler.

, , .

aspx :

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Client Calback Example</title>
    <script type="text/ecmascript">
    function LookUpStock()
    {
        var lb=document.getElementById("tbxPassword");
        var product=lb.value;
        CallServer(product,"");
    }

    function ReceiveServerData(rValue){
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="password" id="tbxPassword" />
            <input type="Button" onclick="LookUpStock">Submit</button>
        </div>
    </form>
</body>

" " (CS):

public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
    String cbReference = Page.ClientScript.GetCallbackEventReference
    (this,"arg", "ReceiveServerData", "context");
    String callbackScript;
    callbackScript = "function CallServer(arg, context)" +
    "{ " + cbReference + ";}";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    "CallServer", callbackScript, true);
}
public void RaiseCallbackEvent(String eventArgument)
{
    if(eventArgument == null)
    {
        returnValue = "-1";
    }
    else
    {
        returnValue = eventArgument;
    }
}
public String GetCallbackResult()
{
    return returnValue;
}
}

product JavaScript returnValue.

+1

I had a situation where HiddenField was not useful for transferring values ​​from javascript to APS.net on servers. My script read the value and then called the server-side code, but the value in the hidden field has not yet been updated in the ASP.net hidden field control. The only solution I found was to write the value in a cookie with javascript and then read it on the ASP.net page.

in javascript:

function setCookie(cname, cvalue, exdays, path) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    if (path != undefined)
    expires += "; path=" + path;
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

setCookie("currentDbCompras", _currentDb);

and server side

Request.Cookies("currentDbCompras").Value

I hope this helps !!!

0
source

All Articles