Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass a json variable from a CS file to an Javascript function?
    primarykey
    data
    text
    <p>I have a method in C#, which creates a JSON object of the user credentials. Below is the CS file.</p> <pre><code>public string CreateLoginjson(string strErrorType, bool blIsAuthenticated) { StringBuilder sbLoginJson = new StringBuilder(); if (blIsAuthenticated) { sbLoginJson.Append("{LoginSuccess:1"); } else { sbLoginJson.Append("{LoginSuccess:0"); } if (strErrorType != string.Empty) { if (strErrorType.TrimEnd(new char[] { ',' }) == "Token" || strErrorType.TrimEnd(new char[] { ',' }) == "BlankToken") { sbLoginJson.Append(",txtTestTokenNumber1:\"Error\""); sbLoginJson.Append(",txtTestTokenNumber2:\"Error\""); sbLoginJson.Append(",txtTestTokenNumber3:\"Error\""); sbLoginJson.Append(",txtTestTokenNumber4:\"Error\""); } if (strErrorType.TrimEnd(new char[] { ',' }) == "Password") { sbLoginJson.Append(",txtPassword:\"Error\""); } if (strErrorType.TrimEnd(new char[] { ',' }) == "UserName") { sbLoginJson.Append(",UserName:\"Error\""); } string strLoadErrorControlMessage = LoadErrorControl(strErrorType, string.Empty); if (strLoadErrorControlMessage!= string.Empty) { sbLoginJson.Append(",ErrorMessage: '" + strLoadErrorControlMessage + "'"); } } sbLoginJson.Append("}"); var LoginJson = sbLoginJson.ToString(); return LoginJson; } </code></pre> <p>Now, I need to pass the LoginJson to a JS function that checks if incorrect credentials are provided, this function finds the control &amp; adds an attribute to it JS</p> <pre><code>function GetLoginJson(strLoginJson) { if (strLoginJson != '' &amp;&amp; strLoginJson != undefined) { var objLoginJson = strLoginJson; if (objLoginJson.LoginSuccess == "1") { } else if (objLoginJson.LoginSuccess == "0") { if (objLoginJson.txtUserName != '' &amp;&amp; objLoginJson.txtUserName != undefined) { $('#txtUserName').attr("class", objLoginJson.txtUserName); } else { $('#txtUserName').attr("class", "Input"); } if (objLoginJson.txtPassword != '' &amp;&amp; objLoginJson.txtPassword != undefined) { $('#txtPassword').attr("class", objLoginJson.txtPassword); } else { $('#txtPassword').attr("class", "Input"); } if (objLoginJson.txtTestTokenNumber1 != '' &amp;&amp; objLoginJson.txtTestTokenNumber1 != undefined) { $('#txtTestTokenNumber1').attr("class", objLoginJson.txtTestTokenNumber1); } else { $('#txtTestTokenNumber1').attr("class", "Error"); } if (objLoginJson.txtTestTokenNumber2 != '' &amp;&amp; objLoginJson.txtTestTokenNumber2 != undefined) { $('#txtTestTokenNumber2').attr("class", objLoginJson.txtTestTokenNumber2); } else { $('#txtTestTokenNumber2').attr("class", "Error"); } if (objLoginJson.txtTestTokenNumber3 != '' &amp;&amp; objLoginJson.txtTestTokenNumber3 != undefined) { $('#txtTestTokenNumber3').attr("class", objLoginJson.txtTestTokenNumber3); } else { $('#txtTestTokenNumber3').attr("class", "Error"); } if (objLoginJson.txtTestTokenNumber4 != '' &amp;&amp; objLoginJson.txtTestTokenNumber4 != undefined) { $('#txtTestTokenNumber4').attr("class", objLoginJson.txtTestTokenNumber4); } else { $('#txtTestTokenNumber4').attr("class", "Error"); } $('#ErrorControl').html(''); } } } </code></pre> <p>I want to pass the JSON variable from the CS to this jQuery statement `$('#ErrorControl').html('');'</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload