<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList1.aspx.cs" Inherits="DUMMY_DataList1" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:250px;">
<legend>Bind DataList example in asp.net</legend>
<asp:DataList ID="dlEmployee" runat="server" CellPadding="4" ForeColor="#333333" RepeatColumns="2">
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<ItemTemplate>
<table style="width:220px;" cellspacing="2" cellpadding="2">
<tr>
<td><b>Title:</b> <%#DataBinder.Eval(Container.DataItem, "Title")%></td>
</tr>
<tr>
<td><b>Message:</b> <%#DataBinder.Eval(Container.DataItem, "Message")%></td>
</tr>
<tr>
<%--<td><b>Salary:</b> <%#DataBinder.Eval(Container.DataItem, "SALARY")%></td>--%>
</tr>
</table>
</ItemTemplate>
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</fieldset>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class DUMMY_DataList1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDataList();
}
}
protected void BindDataList()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
try
{
SqlDataAdapter adp = new SqlDataAdapter("select * from Announcement", con);
adp.Fill(dt);
dlEmployee.DataSource = dt;
dlEmployee.DataBind();
}
catch (Exception ex)
{
Response.Write("Error occured: " + ex.Message.ToString());
}
finally
{
dt.Clear();
dt.Dispose();
con.Close();
}
}
}

0 comments:
Post a Comment