Skip to main content
 首页 » 编程设计

c#之如何在 jquery 变量中获取业务类属性值

2024年02月13日18lexus

我需要为 java 脚本变量获取业务类属性值。我正在使用该值进行一些操作。在这里,我尝试在 aspx 页面中创建对象和字符串,但无法在 Java 脚本中访问字符串“val”。如何访问它?

Aspx 文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.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"> 
<% WebApplication1.Business obj = new WebApplication1.Business(); %> 
<% string val = obj.Name; %> 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script> 
</head> 
<body> 
<script type="text/javascript"> 
        $(document).ready(function () { 
        $("#btnSubmit").click(function(){ 
        alert("welcome"); 
            var valProperty = <%= val%>; 
            alert("Val Property is :   "+ valProperty); 
            $("#txtName").val(valProperty); 
           }); 
        }); 
    </script> 
    <form id="form1" runat="server"> 
    <div> 
    <input type="text" id="txtName" /> 
    <br /> 
    <input type="button" id="btnSubmit" value="Get Server Property Value" /> 
    </div> 
    </form> 
</body> 
</html> 

类文件:

 public class Business 
 { 
    public string Name { get; set; } 
    public string Money { get; set; } 
 } 

请您参考如下方法:

我认为你访问它很好,你只是没有设置 obj.Name

<% WebApplication1.Business obj = new WebApplication1.Business(); %> 
// assign something to obj.Name here 
<% string val = obj.Name; %>