C# 从后面的代码中获取变量值并在aspx页面控件中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8883262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get variable value from code behind and use in aspx page control
提问by Eric Herlitz
I got a web user control where I have controls that needs to be fed with some data from variables or properties from the underlying page.
我有一个 Web 用户控件,其中我的控件需要从底层页面的变量或属性中获取一些数据。
<%@ Control Language="C#" AutoEventWireup="False" CodeFile="Header.ascx.cs" Inherits="Site.UserControls.Base.Header" %>
<asp:Literal runat="server" Text='<%# Testing %>' id="ltrTesting" />
Codebehind
代码隐藏
namespace Site.UserControls.Base
{
public partial class Header : UserControlBase
{
public string Testing = "hello world!";
protected void Page_Load(object sender, EventArgs e)
{
//this.DataBind(); // Does not work
//PageBase.DataBind(); // Does not work
//base.DataBind(); // Does not work
//Page.DataBind(); // Does not work
}
}
}
I did read this topic, but it wont solve my problem, I assume it's because this is a user control and not a page. I want to get property value from code behind
我确实读过这个主题,但它不能解决我的问题,我认为这是因为这是一个用户控件而不是一个页面。我想从后面的代码中获取属性值
采纳答案by Eric Herlitz
Solved this, solution below
解决了这个,下面的解决方案
Since I used a web user control in this case the usual scenarios would not work. But by putting a databind in the page that controls the user control, or any materpage in the chain above the web user control the code started to work
由于我在这种情况下使用了 Web 用户控件,因此通常的情况将不起作用。但是通过在控制用户控件的页面中放置一个数据绑定,或者在 web 用户控件上方的链中的任何 materpage 中,代码开始工作
MasterPage codebehind
母版页代码隐藏
public partial class MasterPages_MyTopMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// Databind this to ensure user controls will behave
this.DataBind();
}
}
Ascx file,all suggested solutions below works
Ascx 文件,以下所有建议的解决方案都有效
<%@ Control Language="C#" AutoEventWireup="False" CodeFile="Header.ascx.cs" Inherits="Site.UserControls.Base.Header" %>
1: <asp:Literal runat="server" Text='<%# DataBinder.GetPropertyValue(this, "Testing") %>' />
2: <asp:Literal runat="server" Text='<%# DataBinder.Eval(this, "Testing") %>' />
3: <asp:Literal runat="server" Text='<%# Testing2 %>' />
Codebehind of ascx
ascx 的代码隐藏
namespace Site.UserControls.Base
{
public partial class Header : UserControlBase //UserControl
{
public string Testing { get { return "hello world!"; } }
public string Testing2 = "hello world!";
protected void Page_Load(object sender, EventArgs e)
{ }
}
}
Thanks for the inspiration!
谢谢你的灵感!
回答by Adam Rackis
You can't usually put scriplets in server controls. But there's an easy workaround: use a plain html control:
您通常不能将脚本放在服务器控件中。但是有一个简单的解决方法:使用普通的 html 控件:
<span id="ltrTesting"><%= this.Testing %></span>
回答by CD Jorgensen
try making Testing be a property rather than a field:
尝试使测试成为一个属性而不是一个字段:
e.g.
例如
public string Testing
{
get { return "Hello World!"; }
}
回答by Adrian Thompson Phillips
Or you could set the Text property of the Literal in the code behind:
或者您可以在后面的代码中设置 Literal 的 Text 属性:
ltrTesting.Text = "Hello World!";

