0

I need to use object in aspx files so I have written code like this

<script type="text/javascript">
    var text = '{ "R000092201": "EIN and Name should be same","F2290026":"Your Form is Incomplete, Please add the Credit Vehicle Details (Contact Support for any further queries)" }';

    var obj = JSON.parse(text);

    switch(ldr["rejection"])
    {
        case "R0000-922-01": document.getElementById('lblrejectionmsg').innerHTML = obj.R000092201;

        case "F2290-005-01": document.getElementById('lblrejectionmsg').innerHTML = obj.F2290026;       
    }
</script>

In above code ldr is object I have declared in back end.

This is my aspx.cs code:

DataRow ldr = ldtReview.Rows[0];
if (Convert.ToString(ldr["rejection"]) == "R0000-922-01")
{
   divErrorList.Visible = true;
   lblRejectionReason.Text = Convert.ToString(ldr["rejection"]);
   //ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
   // lblrejectionmsg.Text = Convert.ToString(ldr["rejection_msg"]);
   contSup.Visible = false;
   editbtn.Visible = true;
}
else
{
   divErrorList.Visible = true;
   editbtn.Visible = false;
   contSup.Visible = true;
   lblRejectionReason.Text = Convert.ToString(ldr["rejection"]);
   // lblrejectionmsg.Text = Convert.ToString(ldr["rejection_msg"]);
}

please let me know where my mistake is or how to use object in aspx files.

Cine
  • 4,255
  • 26
  • 46
Vinayaka N
  • 123
  • 3
  • 13

2 Answers2

1

You are using two different languages - C# and Javascript, which have entirely separate execution contexts. The C# is executed on the server before the page loads. It generates some HTML (and potentially Javascript) which is then sent to the browser. The browser displays the HTML and also executes any javascript present on the page when necessary. There is no link between the two. Therefore you can't just reference a variable from one language directly in the other language.

It seems like in your situation you have a C# variable and you want the value of that variable to be used in a javascript context, you have to render the value of the variable into your output in such a way that the javascript can use it. A hidden field would be a suitable place (the script can then read the value of the hidden field), or you can even inject it directly into the JavaScript generated by the server. A third option is to have a webmethod which you call via ajax, which will return the value of the variable from the server to the browser on-demand.

In your case, you're already writing the value of the variable into a label, which will get rendered to the page. So you can change your javascript like this I think:

<script type="text/javascript">
var obj = { "R000092201": "EIN and Name should be same","F2290026":"Your Form is Incomplete, Please add the Credit Vehicle Details (Contact Support for any further queries)" };

switch(document.getElementById("lblrejectionmsg").innerHTML)
{
    case "R0000-922-01": document.getElementById('lblrejectionmsg').innerHTML = obj.R000092201;
      break;

    case "F2290-005-01": document.getElementById('lblrejectionmsg').innerHTML = obj.F2290026;       
    break;
}
</script>

Having said all that, Cine's answer is a simpler, cleaner implementation. I'm just demonstrating the principle.

ADyson
  • 57,178
  • 14
  • 51
  • 63
0

You are mixing server side objects with client side objects.

In this particular case I would solve the problem like this:

DataRow ldr = ldtReview.Rows[0];
var rejection = Convert.ToString(ldr["rejection"]);

contSup.Visible = true;
editbtn.Visible = false;
divErrorList.Visible = true;
lblRejectionReason.Text = rejection;

if (rejection == "R0000-922-01")
{
  lblrejectionmsg.Text = "EIN and Name should be same";
  contSup.Visible = false;
  editbtn.Visible = true;
}
else if (rejection == "F2290-005-01")
   lblrejectionmsg.Text = "Your Form is Incomplete, Please add the Credit Vehicle Details (Contact Support for any further queries)";

Javascript is then no longer necessary. All you need to do is mark lblrejectionmsg as runAt=server

Cine
  • 4,255
  • 26
  • 46
  • Then I suggest you take a look at one of the many questions about serializing your objects to json, e.g. http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 – Cine Sep 21 '16 at 10:50