0

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
3
  • 1
    Post your entire page. Also, how are you binding the Label? Is it using a SqlDataSource, ObjectDataSource, or are you binding it by getting a data table from a database using ADO.net. We need a little more detail to help you out. Commented Dec 16, 2011 at 16:35
  • @gsirianni, sorry updated the post Commented Dec 16, 2011 at 16:44
  • "Sorry, I'm binding the data in the following way with a DataTable."...so is that working? Commented Dec 16, 2011 at 17:07

3 Answers 3

1

use the ItemDataBound event.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.