简体   繁体   English

如何在ASPX文件中使用C#对象

[英]How to use c# object in aspx files

I need to use object in aspx files so I have written code like this 我需要在aspx文件中使用对象,所以我编写了这样的代码

<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. 在上面的代码ldr是我在后端声明的对象。

This is my aspx.cs code: 这是我的aspx.cs代码:

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. 请让我知道我的错误在哪里或如何在aspx文件中使用对象。

You are using two different languages - C# and Javascript, which have entirely separate execution contexts. 您正在使用两种不同的语言-C#和Javascript,它们具有完全独立的执行上下文。 The C# is executed on the server before the page loads. 在页面加载之前,在服务器上执行了C#。 It generates some HTML (and potentially Javascript) which is then sent to the browser. 它生成一些HTML(可能还有Javascript),然后将其发送到浏览器。 The browser displays the HTML and also executes any javascript present on the page when necessary. 浏览器将显示HTML,并在必要时执行页面上存在的所有JavaScript。 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. 似乎在您的情况下,您有一个C#变量,并且想要在javascript上下文中使用该变量值,因此必须以一种javascript可以使用它的方式将变量的呈现到输出中。 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. 隐藏字段将是一个合适的位置(脚本然后可以读取隐藏字段的值),或者甚至可以将其直接注入到服务器生成的JavaScript中。 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. 第三种选择是拥有一个通过ajax调用的web方法,它将从服务器将变量的值按需返回给浏览器。

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: 所以您可以像这样更改您的JavaScript,我认为:

<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. 综上所述,Cine的答案是更简单,更干净的实现。 I'm just demonstrating the principle. 我只是在说明原理。

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. 这样就不再需要Javascript。 All you need to do is mark lblrejectionmsg as runAt=server 您需要做的只是将lblrejectionmsg标记为runAt=server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM