List.aspx.cs namespace Forum { /// <summary> /// List 的摘要说明。 /// </summary> public class List :System.Web.UI.Page { public string fbId; public string action; public string cPage; public string Title; public DataRow[] Config; public DataRow[] Board; private void SetModel() { Db List=new Db(); cPage="1"; if(Request.Params["Pa.......... .....................................
{
if(!IsPostBack)
{
BindGrid();
}
}
/// <summary>
/// 这个函数返回关于产品细节的DataSet
/// </summary>
///<returns></returns>
private DataSet GetProductData()
{
///SQLStatement是一个SQL语句(string型的)
string SQLStatement="SELECT Products.ProductID, Products.ProductName, Products.QuantityPerUnit, Products.UnitPrice, "+
"Products.UnitsInStock, Products.UnitsOnOrder, Products.ReorderLevel, Products.Discontinued "+
"FROM Products"; :
///声明 SqlConnection对象:myConnection
SqlConnection myConnection=new SqlConnection(@"server=(local)\NetSDK;”+
”database=NorthWind;uid=sa;pwd=1q2w3e;");
///声明Command对象:myCommand
SqlDataAdapter myCommand = new SqlDataAdapter(SQLStatement,myConnection);
///设置Command命令的类型为Text类型
myCommand.SelectCommand.CommandType=CommandType.Text;
///创建DataSet对象实例
myDataSet = new DataSet();
///把从表Products返回的数据填充myData
myCommand.Fill(myDataSet, "Products");
///最后返回myDataSet对象
return myDataSet;
}
/// <summary>
/// 这个函数只有当用户点击Edit按钮时才会被激活
/// </summary>
/// <paramname="sender"></param>
/// <paramname="E"></param>
protected void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
///找出被选定项目的索引(ItemIndex),并且进一步绑定数据
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
/// <summary>
/// 用户点击Cancel按钮时激活MyDataGrid函数
/// </summary>
/// <paramname="sender"></param>
/// <paramname="E"></param>
protected void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
-----
MyDataGrid_Delete函数代码如下:
/// <summary>
///从DataSet中删除一条记录
/// </summary>
/// <param name="sender"></param>
/// <param name="E"></param>
protected void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E)
{
int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
string SQLStatement="Delete Products WHERE ProductID="+ProductID;
string myConnectionString = "server=localhost;uid=sa;pwd=1q2w3e;database=NorthWind"; SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand (SQLStatement,myConnection);
myCommand.CommandTimeout = 15;
myCommand.CommandType=CommandType.Text; try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch(Exception ee)
{
throw ee;
}
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
-------------------
bool Discon=((CheckBox)E.Item.FindControl("Discontinued")).Checked;
-------------------
/// <summary>
///更新记录
/// </summary>
/// <param name="sender"></param>
/// <param name="E"></param>
protected void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{
int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
string ProductName = ((TextBox)E.Item.Cells[3].Controls[0]).Text;
string QuantityPerUnit=((TextBox)E.Item.Cells[4].Controls[0]).Text;
string UnitPrice = ((TextBox)E.Item.Cells[5].Controls[0]).Text;
Int16 UnitsInStock=Int16.Parse(((TextBox)E.Item.Cells[6].Controls[0]).Text);
Int16 UnitsOnOrder=Int16.Parse(((TextBox)E.Item.Cells[7].Controls[0]).Text);
Int16 ReorderLevel=Int16.Parse(((TextBox)E.Item.Cells[8].Controls[0]).Text);
bool Discon=((CheckBox)E.Item.FindControl("Discontinued")).Checked;
int result; if(!Discon)
{
result=0;
}
else
{
result=1;
}
string SQLStatement="UPDATE Products "+
"SET ProductName='"+ProductName+"', "+
"QuantityPerUnit='"+QuantityPerUnit+"', "+
"UnitPrice ="+UnitPrice.Substring(UnitPrice.IndexOf("¥")+1)+", "+
"UnitsInStock ="+UnitsInStock+", "+
"UnitsOnOrder ="+UnitsOnOrder+", "+
"ReorderLevel ="+ReorderLevel+", "+
"Discontinued ="+result+
" WHERE ProductID ="+ProductID; string myConnectionString = "server=localhost;uid=sa;pwd=1q2w3e;database=Northwind";
SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand(SQLStatement,myConnection);
myCommand.CommandTimeout = 15;
myCommand.CommandType = CommandType.Text; try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch(Exception ee)
{
throw ee ;
} MyDataGrid.EditItemIndex = -1;
BindGrid();
} 接下来的BindGrid()调用私有函数GetProductData取得DataSet对象并绑定到DataGrid控件。
/// <summary>
/// 接受数据库数据并再次绑定
/// </summary>
protected void BindGrid()
{
MyDataGrid.DataSource=GetProductData().Tables["Products"].DefaultView;
MyDataGrid.DataBind();
} /// <summary>
/// 分页操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MyDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
MyDataGrid.CurrentPageIndex=e.NewPageIndex;
BindGrid();
} /// <summary>
/// 分类
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Sort_Grid(Object sender, DataGridSortCommandEventArgs e)
{
DataView dv= new DataView(GetProductData().Tables["Products"]);
dv.Sort= e.SortExpression.ToString();
MyDataGrid.DataSource=dv;
MyDataGrid.DataBind();
}
行 1: <%@ Register TagPrefix="Forum" TagName="Top" Src="_Top.ascx" %>
行 2: <%@ Register TagPrefix="Forum" TagName="Bt" Src="_Bt.ascx" %>
行 3: <%@ Page language="c#" Codebehind="List.aspx.cs" AutoEventWireup="false" Inherits="Forum.List" %>
行 4: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
行 5: <HTML>
List.aspx.cs namespace Forum
{
/// <summary>
/// List 的摘要说明。
/// </summary>
public class List :System.Web.UI.Page
{
public string fbId;
public string action;
public string cPage;
public string Title;
public DataRow[] Config;
public DataRow[] Board;
private void SetModel()
{ Db List=new Db();
cPage="1";
if(Request.Params["Pa..........
.....................................