简体   繁体   English

如何将代码中的变量调用到aspx页面后面

[英]how to call a variable in code behind to aspx page

i know i have seen this but cant recall the correct way of doing it... basically i have a string variable called "string clients" in my .cs file.. but i wasn't to be able to pass it to my aspx page something like 我知道我已经看到了这个但是无法回想起这样做的正确方法...基本上我的.cs文件中有一个名为“string clients”的字符串变量..但是我无法将它传递给我的aspx页面的东西

<%=clients%>  

please correct me, i do not recall or not sure how to do this. 请纠正我,我不记得或不确定如何做到这一点。 (new to c#) and when i googled it.. it was not clear.. or not many of these out there.. searched as (对c#来说是新的)当我用谷歌搜索它时......不清楚......或者其中很多都没有...搜索过

"asp.net c# <%= %> not consistent results.. maybe because i do not know how to call these.. “asp.net c# <%= %>结果不一致..也许是因为我不知道怎么称呼这些......

The field must be declared public for proper visibility from the ASPX markup. 必须将该字段声明为public才能从ASPX标记中获得适当的可见性。 In any case, you could declare a property: 在任何情况下,您都可以声明一个属性:


private string clients;
public string Clients { get { return clients; } }

UPDATE: It can also be declared as protected , as stated in the comments below. 更新:它也可以声明为protected ,如下面的评论中所述。

Then, to call it on the ASPX side: 然后,在ASPX端调用它:

<%=Clients%> <%=客户端%>

Note that this won't work if you place it on a server tag attribute. 请注意,如果将其放在服务器标记属性上,这将不起作用。 For example: 例如:

<asp:Label runat="server" Text="<%=Clients%>" /> <asp:Label runat =“server”Text =“<%= Clients%>”/>

This isn't valid. 这是无效的。 This is: 这是:

<div><%=Clients%></div> <DIV> <(%)=客户端%> </ DIV>

In your code behind file, have a public variable 在你的代码隐藏文件中,有一个公共变量

public partial class _Default : System.Web.UI.Page
{
    public string clients;

    protected void Page_Load(object sender, EventArgs e)
    {
        // your code that at one points sets the variable
        this.clients = "abc";
    }
}

now in your design code, just assign that to something, like: 现在在您的设计代码中,只需将其分配给某些内容,例如:

<div>
    <p><%= clients %></p>
</div>

or even a javascript variable 甚至是一个javascript变量

<script type="text/javascript">

    var clients = '<%= clients %>';

</script>

For 对于

<%=clients%>

to work you need to have a public or protected variable clients in the code-behind. 为了工作,你需要在代码隐藏中拥有一个公共或受保护的变量clients

Here is an article that explains it: http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx 这是一篇解释它的文章: http//msdn.microsoft.com/en-us/library/6c3yckfw.aspx

First you have to make sure the access level of the variable is protected or public. 首先,您必须确保变量的访问级别受到保护或公开。 If the variable or property is private the page won't have access to it. 如果变量或属性是私有的,则页面将无法访问它。

Code Behind 代码背后

protected String Clients { get; set; }

Aspx ASPX

<span><%=Clients %> </span>

在浏览ASPX页面之前,请确保已编译* .cs文件。

You need to declare your clients variable as public, eg 您需要将您的客户变量声明为公共变量,例如

public string clients;

but you should probably do it as a Property, eg 但你应该把它作为一个财产,例如

private string clients;
public string Clients{ get{ return clients; } set {clients = value;} }

And then you can call it in your .aspx page like this: 然后你可以在你的.aspx页面中调用它:

<%=Clients%>

Variables in C# are private by default. 默认情况下,C#中的变量是私有的。 Read more on access modifiers in C# on MSDN and properties in C# on MSDN 详细了解MSDN上C#中的访问修饰符MSDN 上C#中的属性

I would create a property to access the variable, like this: 我会创建一个属性来访问变量,如下所示:

protected string Test
{
    get; set;
}

And in your markup: 在你的标记中:

<%= this.Test %>

The HelloFromCsharp.aspx look like this HelloFromCsharp.aspx看起来像这样

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <p>
       <%= clients%>
    </p>
    </form>
</body>
</html>

And the HelloFromCsharp.aspx.cs HelloFromCsharp.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test
{
    public partial class HelloFromCsharp : System.Web.UI.Page
    {
        public string clients;
        protected void Page_Load(object sender, EventArgs e)
        {
            clients = "Hello From C#";
        }
    }
}

You can access a public/protected property using the data binding expression <%# myproperty %> as given below: 您可以使用数据绑定表达式<%# myproperty %>访问公共/受保护的属性,如下所示:

    <asp:Label ID="Label1" runat="server" Text="<%#CodeBehindVarPublic %>"></asp:Label>

you should call DataBind method, otherwise it can't be evaluated. 你应该调用DataBind方法,否则无法进行评估。

    public partial class WebForm1 : System.Web.UI.Page
    {
     public string CodeBehindVarPublic { get; set; }
      protected void Page_Load(object sender, EventArgs e)
        {
          CodeBehindVarPublic ="xyz";
       //you should call the next line  in case of using <%#CodeBehindVarPublic %>

          DataBind();
        }

} }

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

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