DataList Control Usages
First of all add templates to the datalist control as:
Datagrid.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGrid.aspx.cs" Inherits="Database_Controls_UnFormatted_DataGrid" %>
<!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 align="center" >
<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
onselectedindexchanged="DataList1_SelectedIndexChanged"
onupdatecommand="DataList1_UpdateCommand">
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderTemplate >
<table style ="border : solid 2px;" align="left"><tr style ="background-color:Gray; " align ="left" ><td><asp:TextBox ID="t1" Text ="Id" runat="server" ForeColor ="Black" ReadOnly ="true" ></asp:TextBox></td><td><asp:TextBox ID="t2" Text ="Name" runat="server" ForeColor ="Black" ReadOnly ="true" ></asp:TextBox></td><td><asp:TextBox ID="t3" Text ="Salary" runat="server" ForeColor ="Black" ReadOnly ="true" ></asp:TextBox></td</tr></table>
</HeaderTemplate>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemTemplate >
<table style ="border : solid 2px;">
<tr style ="border : solid 2px;">
<td><asp:TextBox ID="t1" Text ='<%#Eval("id") %>' runat="server" ReadOnly ="true" ></asp:TextBox></td><td><asp:TextBox ID="t2" Text ='<%#Eval ("name") %>' runat="server" ReadOnly ="true" ></asp:TextBox></td><td><asp:TextBox ID="t3" Text ='<%#Eval("sal") %>' runat="server" ReadOnly ="true" ></asp:TextBox></td><td><asp:Button ID ="select" runat="server" CommandName ="Select" Text="Select"/></td><td><asp:Button ID="edit" runat ="server" CommandName ="Edit" Text ="Edit" /></td><td><asp:Button ID="delete" runat ="server" CommandName ="Delete" Text ="Delete" /></td></tr></table>
</ItemTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditItemTemplate >
<table style ="border : solid 2px;">
<tr style ="border : solid 2px;">
<td><asp:TextBox ID="t1" Text ='<%#Eval("id") %>' runat="server" ReadOnly ="false" ForeColor ="White" BackColor="Green" ></asp:TextBox></td>
<td><asp:TextBox ID="t2" Text ='<%#Eval ("name") %>' runat="server" ReadOnly ="false" ForeColor ="White" BackColor="Green" ></asp:TextBox></td>
<td><asp:TextBox ID="t3" Text ='<%#Eval("sal") %>' runat="server" ReadOnly ="false" ForeColor ="White" BackColor="Green" ></asp:TextBox></td>
<td><asp:Button ID ="update" runat="server" CommandName ="Update" Text="Update"/></td>
<td><asp:Button ID="cancel" runat ="server" CommandName ="Cancel" Text ="Cancel" /></td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
Code ..........
To work with data list .. you have perform action at ItemCommand event... to get particulate command you have to specify CommandName property of the controls.. which provide particular command at click event...
.............
Unformatted contorls use Event Bubbling Concept..
if a parent control have achild contorl then any event of child fire then it fire their event to parent control .. then parent control fire an event... as like in repeater control fire itemCommand Event.
But Datalist improves the functionality of the repeater so it have some another events also...
Datalist.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Database_Controls_UnFormatted_DataGrid : System.Web.UI.Page
{
SqlDataAdapter da;
DataSet ds;
private void fgh()
{
try
{
da = new SqlDataAdapter("select * from emp", "Data Source=.;Initial Catalog=NIR;Integrated Security=True");
ds = new DataSet();
da.Fill(ds);
DataList1 .DataSource = ds;
DataList1 .DataBind();
}
catch (Exception e)
{
Response.Write(e.Message);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if ( !IsPostBack )
{
fgh();
}
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
TextBox t = (TextBox)e.Item.FindControl("t1");
TextBox tt = (TextBox)e.Item.FindControl("t2");
TextBox ttt = (TextBox)e.Item.FindControl("t3");
string s = "update emp set name='" + tt.Text + "',Sal=" + ttt.Text + " where id=" + t.Text;
save(s);
DataList1.EditItemIndex = -1;
fgh();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
fgh();
}
public void save(string strQuerty)
{
try
{
SqlCommand cmd = new SqlCommand(strQuerty);
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=NIR;Integrated Security=True");
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception e)
{
Response.Write(e.Message);
}
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
TextBox t = (TextBox)e.Item.FindControl("t1");
string s = "delete from emp where id=" + t.Text;
save(s);
fgh();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
fgh();
}
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("hello");
}
}
No comments :
Post a Comment