daveomcd
daveomcd

Reputation: 6565

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

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

Upvotes: 0

Views: 3032

Answers (3)

gsirianni
gsirianni

Reputation: 1364

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();
    }
}

Upvotes: 0

Jason Meckley
Jason Meckley

Reputation: 7591

use the ItemDataBound event.

Upvotes: 1

Aaron
Aaron

Reputation: 57843

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.

Upvotes: 0

Related Questions