C# 从 ASP.NET 中的标记访问我的类背后代码的属性的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1089239/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:58:28  来源:igfitidea点击:

Best way to access properties of my code behind class from the markup in ASP.NET

c#asp.netscope

提问by marcgg

This might be a really dumb question but I'm learning .NET, so I'm quite clueless...

这可能是一个非常愚蠢的问题,但我正在学习 .NET,所以我一无所知......

Let's say I have two files default.aspx and the associated default.aspx.cs.

假设我有两个文件 default.aspx 和关联的 default.aspx.cs。

default.aspx.cs:

默认.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    var myObject = new MyObject();
}

Is there a way that in the default.aspx I could do something like:

有没有办法在 default.aspx 中我可以执行以下操作:

<%= myObject.SomePropertyOfThisObject%>

... or something similar, without having to use databinders or something complicated like this? And if there's no way around binding the data, what would be the best way to do it?

...或类似的东西,而不必使用数据绑定器或像这样复杂的东西?如果无法绑定数据,那么最好的方法是什么?

采纳答案by John Saunders

You can, but you'll have to do it a bit differently. In your default.aspx.cs, add a member:

你可以,但你必须以不同的方式去做。在您的 default.aspx.cs 中,添加一个成员:

protected MyObject _myObject;

Then, in Page_Load:

然后,在 Page_Load 中:

protected void Page_Load(object sender, EventArgs e)
{
         _myObject = new MyObject();
}

Then, in default.aspx, you can do:

然后,在 default.aspx 中,您可以执行以下操作:

<%= _myObject.SomePropertyOfThisObject %>

Of course, this assumes that class MyObject has a property named Awesome. You didn't mean the System.Object class, did you, since it doesn't have a property named Awesome.

当然,这假设类 MyObject 有一个名为 Awesome 的属性。您不是指 System.Object 类,是吗,因为它没有名为 Awesome 的属性。



Since your question was asking about the bestway, I'll go further. The way I showed is not the best. The best is more often to use a data binding expression. If you don't like those, then you can set things in the codebehind:

由于您的问题是询问最佳方式,因此我会走得更远。我展示的方式并不是最好的。最好是经常使用数据绑定表达式。如果你不喜欢这些,那么你可以在代码隐藏中设置:

protected void Page_Load(object sender, EventArgs e)
{
         _myObject = new MyObject();
        //
        _myLabel.Text = _myObject.SomePropertyOfThisObject;
}

Assuming:

假设:

<asp:Label runat="server" id="_myLabel" />

回答by marcgg

Doesn't it have to be public to be accessible in that scope?

在那个范围内可以访问它不是必须是公开的吗?

..

..

public var myObject = null;
protected void Page_Load(object sender, EventArgs e)
{
  myObject = new Object();
}

回答by LBushkin

The <%= ... %> is ASPX short-hand for Response.Write( .... ).

<%= ... %> 是 ASPX 的简写Response.Write( .... )

If you change myObject to be strongly types (rather than just types as Object) you can certain use the line:

如果将 myObject 更改为强类型(而不仅仅是类型为 Object),则可以使用以下行:

<%= myObject.Awesome %>

to emit a value.

发出一个值。

Be aware, however, that the <%= syntax has limitations - specifically:

但是请注意, <%= 语法具有局限性 - 特别是:

  • it cannot be used within HTML attribute as a value
  • it cannot emit server-side elements
  • it cannot be used within ASP UpdatePanels - you will get a exception when you perform a partial postback
  • 它不能在 HTML 属性中用作值
  • 它不能发出服务器端元素
  • 它不能在 ASP UpdatePanels 中使用 - 当你执行部分回发时你会得到一个异常

You are probably better off creating a Label control on you page, and programmatically setting the Textproperty to the value. This also gives you more control over how value-to-string conversions are performed. And will work correctly with update panel controls.

您最好在页面上创建一个 Label 控件,并以编程方式将该Text属性设置为该值。这也使您可以更好地控制如何执行值到字符串的转换。并且将与更新面板控件一起正常工作。

回答by womp

You can do any coding that you'd like to do directly in the ASPX file, rather than using codebehind. So, to accomplish what I think you want to do, you would have...

您可以直接在 ASPX 文件中执行任何您想执行的编码,而不是使用代码隐藏。因此,要完成我认为您想做的事情,您将...

<some html>

<% var MyObject = new MyObject();
   Response.Write(myObj.Awesome()); %>

<some html>

However, this is really not recommended. Codebehind is the suggested "best practice" way of doing things, because this separates your code from your markup, which is fundamental in any good architecture. I would recommend using something like what John Saunders posted in order to avoid databinding, but you should really consider manipulating your controls in the codebehind using the lifecycle events rather than outputting object properties directly to your HTML. For example, if you were trying to output some text, then do something like

但是,这真的不推荐。Codebehind 是建议的“最佳实践”做事方式,因为这将您的代码与您的标记分开,这是任何良好架构的基础。我建议使用类似 John Saunders 发布的内容以避免数据绑定,但您确实应该考虑使用生命周期事件在代码隐藏中操作控件,而不是将对象属性直接输出到 HTML。例如,如果您尝试输出一些文本,请执行以下操作

var literal = new LiteralControl(myObject.Awesome());
myPanel.Controls.Add(literal);

回答by Sher Abbas

You can declare variable as protected in code behind (.cs) and then call that on aspx page to retrieve all data.

您可以在代码隐藏 (.cs) 中将变量声明为受保护,然后在 aspx 页面上调用它以检索所有数据。

e.g.

例如

protected IEnumerable<Class> _mydata;

internal void MyDefaultData()
{
mydata = doallthework;
}

on aspx page you can do like following

在 aspx 页面上,您可以执行以下操作

<%=_mydata.FirstOrDefault().FirstName%>

<%=_mydata.FirstOrDefault().FirstName%>

Make sure method is called on load.

确保在加载时调用方法。