asp.net listview databind


You can bind asp.net listview controls in two ways:

1.  The DataSourceID property allows you to bind to Data Source controls such as the ObjectDataSource or SqlDataSource controls:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"></asp:ListView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:MyConn %>"

SelectCommand="SELECT product_id, product_name FROM Products

DeleteCommand="DELETE FROM [Products] WHERE [product_id] = @product_id"

UpdateCommand="UPDATE Products SET product_name = @product_name, userid = @userid WHERE (product_id = @product_id)"

InsertCommand="INSERT INTO Products(product_id, product_name, userid) VALUES (@product_id, @product_name, @userid)">

<UpdateParameters>

<asp:Parameter Name="product_id" Type="Int32" />

<asp:Parameter Name="product_name" Type="String" />

<asp:SessionParameter DefaultValue="DefaultUser" Name="userid” SessionField="SessionUserId" />

</UpdateParameters>

<InsertParameters>

<asp:Parameter Name="product_id" Type="Int32" />

<asp:Parameter Name="product_name" Type="String" />

<asp:SessionParameter DefaultValue="DefaultUser" Name="userid” SessionField="SessionUserId" />

</InsertParameters>

<DeleteParameters><asp:Parameter Name="@product_id" Type="Int32" /></DeleteParameters>

</asp:SqlDataSource>

2.  The DataSource property allows you to bind to objects in the code behind such as ADO.NET datatables, datasets, datareaders and other objects such as collections. You must execute the DataBind method in your code to do the actual binding.  Here is a summary:

a. Create a load method that can be called to load the listview.

b. In the method, declare and load a data table with your info from the database

c. Assign the loaded table into the listview using the Datasource method

d. Execute the listview Databind method.

e. Add bind statements to the listview in the front end to capture and display the fields from the loaded table.  You must use the <%# … %> format and the DataBinder methods. The DataBinder.Eval method is read-only access and the DataBinder.Bind method is for read-write.

Here is an example of what the load method could look like:

private void loadProducts( )

{

DataTable dtProducts = new DataTable();

try

{

dtProducts = Data.GetProducts;

ListView1.DataSource = dtProducts;

ListView1.DataBind();

}

catch (Exception ex)

{

Response.Redirect("~/Pages/Error.aspx");

}

}

Here is an example of a bind statement in the listview’s ItemTemplate for the capture and display of the product_name field. 

You must use the <%# … %> format and the DataBinder methods. The DataBinder.Eval method is read-only access and the DataBinder.Bind method is for read-write.

<ItemTemplate>

<tr>

<td>

<%# DataBinder.Eval(Container.DataItem,"product_name")%>

</td>

</tr>

</ItemTemplate>

Here is another example.  This time we are binding a currency field to a textbox.

<tr>

<td>

<asp:TextBox ID="txtPrice" Text='<%# Eval("trans_def_price","{0:c}") %>' />

</td>

</tr>

No comments:

Post a Comment