将C#属性值由使用Javascript [英] Set C# Property Value by Javascript

查看:161
本文介绍了将C#属性值由使用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#属性的类别id ,我想设置它在JavaScript值。

I have a C# Property CategoryID, I want to set it's value in Javascript.

我想设置的值类别id 象下面这样:

I am trying to set the value CategoryID like below:

var sPath = window.location.pathname;
var catId = null;

var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if (sPage == 'xyz.aspx')
{
    <%=CommonUtility.CategoryID=4%>;
}
else if(sPage == 'zxy.aspx')
{
    <%=CommonUtility.CategoryID=5%>;
}

但是,用这种方法我总是得到的值类别id = 5(这是在别的块)。

But by this method I always get the value of CategoryID= 5(which is in else block) .

请建议我怎样才能根据病情的属性值。

Please suggest me how can get the Property value based on condition.

推荐答案

您不能从客户端(JS)设置C#属性。您可以使用Ajax做了一些工作,但你根本无法操纵服务器端的code。

You can't set a C# property from a client-side (js). You may use ajax to do some work, but you simply can't manipulate server-side code.

编辑:

如果你仍然不知道它是如何可能的,你得到一个数值,看到的事实小李的解释。但事实依然存在。你不能。不可能。如果你想知道更详细的解释,请参见asp.net的实际工作原理,它的生命周期等,把它会是这样的简单的方法:

if you still wonder how it's possible you get a value, see Mike's explanation of that fact. But the truth remains. You can't. It's impossible. If you want to know the longer explanation, see how asp.net actually works, it's lifecycle etc. Simple way of putting it would be like this:

一个用户发送到使用他的浏览器的服务器的请求。服务器接收它,创建一个请求的页面和实例化所需的类等等。然后它被解析并发送到客户端为html(当然其他资源,如图像,CSS ...)。实例化页面类不能被访问和客户端后修改,因为它已经被服务器刷新。每个请求创建一个新的实例。有没有反正交互使用C#JS的方式。你能想象它会像什么,如果你可以使用一些JS修改C#在远程服务器上?这是没有意义的。

A user sends a request to the server using his browser. The server receives it, creates a requested page and instantiates needed classes etc. Then it's gets parsed and sent to the client as html (and other resources of course, like images, css...). The instantiated page class CAN'T be accessed and modified afterwards by the client, because it's already flushed by the server. Every request creates a new instance. There's no way of interacting js with c# anyway. Can you imagine what it would be like, if you could use some js to modify C# on a remote server? It doesn't make sense at all.

这篇关于将C#属性值由使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆