GridView与数据库源操作
直接贴源码了(这两段代码完成了,新增,编辑,删除,排序四个功能)
这里我不解释了,自己复制回去运行看一看,数据库SQL2005免费版:ID,MName,url,paixu四个字段
图:
SystemMenu.aspx
<%@ Page Language="C#" CodeFile="SystemMenu.aspx.cs" Inherits="manage_view_SystemMenu" %>
<!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>
<link href="iframe.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="MenuView" runat="server" DataKeyNames="ID"
DataSourceID="MenuDataSource" EmptyDataText="未添加菜单信息"
EnableModelValidation="True" onrowcreated="MenuView_RowCreated"
onload="MenuView_Load">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="true" ReadOnly="true" >
<HeaderStyle Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="MName" HeaderText="菜单显示名称" SortExpression="MName" >
<HeaderStyle Width="25%" />
</asp:BoundField>
<asp:BoundField DataField="url" HeaderText="菜单链接" SortExpression="url" >
<HeaderStyle Width="25%" />
</asp:BoundField>
<asp:BoundField DataField="paixu" HeaderText="菜单排序" SortExpression="paixu"
ReadOnly="True" >
<HeaderStyle Width="10%" />
</asp:BoundField>
<asp:TemplateField HeaderText="菜单操作">
<HeaderStyle Width="20%" />
<ItemTemplate>
<asp:ImageButton ID="PaixiShen" runat="server" OnClick="UpPaixiShen" ImageUrl="images/up.gif" ToolTip="升序" />
<asp:ImageButton ID="PaixiJian" runat="server" OnClick="UpPaixiJian" ImageUrl="images/down.gif" ToolTip="降序" />
<asp:ImageButton ID="DelMenuBT" runat="server" Text="删除" CommandName="Delete" OnClientClick="javascript:return confirm('确定删除吗?')" ImageUrl="images/del.gif" ToolTip="删除"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="修改操作" ShowEditButton="True"
CancelImageUrl="images/cancel.gif"
EditImageUrl="~images/edit.gif"
UpdateImageUrl="images/save.gif" CancelText="<img src='images/cancel.gif' border=0 title='取消'>"
EditText="<img src='images/edit.gif' border=0 title='修改'>" UpdateText="<img src='images/save.gif' border=0 title='保存'>">
<HeaderStyle Width="15%" />
</asp:CommandField>
</Columns>
</asp:GridView>
<table><tr><td width="26%">菜单显示的名称</td><td width="34%"><asp:TextBox ID="addMName" Width="50%" runat="server"></asp:TextBox></td><td width="40%">请输入WEB上显示的菜单名称</td></tr>
<tr><td>菜单对应的链接</td><td><asp:TextBox ID="addurl" runat="server" Width="98%"></asp:TextBox></td><td>请输入菜单名称对应链接的地址</td></tr>
<tr><td>菜单对应的顺序</td><td><asp:TextBox ID="addpaixu" runat="server" Width="20%" ReadOnly="true"></asp:TextBox></td><td>由系统自动计算出排序到最后</td></tr>
<tr><td colspan="3"><asp:Button ID="addSubBT" runat="server" Text="增加" OnClick="InsertShipper" /></td></tr>
<tr><td colspan="3">注:如果需要指向非本站地站请加上链接前加上"http://"如"http://www.bolisoft.net"</td></tr>
</table>
<asp:SqlDataSource ID="MenuDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [MName], [url], [paixu] FROM [menu] ORDER BY [paixu], [ID]"
DeleteCommand="DELETE FROM [menu] WHERE [ID] [email protected]"
UpdateCommand="UPDATE [menu] SET [MName][email protected],[url][email protected] WHERE [ID] [email protected]"
InsertCommand="Insert INTO [menu]([MName],[url],[paixu]) VALUES (@addMName,@addurl,@addpaixu)"
DataSourceMode="DataReader">
<insertparameters>
<asp:formparameter name="addMName" formfield="addMName" />
<asp:formparameter name="addurl" formfield="addurl" />
<asp:formparameter name="addpaixu" formfield="addpaixu" />
</insertparameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<!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>
<link href="iframe.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="MenuView" runat="server" DataKeyNames="ID"
DataSourceID="MenuDataSource" EmptyDataText="未添加菜单信息"
EnableModelValidation="True" onrowcreated="MenuView_RowCreated"
onload="MenuView_Load">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="true" ReadOnly="true" >
<HeaderStyle Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="MName" HeaderText="菜单显示名称" SortExpression="MName" >
<HeaderStyle Width="25%" />
</asp:BoundField>
<asp:BoundField DataField="url" HeaderText="菜单链接" SortExpression="url" >
<HeaderStyle Width="25%" />
</asp:BoundField>
<asp:BoundField DataField="paixu" HeaderText="菜单排序" SortExpression="paixu"
ReadOnly="True" >
<HeaderStyle Width="10%" />
</asp:BoundField>
<asp:TemplateField HeaderText="菜单操作">
<HeaderStyle Width="20%" />
<ItemTemplate>
<asp:ImageButton ID="PaixiShen" runat="server" OnClick="UpPaixiShen" ImageUrl="images/up.gif" ToolTip="升序" />
<asp:ImageButton ID="PaixiJian" runat="server" OnClick="UpPaixiJian" ImageUrl="images/down.gif" ToolTip="降序" />
<asp:ImageButton ID="DelMenuBT" runat="server" Text="删除" CommandName="Delete" OnClientClick="javascript:return confirm('确定删除吗?')" ImageUrl="images/del.gif" ToolTip="删除"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="修改操作" ShowEditButton="True"
CancelImageUrl="images/cancel.gif"
EditImageUrl="~images/edit.gif"
UpdateImageUrl="images/save.gif" CancelText="<img src='images/cancel.gif' border=0 title='取消'>"
EditText="<img src='images/edit.gif' border=0 title='修改'>" UpdateText="<img src='images/save.gif' border=0 title='保存'>">
<HeaderStyle Width="15%" />
</asp:CommandField>
</Columns>
</asp:GridView>
<table><tr><td width="26%">菜单显示的名称</td><td width="34%"><asp:TextBox ID="addMName" Width="50%" runat="server"></asp:TextBox></td><td width="40%">请输入WEB上显示的菜单名称</td></tr>
<tr><td>菜单对应的链接</td><td><asp:TextBox ID="addurl" runat="server" Width="98%"></asp:TextBox></td><td>请输入菜单名称对应链接的地址</td></tr>
<tr><td>菜单对应的顺序</td><td><asp:TextBox ID="addpaixu" runat="server" Width="20%" ReadOnly="true"></asp:TextBox></td><td>由系统自动计算出排序到最后</td></tr>
<tr><td colspan="3"><asp:Button ID="addSubBT" runat="server" Text="增加" OnClick="InsertShipper" /></td></tr>
<tr><td colspan="3">注:如果需要指向非本站地站请加上链接前加上"http://"如"http://www.bolisoft.net"</td></tr>
</table>
<asp:SqlDataSource ID="MenuDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [MName], [url], [paixu] FROM [menu] ORDER BY [paixu], [ID]"
DeleteCommand="DELETE FROM [menu] WHERE [ID] [email protected]"
UpdateCommand="UPDATE [menu] SET [MName][email protected],[url][email protected] WHERE [ID] [email protected]"
InsertCommand="Insert INTO [menu]([MName],[url],[paixu]) VALUES (@addMName,@addurl,@addpaixu)"
DataSourceMode="DataReader">
<insertparameters>
<asp:formparameter name="addMName" formfield="addMName" />
<asp:formparameter name="addurl" formfield="addurl" />
<asp:formparameter name="addpaixu" formfield="addpaixu" />
</insertparameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
SystemMenu.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class manage_view_SystemMenu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.addpaixu.Text = (MenuView.Rows.Count+1).ToString();
}
public void InsertShipper(object source, EventArgs e)
{
MenuDataSource.Insert();
//刷新
Response.Redirect(Request.Url.ToString());
}
protected void MenuView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow){
if (e.Row.RowIndex == 0)
{
((ImageButton)e.Row.Cells[4].Controls[1]).Visible = false;
}
}
}
protected void MenuView_Load(object sender, EventArgs e)
{
((ImageButton)MenuView.Rows[MenuView.Rows.Count - 1].Cells[4].Controls[3]).Visible = false;
}
public void UpPaixiShen(object source, EventArgs e)
{
int rowid = (((GridViewRow)((ImageButton)source).Parent.Parent).RowIndex);
int CurrID = (int)MenuView.DataKeys[rowid].Value;
int UpID = (int)MenuView.DataKeys[rowid-1].Value;
//Response.Write("上一条ID:"+UpID);
//Response.Write("当前ID:" + CurrID);
//Response.Write(((BoundField)MenuView.Rows[rowid].Cells[0].Controls[0]).DataField);
MenuDataSource.UpdateCommand = "UPDATE [menu] SET [paixu]=[paixu]-1 WHERE [ID] =" + CurrID.ToString() + ";UPDATE [menu] SET [paixu]=[paixu]+1 WHERE [ID] ="+ UpID.ToString();
MenuDataSource.Update();
//刷新
Response.Redirect(Request.Url.ToString());
}
public void UpPaixiJian(object source, EventArgs e)
{
int rowid = (((GridViewRow)((ImageButton)source).Parent.Parent).RowIndex);
int CurrID = (int)MenuView.DataKeys[rowid].Value;
int DownID = (int)MenuView.DataKeys[rowid + 1].Value;
MenuDataSource.UpdateCommand = "UPDATE [menu] SET [paixu]=[paixu]+1 WHERE [ID] =" + CurrID.ToString() + ";UPDATE [menu] SET [paixu]=[paixu]-1 WHERE [ID] =" + DownID.ToString();
MenuDataSource.Update();
//刷新
Response.Redirect(Request.Url.ToString());
}
}
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class manage_view_SystemMenu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.addpaixu.Text = (MenuView.Rows.Count+1).ToString();
}
public void InsertShipper(object source, EventArgs e)
{
MenuDataSource.Insert();
//刷新
Response.Redirect(Request.Url.ToString());
}
protected void MenuView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow){
if (e.Row.RowIndex == 0)
{
((ImageButton)e.Row.Cells[4].Controls[1]).Visible = false;
}
}
}
protected void MenuView_Load(object sender, EventArgs e)
{
((ImageButton)MenuView.Rows[MenuView.Rows.Count - 1].Cells[4].Controls[3]).Visible = false;
}
public void UpPaixiShen(object source, EventArgs e)
{
int rowid = (((GridViewRow)((ImageButton)source).Parent.Parent).RowIndex);
int CurrID = (int)MenuView.DataKeys[rowid].Value;
int UpID = (int)MenuView.DataKeys[rowid-1].Value;
//Response.Write("上一条ID:"+UpID);
//Response.Write("当前ID:" + CurrID);
//Response.Write(((BoundField)MenuView.Rows[rowid].Cells[0].Controls[0]).DataField);
MenuDataSource.UpdateCommand = "UPDATE [menu] SET [paixu]=[paixu]-1 WHERE [ID] =" + CurrID.ToString() + ";UPDATE [menu] SET [paixu]=[paixu]+1 WHERE [ID] ="+ UpID.ToString();
MenuDataSource.Update();
//刷新
Response.Redirect(Request.Url.ToString());
}
public void UpPaixiJian(object source, EventArgs e)
{
int rowid = (((GridViewRow)((ImageButton)source).Parent.Parent).RowIndex);
int CurrID = (int)MenuView.DataKeys[rowid].Value;
int DownID = (int)MenuView.DataKeys[rowid + 1].Value;
MenuDataSource.UpdateCommand = "UPDATE [menu] SET [paixu]=[paixu]+1 WHERE [ID] =" + CurrID.ToString() + ";UPDATE [menu] SET [paixu]=[paixu]-1 WHERE [ID] =" + DownID.ToString();
MenuDataSource.Update();
//刷新
Response.Redirect(Request.Url.ToString());
}
}
转载于:https://www.cnblogs.com/kyeeliu/archive/2011/06/12/GridView.html