Thursday, April 25, 2013

Bind data to datalist in asp.net

Title:How to bind data to data list in asp.net using c#.net

Description:
In previous post we have seen bind data to grid view,bind xml data to grid view in asp.net.In this post i will show how to bind the data to data list using SQL Data source in asp.net.Here we don't need write the code to get the data for data list
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" Font-Size="10pt" Width="350px" HeaderStyle-BackColor="ActiveBorder"
 AlternatingItemStyle-BackColor="Aqua" DataSourceID="SqlDataSource1">
<HeaderTemplate>
Order Details
</HeaderTemplate>
<ItemTemplate>
OrderID: &nbsp;<asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>' />
OrderName:
<asp:Label ID="OrderNameLabel" runat="server" Text='<%# Eval("OrderName") %>' />
Phone:
<asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phone") %>' />
Address:
<asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
Amount:
<asp:Label ID="AmountLabel" runat="server" Text='<%# Eval("Amount") %>' />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Orders]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
1.Select data source for data list using options
Design mode ->Data list Tasks->choose new data source
2.Select the SQL Data Source->click next
3.Create new connection wizard will open.In this we have make select the new connection button.
4.Select the data base which you want to use
5.click on next then get wizard to configure the data using query.
6.click finish

No comments:

Bel