I know how to do this in MVC:
我知道如何在MVC中这样做:
<title> <%= Model.Title %> </title>
I also know how to bind a SQLDataSource or ObjectDataSource to a control on a form or listview.
我也知道如何将SQLDataSource或ObjectDataSource绑定到窗体或列表视图上的控件。
But how do I render a field from my SQLDataSource or ObjectDataSource directly into the HTML?
但是如何将我的SQLDataSource或ObjectDataSource中的字段直接呈现到HTML中?
3 个解决方案
#1
You can declare a property and set its value using your desired field's value.
您可以使用所需字段的值声明属性并设置其值。
Code-behind:
private string myTitle;
protected string MyTitle
{
get { return myTitle; }
set { myTitle = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
MyTitle = "Testing 123!";
}
Markup: <title><%= MyTitle %></title>
标记:
#2
You can use the Page.Title
property to set the title in code behind.
您可以使用Page.Title属性在后面的代码中设置标题。
Once you've run your SelectCommand or equivalent on your data source, just simply assign Page.Title a value from the result set.
在数据源上运行SelectCommand或等效命令后,只需简单地分配Page.Title结果集中的值即可。
Alternately, you can use the aspx page itself, and just inside the html assign a text string, like this:
或者,您可以使用aspx页面本身,并在html内部分配一个文本字符串,如下所示:
<title>
<%= dataSource.Select(...) %>
</title>
#3
In WebForms you still need to use a WebControl that implements DataBinding as the "container" for your fields. For instance, a GridView, Repeater, ListView, FormView or DetailsView. Unfortunately there isn't a WebControl designed specifically for rending just one row or object. So, you have a choice:
在WebForms中,您仍然需要使用实现DataBinding的WebControl作为字段的“容器”。例如,GridView,Repeater,ListView,FormView或DetailsView。遗憾的是,没有一个专门用于仅仅渲染一行或一个对象的WebControl。所以,你有一个选择:
Use a Repeater something like this:
使用这样的Repeater:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="MyDataSource">
<ItemTemplate>
<%# Eval("MyProperty") %>
</ItemTemplate>
</asp:Repeater>
Another alernative is to not use a DataSource. Instead, add properties to your page and then bind your data to these. For example, in your page codebehind:
另一个问题是不使用DataSource。而是向页面添加属性,然后将数据绑定到这些属性。例如,在您的页面代码隐藏中:
public string MyPageProperty
{
get { return _myPageProperty; }
set { _myPageProperty = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
MyPageProperty = "This is some data";
}
You can then do this in your page:
然后,您可以在页面中执行此操作:
<div>The value is: <%= MyPageProperty %></div>
Hope that helps.
希望有所帮助。
#1
You can declare a property and set its value using your desired field's value.
您可以使用所需字段的值声明属性并设置其值。
Code-behind:
private string myTitle;
protected string MyTitle
{
get { return myTitle; }
set { myTitle = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
MyTitle = "Testing 123!";
}
Markup: <title><%= MyTitle %></title>
标记:
#2
You can use the Page.Title
property to set the title in code behind.
您可以使用Page.Title属性在后面的代码中设置标题。
Once you've run your SelectCommand or equivalent on your data source, just simply assign Page.Title a value from the result set.
在数据源上运行SelectCommand或等效命令后,只需简单地分配Page.Title结果集中的值即可。
Alternately, you can use the aspx page itself, and just inside the html assign a text string, like this:
或者,您可以使用aspx页面本身,并在html内部分配一个文本字符串,如下所示:
<title>
<%= dataSource.Select(...) %>
</title>
#3
In WebForms you still need to use a WebControl that implements DataBinding as the "container" for your fields. For instance, a GridView, Repeater, ListView, FormView or DetailsView. Unfortunately there isn't a WebControl designed specifically for rending just one row or object. So, you have a choice:
在WebForms中,您仍然需要使用实现DataBinding的WebControl作为字段的“容器”。例如,GridView,Repeater,ListView,FormView或DetailsView。遗憾的是,没有一个专门用于仅仅渲染一行或一个对象的WebControl。所以,你有一个选择:
Use a Repeater something like this:
使用这样的Repeater:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="MyDataSource">
<ItemTemplate>
<%# Eval("MyProperty") %>
</ItemTemplate>
</asp:Repeater>
Another alernative is to not use a DataSource. Instead, add properties to your page and then bind your data to these. For example, in your page codebehind:
另一个问题是不使用DataSource。而是向页面添加属性,然后将数据绑定到这些属性。例如,在您的页面代码隐藏中:
public string MyPageProperty
{
get { return _myPageProperty; }
set { _myPageProperty = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
MyPageProperty = "This is some data";
}
You can then do this in your page:
然后,您可以在页面中执行此操作:
<div>The value is: <%= MyPageProperty %></div>
Hope that helps.
希望有所帮助。