ASP.NET:如何通过代码隐藏设置 ListView 数据,而不是使用 Text 属性中的 Bind() 函数?

发布于 2024-12-21 22:45:31 字数 521 浏览 4 评论 0原文

如何通过代码隐藏设置 ListView 数据,而不是使用 Text 属性中的 Bind() 函数?

现在我正在执行以下操作,但我想在代码隐藏中检索并设置它。我正在使用 VB...谢谢!

<asp:Label ID="Date" runat="server" Text='<%# Bind("Date") %>'></asp:Label>

编辑:

抱歉,我按照以下方式将数据与数据表绑定。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       If Not IsPostBack Then

            ListView.DataSource = MyDataTable
            ListView.DataBind()

       End If

End Sub

How do I set ListView data through the codebehind instead of using the Bind() function in the Text attribute?

Right now I'm doing the following, but I'd like to have it retrieved and set in the codebehind. I'm using VB... Thanks!

<asp:Label ID="Date" runat="server" Text='<%# Bind("Date") %>'></asp:Label>

Edit:

Sorry, I'm binding the data in the following way with a DataTable.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       If Not IsPostBack Then

            ListView.DataSource = MyDataTable
            ListView.DataBind()

       End If

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛放△進行李 2024-12-28 22:45:31

使用 ItemDataBound 事件。

use the ItemDataBound event.

时光暖心i 2024-12-28 22:45:31

在没有看到您的代码的情况下,我可以告诉您 ListView 有一个 DataSource 属性,您应该能够在加载代码中设置该属性(然后执行 DataBind())。我知道我以前用 GridView 做过这样的事情。

Without seeing your code, I can tell you that a ListView has a DataSource property that you should just be able to set in your load code (and then do a DataBind()). I know I've done that before with a GridView.

晨曦慕雪 2024-12-28 22:45:31

根据您提供的信息,这是我能给您的最好的信息。您可能希望将此代码段放入 ListView 的 PreRender 事件中。

Label lblDate = (Label)ListView.FindControl("Date");

if(dataTable.Rows.Count > 0 && dataTables.Columns.Contains("Date"))
{
    DataRow row = dataTable.Rows[0];
    If(!DBNull.Equals(row["Date"])
    {
        lblDate.Text = row["Date"].ToString();
    }
}

Based on the info you have provided this is the best I can give you. You may want to put this snippet in the PreRender event for your ListView.

Label lblDate = (Label)ListView.FindControl("Date");

if(dataTable.Rows.Count > 0 && dataTables.Columns.Contains("Date"))
{
    DataRow row = dataTable.Rows[0];
    If(!DBNull.Equals(row["Date"])
    {
        lblDate.Text = row["Date"].ToString();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文