Sunday, January 17, 2010

Using DataList Control Template



C#:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
static DataSet ds;
static SqlDataAdapter da;
static SqlCommandBuilder cmdBuilder;
static string currentCno;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bind();
}
}
public void Bind()
{
SqlConnection con = new SqlConnection("Initial Catalog=sangram;Integrated Security=SSPI;Data Source=(local);");
con.Open();
da = new SqlDataAdapter("select * from courses", con);
cmdBuilder = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "courses");
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Select")
{

MessageLabel.Text ="You Have Selected Course In: " + ds.Tables[0].Rows[e.Item.ItemIndex][1].ToString() ;

}
}
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
MessageLabel.Text = "All Items Have Been Created";
}
}

HTML :
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemCreated="Repeater1_ItemCreated">
<HeaderTemplate>
<table>
<tr>
<td><strong>Id</strong></td>
<td><strong>Course Name</strong></td>
<td><strong>Course Duration</strong></td>
<td><strong>Fees</strong></td>
<td><strong>Command</strong></td>
</tr>
</HeaderTemplate>

<FooterTemplate>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</FooterTemplate>

<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno")%> ID="LblCourseId"></asp:Label></td>
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname")%> ID="LblCourseName"></asp:Label></td>
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> ID="LblCourseDuration"></asp:Label></td>
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> ID="LblCourseFees"></asp:Label></td>
<td><asp:Button ID="Btn1" Text="Select" CommandName="Select" runat="server" /> </td>
</tr>
</ItemTemplate>

<AlternatingItemTemplate>
<tr style="background-color:Burlywood;">
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno")%> ID="LblCourseId"></asp:Label></td>
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname")%> ID="LblCourseName"></asp:Label></td>
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> ID="LblCourseDuration"></asp:Label></td>
<td><asp:Label runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> ID="LblCourseFees"></asp:Label></td>
<td><asp:Button ID="Btn1" Text="Select" CommandName="Select" runat="server" /> </td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
<br />
<asp:Label ID="MessageLabel" runat="server"></asp:Label></div>
</form>
</body>
</html>

No comments:

Post a Comment