我们可以在javascript中访问属性吗? [英] can we access property in javascript

查看:71
本文介绍了我们可以在javascript中访问属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中我定义了一个静态类[在App_code中]



在静态类中定义了一个属性





公共静态类ClassName

{

public static string Lock {get; set;}

}



i已经为物业分配了一些价值

现在我需要在java脚本中获得这个分配的属性值。

解决方案

请做一些修改



1.班级名称和财产名称可以'是相同的。

2.你的属性应该存在于javascript定义的那个页面后面的代码中。



例如:



代码隐藏页面是



 使用系统; 

命名空间 UserInterface
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
LockValue = Sandeep;
}
public static string LockValue { get ; set ; }
}

}





和你的.aspx页面按钮点击调用属性值



 <%@       语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  WebForm1。 aspx.cs   继承  =  UserInterface.WebForm1   %>  

< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > < / title >
< script 类型 = text / javascript >
function 提交()
{
var lock = ' <%= LockValue%>';
alert(锁定);
}
< / 脚本 >
< / head >
< body >
< 表单 id = form1 runat < span class =code-keyword> = server >
< div >
< asp:按钮 ID = btnTest runat = 服务器 文字 = 测试 OnClientClick < span class =code-keyword> = 提交() / >
< / div >
< / form >
< / body >
< / html >


in my project i have defined one static class [in App_code]

in static class defined one property


public static class ClassName
{
public static string Lock{get;set;}
}

i have assign some value to property
now i need to get this assigned property value in java script.
.

解决方案

Please do some changes

1. Class name and property name can't be same.
2. Your property should be exist in code behind page of that page where javascript defined.

For example :

Code behind page is

using System;

namespace UserInterface
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LockValue = "Sandeep";
        }
        public static string LockValue { get; set; }
    }

}



and your .aspx page where on button click calling property value

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UserInterface.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function Submit()
        {
            var lock = '<%= LockValue %>';
            alert(lock);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnTest" runat="server" Text="Test" OnClientClick="Submit()" />
    </div>
    </form>
</body>
</html>


这篇关于我们可以在javascript中访问属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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