<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Docmgr_test" %>
<!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>GridView添加、编辑、删除测试</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!----列表模块---->
<asp:GridView ID="GridView_Plans" runat="server" AutoGenerateColumns="False" DataKeyNames="RoutePlanID" DataSourceID="SqlDataSource_Plans" Width="100%" OnRowCommand="GridView_Plans_RowCommand" OnRowDataBound="GridView_Plans_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="序号" InsertVisible="False" >
<ItemStyle Width="5%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" Width="5%"/>
<ItemTemplate >
<asp:Label ID="LblXH" runat="server" Width="5%"
Text='<%#this.GridView_Plans.PageIndex * this.GridView_Plans.PageSize +this.GridView_Plans.Rows.Count + 1%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RouteDesignID" HeaderText="所属线路" SortExpression="RouteDesignID" >
<ItemStyle Width="8%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" Width="8%"/>
</asp:BoundField>
<asp:BoundField DataField="RouteNo" HeaderText="行程编号" SortExpression="RouteNo" >
<ItemStyle Width="8%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" Width="8%"/>
</asp:BoundField>
<asp:BoundField DataField="RoutePlan" HeaderText="行程安排" SortExpression="RoutePlan" >
<ItemStyle Width="45%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" Width="45%"/>
</asp:BoundField>
<asp:BoundField DataField="RouteMeal" HeaderText="用餐" SortExpression="RouteMeal" >
<ItemStyle Width="6%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" Width="6%"/>
</asp:BoundField>
<asp:BoundField DataField="RouteAcc" HeaderText="住宿" SortExpression="RouteAcc" >
<ItemStyle Width="6%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" Width="6%"/>
</asp:BoundField>
<asp:BoundField DataField="Remark" HeaderText="备注" SortExpression="Remark" >
<ItemStyle Width="12%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" Width="12%"/>
</asp:BoundField>
<asp:TemplateField HeaderText="操作" >
<HeaderTemplate>
操作
<asp:LinkButton ID="LBtnAdd" runat="server" style="width:25px;" Text="添加" CommandName="BtnAdd"></asp:LinkButton>
</HeaderTemplate>
<ItemStyle Width="10%" VerticalAlign="Middle" HorizontalAlign="Center"/>
<ItemTemplate>
<asp:LinkButton ID="LBtnEdit" runat="server" style="width:25px;" CommandName="BtnEdit" Text="编辑" ></asp:LinkButton>
<asp:LinkButton ID="LBtnDele" runat="server" style="width:25px;" CommandName="Delete" Text="删除" OnClientClick="return confirm('真的要删除吗?')" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
第
<asp:Label ID="LabelCurrentPage" runat="server"
Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
页 共
<asp:Label ID="LabelPageCount" runat="server"
Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页
</PagerTemplate>
</asp:GridView>
<asp:FormView ID="FormView_Plans" Visible="false" runat="server" DataKeyNames="RoutePlanID" DataSourceID="SqlDataSource_Plans" Width="100%" OnItemCommand="FormView_Plans_ItemCommand">
<EditItemTemplate>
<table width="90%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="10%">
<asp:TextBox ID="TextBox7" runat="server" Width="90%" Text='<%# Bind("RoutePlanID") %>' visible = "false"></asp:TextBox>
线路编号:</td>
<td colspan="5">
<asp:TextBox ID="TextBox1" runat="server" Width="99%" Text='<%# Bind("RouteDesignID") %>'></asp:TextBox></td>
</tr>
<tr>
<td width="10%">行程天数:</td>
<td width="25%">
<asp:TextBox ID="TextBox2" runat="server" Width="99%" Text='<%# Bind("RouteNo") %>'></asp:TextBox></td>
<td width="10%">用餐:</td>
<td width="25%">
<asp:TextBox ID="TextBox3" runat="server" Width="99%" Text='<%# Bind("RouteMeal") %>'></asp:TextBox></td>
<td width="10%">住宿:</td>
<td width="20%">
<asp:TextBox ID="TextBox4" runat="server" Width="99%" Text='<%# Bind("RouteAcc") %>'></asp:TextBox></td>
</tr>
<tr>
<td width="10%">线路安排:</td>
<td colspan="5">
<asp:TextBox ID="TextBox5" runat="server" Width="99%" Height="50px" TextMode="MultiLine" Text='<%# Bind("RoutePlan") %>'></asp:TextBox></td>
</tr>
<tr>
<td width="10%">备注:</td>
<td colspan="5">
<asp:TextBox ID="TextBox6" runat="server" Width="99%" Text='<%# Bind("Remark") %>'></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="width: 100%" colspan="6">
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" >
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消">
</asp:LinkButton>
</td>
</tr>
</table>
</EditItemTemplate>
<InsertItemTemplate>
<table width="90%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="10%">
线路编号:</td>
<td colspan="5">
<asp:TextBox ID="TextBox1" runat="server" Width="99%" Text='<%# Bind("RouteDesignID") %>'></asp:TextBox></td>
</tr>
<tr>
<td width="10%">行程天数:</td>
<td width="25%">
<asp:TextBox ID="TextBox2" runat="server" Width="99%" Text='<%# Bind("RouteNo") %>'></asp:TextBox></td>
<td width="10%">用餐:</td>
<td width="25%">
<asp:TextBox ID="TextBox3" runat="server" Width="99%" Text='<%# Bind("RouteMeal") %>'></asp:TextBox></td>
<td width="10%">住宿:</td>
<td width="20%">
<asp:TextBox ID="TextBox4" runat="server" Width="99%" Text='<%# Bind("RouteAcc") %>'></asp:TextBox></td>
</tr>
<tr>
<td width="10%">线路安排:</td>
<td colspan="5">
<asp:TextBox ID="TextBox5" runat="server" Width="99%" Height="50px" TextMode="MultiLine" Text='<%# Bind("RoutePlan") %>'></asp:TextBox></td>
</tr>
<tr>
<td width="10%">备注:</td>
<td colspan="5">
<asp:TextBox ID="TextBox6" runat="server" Width="99%" Text='<%# Bind("Remark") %>'></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="width: 100%" colspan="6">
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="插入" >
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消">
</asp:LinkButton>
</td>
</tr>
</table>
</InsertItemTemplate>
<PagerSettings Visible="False" />
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource_Plans" runat="server"
ConnectionString="<%$ ConnectionStrings:TravelConnectionString %>"
DeleteCommand="DELETE FROM LY_Routeplans WHERE (RoutePlanID = @RoutePlanID)"
InsertCommand="INSERT INTO LY_Routeplans(RouteDesignID, RouteNo, RoutePlan, RouteMeal, RouteAcc, Remark) VALUES (@RouteDesignID, @RouteNo, @RoutePlan, @RouteMeal, @RouteAcc, @Remark)"
SelectCommand="SELECT RoutePlanID, RouteDesignID, RouteNo, RoutePlan, RouteMeal, RouteAcc, Remark FROM LY_Routeplans ORDER BY RoutePlanID"
UpdateCommand="UPDATE LY_Routeplans SET RouteDesignID = @RouteDesignID, RouteNo = @RouteNo, RoutePlan = @RoutePlan, RouteMeal = @RouteMeal, RouteAcc = @RouteAcc, Remark = @Remark WHERE (RoutePlanID = @RoutePlanID)" OnInserting="SqlDataSource_Plans_Inserting"
>
<DeleteParameters>
<asp:Parameter Name="RoutePlanID" Type="int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="RouteDesignID" Type="int32" />
<asp:Parameter Name="RouteNo" Type="int32" />
<asp:Parameter Name="RoutePlan" Type="string" />
<asp:Parameter Name="RouteMeal" Type="string" />
<asp:Parameter Name="RouteAcc" Type="string" />
<asp:Parameter Name="Remark" Type="string" />
<asp:Parameter Name="RoutePlanID" Type="int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="RouteDesignID" Type="int32" />
<asp:Parameter Name="RouteNo" Type="int32" />
<asp:Parameter Name="RoutePlan" Type="string" />
<asp:Parameter Name="RouteMeal" Type="string" />
<asp:Parameter Name="RouteAcc" Type="string" />
<asp:Parameter Name="Remark" Type="string" />
</InsertParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
2、后台代码
using System;
using System.Data;
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 Docmgr_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 更改颜色和设置编辑状态的颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView_Plans_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != -1)
{
//变色
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor= '#C7E0F9' ");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor= '#ffffff' ");
((LinkButton)e.Row.FindControl("LBtnEdit")).CommandArgument = e.Row.RowIndex.ToString();
}
}
/// <summary>
/// 更改编辑状态和插入状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView_Plans_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intRow;
switch (e.CommandName)
{
case "BtnEdit"://编辑
intRow = Convert.ToInt16(e.CommandArgument);
FormView_Plans.Visible = true;
FormView_Plans.ChangeMode(FormViewMode.Edit);
FormView_Plans.PageIndex = (GridView_Plans.PageIndex) * GridView_Plans.PageSize + intRow;
HiddButt(GridView_Plans);
break;
case "BtnAdd":
FormView_Plans.Visible = true;
FormView_Plans.ChangeMode(FormViewMode.Insert); //FormView 切换为编辑模式
HiddButt(GridView_Plans);
//设置选择到的线路编号
break;
default:
break;
}
}
/// <summary>
/// 取消后显示所有的记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void FormView_Plans_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (CheckPlanModle() || e.CommandName == "Cancel")
{
FormView_Plans.Visible = false;
DispButt(GridView_Plans);
}
}
/// <summary>
/// 插入的时候判断当前记录是否存在
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void SqlDataSource_Plans_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
if (!CheckPlanModle())
{
Travel.ShowMsg(Page, "该记录已经存在,请确认!");
e.Cancel = true;
this.SqlDataSource_Plans.SelectCommand = "SELECT RoutePlanID, RouteDesignID, RouteNo, RoutePlan, RouteMeal, RouteAcc, Remark FROM LY_Routeplans ORDER BY RoutePlanID";
this.GridView_Plans.DataSourceID = "SqlDataSource_Plans";
}
}
/// <summary>
/// 隐藏
/// </summary>
/// <param name="gv"></param>
protected void HiddButt(GridView gv)
{
for (int i = 0; i < gv.Rows.Count; i++)
{
GridViewRow gvr = gv.Rows[i];
if (gvr.RowType == DataControlRowType.DataRow && gvr.RowIndex != -1)
{
((LinkButton)gvr.FindControl("LBtnEdit")).Visible = false;
((LinkButton)gvr.FindControl("LBtnDele")).Visible = false;
}
}
}
/// <summary>
/// 显示
/// </summary>
/// <param name="gv"></param>
protected void DispButt(GridView gv)
{
for (int i = 0; i < gv.Rows.Count; i++)
{
GridViewRow gvr = gv.Rows[i];
if (gvr.RowType == DataControlRowType.DataRow && gvr.RowIndex != -1)
{
if (gvr.RowIndex == 0 && gvr.Cells[0].Text.Trim() == " ")
{
((LinkButton)gvr.FindControl("LBtnEdit")).Visible = false;
((LinkButton)gvr.FindControl("LBtnDele")).Visible = false;
}
else
{
((LinkButton)gvr.FindControl("LBtnEdit")).Visible = true;
((LinkButton)gvr.FindControl("LBtnDele")).Visible = true;
}
}
}
}
/// <summary>
/// 监测模型
/// </summary>
/// <returns></returns>
protected bool CheckPlanModle()
{
string strsql;
TextBox tb_plan = (TextBox)this.FormView_Plans.FindControl("TextBox5");
string str_plan = tb_plan.Text;
if (str_plan.Trim() == "")
{
return false;
}
else
{
strsql = "SELECT * FROM LY_Routeplans WHERE RoutePlan='" + str_plan.Trim() + "'";
DataTable dt = DbHelperSQL.QueryTable(strsql);
if (dt.Rows.Count == 0)
{
return true;
}
else
{
return false;
}
}
}
}