简体   繁体   English

ASP.NET:可以使用服务器标签将C#或VB.NET表达式嵌入到Javascript函数中吗?

[英]ASP.NET: Can you use server tags to embed C# or VB.NET expressions into a Javascript function?

I have an enum called SiteTypes that contains several values that are all bound to a dropdown list. 我有一个名为SiteTypes的枚举,其中包含几个都绑定到下拉列表的值。 On the client side, I need to check this dropdown to see if the selected value is one of those enum values. 在客户端,我需要检查此下拉列表,以查看所选值是否为这些枚举值之一。 I don't want to hardcode the value of the enum in the script in case it needs to change, so I want to use a server tag to get it directly from the enum itself. 我不想在脚本中对枚举的值进行硬编码以防万一需要更改,因此我想使用服务器标签直接从枚举本身获取它。 Conecptually, I would like to do this: 从概念上讲,我想这样做:

function SiteIdChanged() {
    var x = "<%=SiteTypes.Employee %>";
}

The way I am doing it now is created a protected property in the codebehind that returns that specific enum value and am doing this: 我现在的操作方式是在代码背后创建一个受保护的属性,该属性返回该特定的枚举值,并正在执行此操作:

function SiteIdChanged() {
    var x = "<%=EmployeeSiteTypeValue %>";
}

I don't like that, though, because I have to create a special property on every page that I need to do such a check. 不过,我不喜欢这样,因为我必须在需要进行此类检查的每个页面上创建一个特殊属性。

Is there a way to do what I want here? 有什么办法可以在这里做我想要的吗?

As long as your enum is marked public , you can just go with your first option. 只要您的enum被标记为public ,您就可以选择第一个选项。 There's no need to put a property on every single page you want to retrieve the value from. 无需在要从中检索值的每个页面上放置属性。

That approach is really the simplest solution for writing out server side values in your JavaScript. 该方法实际上是在JavaScript中写出服务器端值的最简单解决方案。

If you don't like your current implementation I would consider using a PageMethod to compare the dropdown selection to the enum value. 如果您不喜欢当前的实现,则可以考虑使用PageMethod将下拉选择项与枚举值进行比较。 This approach will probably be cleaner, as you can do most of the logic server-side. 由于您可以在大多数逻辑服务器端进行操作,因此这种方法可能更简洁。

Here's a tutorial on PageMethods : 这是有关PageMethods的教程:
http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx

Are you getting a "xxx is inaccessible due to its protection level" error when you compile or run the page? 编译或运行页面时,是否收到“由于其保护级别而无法访问xxx”的错误? enums are public by default, classes are not. 默认情况下,枚举是公共​​的,而类不是。 My guess is that you've defined your enum inside your page's class and you aren't explicitly marking it with the 'public' access modifier. 我的猜测是您已经在页面的类中定义了枚举,而没有使用“ public”访问修饰符对其进行明确标记。 Explicitly mark it as public or move it outside of the class and see what happens. 明确将其标记为公开或将其移出课堂,然后看看会发生什么。 If you're planning on using it on lots of pages you should stick the enum definition in in a file in the App_Code folder of your project. 如果打算在许多页面上使用它,则应将枚举定义粘贴在项目的App_Code文件夹中的文件中。

You can use the Enum.IsDefined Method this well tell you if the selected value from the dropdown is actually part of your enum. 您可以使用Enum.IsDefined方法很好地告诉您从下拉列表中选择的值是否实际上是枚举的一部分。

Enum.IsDefined(typeof(MyEnum), myValue)

http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx http://msdn.microsoft.com/zh-CN/library/system.enum.isdefined.aspx

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

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