Backcolor = "white" cellpadding = "3" gridlines = "horizontal" autogeneratecolumns = "false" datakeyfield = "job_id"
PageSize = "5" allowpaging = "true">
selecteditedItemStyle>
alternatingItemStyle>
itemstyle>
headerstyle> footerStyle>
<% # (DataGrid1.pageSize * DataGrid1.currentPageIndex) Container.Index 1%>
Itemtemplate>
asp: templateColumn>
asp: boundcolumn>
asp: boundcolumn>
asp: boundcolumn>
asp: boundcolumn>
asp: editcommandcolumn>
asp: buttoncolumn>
asp: hyperlinkcolumn>
Columns>
pageerstyle>
asp: datagrid> form>
body>
Html>
Postcod Webform1.aspx.cs
code:
Using system;
Using system.collections;
Using system.componentmodel; using system.data;
Using system.drawing;
Using system.Web;
Using system.Web.SessionState;
Using system.Web.ui;
Using system.Web.ui.webcontrols;
Using system.Web.ui.htmlcontrols;
Using system.data.sqlclient;
Namespace Tayesample
{
///
/// WebForm1 summary description.
/// summary>
Public class Webform1: System.Web.ui.page
{
Protected system.web.ui.webcontrols.dataGrid DataGrid1;
Private SqlConnection CN = New SqlConnection ("Server = (local); Database = Pubs; UID = SA; PWD =");
Private Void Page_Load (Object Sender, System.EventArgs E)
{
/ / Place the user code here to initialize the page
IF (! ispostback)
{
Bind ();
}
}
#Region web form designer generated code
Override protected void oninit (Eventargs E)
{
//
// Codegen: This call is necessary for the ASP.NET Web Form Designer.
//
InitializationComponent ();
Base.onit (E);
}
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
This.DataGrid1.pageIndexchanged = new system.web.ui.WebControls.DataGridPageChangeDeventhandler (this.DataGrid1_pageIndexchanged);
This.DataGrid1.cancelcommand = new system.Web.ui.WebControls.DataGridCommandeventHandler (this.DataGrid1_cancelcommand);
This.DataGrid1.editcommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_editCommand);
This.DataGrid1.updateCommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_updateCommand);
This.DataGrid1.deleteCommand = new system.web.ui.webcontrols.DataGridCommandeventHandler (this.DataGrid1_deleteCommand);
This.DataGrid1.itemdatabase.DataGriditeMeventHndler (THISDATAGRID1_ITEMDATABOUND);
This.Load = new system.eventhandler (this.page_load);
#ndregion
//
// data binding
//
Private void bind ()
{
String selstr = "SELECT * from Jobs";
SqldataAdapter Sqlda = New SqldataAdapter (Selstr, CN);
DataSet DS = New DataSet ();
Sqlda.fill (DS);
THIS.DATAGRID1.DATASOURCE = DS;
THIS.DATAGRID1.DATABIND ();
}
//
// Add confirm and onmouseout onmouseover event
//
Private void DataGrid1_itemdatabase (Object Sender, DataGriditeMeventArgs E)
{
IF (E.Item.ItemType == ListItemType.Item || E.Item.ItemType == ListItemType.alternatingItem)
{
LinkButton Delbttn = (LinkButton) E.Item.cells [6] .controls [0];
Delbttn.attributes.add ("onclick", "JavaScript: Return Confirm ('OK Delete" E.Item.cells [0] .Text ");");
E.Item.attributes.add ("onmouseover", "this.style.BackgroundColor = '# f3f3f3'");
IF (E.Item.ItemType == ListItemType.Item)
{
E.Item.attributes.add ("onmouseout", "this.style.BackgroundColor = '# e7e7ff'");
}
IF (E.Item.ItemType == ListItemType.alternatingItem)
{
E.Item.attributes.add ("onmouseout", "this.style.BackgroundColor = '# f7f7f7');
}
}
}
//
// Edit
//
Private void DataGrid1_EditCommand (Object Source, DataGridCommandeventArgs E)
{
This.dataGrid1.edititeMindex = E.Item.itemindex;
THIS.BIND ();
}
//
// cancel
//
Private void DataGrid1_cancelcommand (Object Source, DataGridCommandeventArgs E)
{
THIS.DATAGRID1.EDITITEMINDEX = -1;
THIS.BIND ();
}
//
// Delete
//
Private void DataGrid1_DeleteCommand (Object Source, DataGridCommandeventArgs E)
{
String delstr = "delete from jobs where job_id = @ Job_ID";
Sqlcommand sqlcmd = new SQLCOMMAND (DELSTR, CN); Sqlcmd.Parameters.Add ("@ Job_ID", SqldbType.smallint, 2);
Sqlcmd.parameters ["@ Job_ID"]. value = this.dataGrid1.datakeys [E.Item.ItemIndex];
Try
{
Cn.open ();
Sqlcmd.executenon query ();
Cn.close ();
IF (! page.isstartupscriptregistered ("delasert")))
{
Page.RegisterStartupScript ("delelert", @ "