项目中有个需求,需要对已经绑定数据并分页的GrideView进行新增一行空白行。用户在点击“新增行”后,在界GrideView界面的最下面增加一行空白行。
要求:1、用户在第几页点击新增行就在当前页面显示新增行;
2、新增行按钮在GrideView外面;
具体代码如下:
前台页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="YPCGJCB.aspx.cs" Inherits="CenterBase.供应链管理.YPCGJCB" %>
<%@ Register Assembly="UserControls" Namespace="UserControls" TagPrefix="cc2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>药品折扣基础表</title>
<link rel="stylesheet" href="../js/Grid/css/demo_page.css" />
<link rel="stylesheet" href="../js/DatePicker/base/jquery-ui.css" />
<script src="../js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="../js/JScript.js" type="text/javascript"></script>
<script src="../js/DatePicker/jquery.ui.core.js" type="text/javascript"></script>
<script src="../js/DatePicker/jquery.ui.datepicker.js" type="text/javascript"></script>
<script src="../js/Ajax.js"></script>
<script src="../js/jquery-1.4.1.js"></script>
<style type="text/css">
.auto-style2 {
height: 25px;
}
.auto-style3 {
width: 156px;
height: 25px;
}
.auto-style4 {
margin-top: 5px;
}
.auto-style5 {
text-align: right;
}
</style>
<script language="javascript" type="text/javascript">
var dgPersonsID = "<%= YPZKXX.ClientID %>";
$(document).ready(function () {
$("#dgPersons tr").eq(1).hide();
});
function AddNewRow() {
var tr = $("#dgPersons tr").eq(1).clone();
tr.show();
tr.appendTo("#dgPersons");
refreshNo();
return false;
}
function refreshNo() {
var dgg = document.getElementById(dgPersonsID);
var index = 1;
for (var i = 2; i < dgg.rows.length; ++i) {
var cells = dgg.rows[i].cells;
var row = dgg.rows[i];
if (row.style.display != "none") {
cells[0].firstChild.nodeValue = index++;
}
}
return false;
}
</script>
</head>
<body id="dt_example" class="ex_highlight_row">
<form id="form1" runat="server">
<div id="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-image: url(../images/Pop_page_back.gif)" align="center">
<tr>
<td valign="center" style="height: 27px"> <strong>药品折扣基础表</strong></td>
</tr>
</table>
<div id="search">
<table class="t2" style="width:100%">
<tr>
<td class="auto-style2">医院名称:</td>
<td class="auto-style3">
<input type="text" id="txtyymc" runat="server" class="inputbox" readonly="true"/>
</td>
<td class="auto-style2">开始时间:</td>
<td class="auto-style2">
<input type="text" id="txtkssj" runat="server" class="inputbox" readonly="true" />
</td>
<td class="auto-style2">结束时间:</td>
<td class="auto-style2">
<input type="text" id="txtjssj" runat="server" class="inputbox" readonly="true"/>
</td>
<td class="auto-style2">供应商名称:</td>
<td class="auto-style2">
<input type="text" id="txtgys" runat="server" class="inputbox" readonly="true"/>
</td>
</tr>
</table>
</div>
<div id="menu" class="auto-style5">
<asp:Button ID="Btn_Download" runat="server" Text="查 询" CssClass="buttoncss" OnClick="Btn_Download_Click"/>
<asp:Button ID="btn_AddLine" runat="server" Text="新增行" CssClass="buttoncss" OnClientClick="return AddNewRow();return false;" />
<asp:Button ID="btn_xz" runat="server" Text="下 载" CssClass="buttoncss"/>
 <asp:Button ID="btn_dr" runat="server" Text="导 入" CssClass="buttoncss" OnClick="btn_dr_Click" />
<asp:CheckBox runat="server" Text="显示历史数据" ID="cbo_xs" Checked="false" />
</div>
<div>
<asp:GridView ID="YPZKXX" runat="server" AutoGenerateColumns="False"
Width="100%" HorizontalAlign="Center" style="text-align: center" NoNewline="word-break: keep-all;" CssClass="auto-style4" OnRowCancelingEdit="YPCGJCXX_RowCancelingEdit" OnRowEditing="YPCGJCXX_RowEditing" OnRowUpdating="YPCGJCXX_RowUpdating">
<EmptyDataTemplate>
没有数据
</EmptyDataTemplate>
<FooterStyle ForeColor="White" BackColor="#3B6BD1" Font-Bold="True"></FooterStyle>
<SelectedRowStyle Font-Bold="True" ForeColor="Navy" BackColor="#FFCC66" BorderColor="CornflowerBlue" />
<RowStyle ForeColor="#333333" BackColor="#FFFBD6" BorderColor="CornflowerBlue" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Bottom" Wrap="False" />
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#3B6BD1" Wrap="False"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="编号" Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="医院名称">
<ItemTemplate>
<asp:Label ID="lbl_yymc" runat="server" Text='<%# Bind("yymc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="供货商编码">
<ItemTemplate>
<asp:Label ID="lbl_ghsbm" runat="server" Text='<%# Bind("ghsbm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="中心编码">
<ItemTemplate>
<asp:Label ID="txt_zxbm" runat="server" Text='<%# Bind("zxbm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="药品编码">
<ItemTemplate>
<asp:Label ID="lbl_ypbm" runat="server" Text='<%# Bind("ypbm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="药品名称">
<ItemTemplate>
<asp:Label ID="lbl_ypmc" runat="server" Text='<%# Bind("ypmc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="药品规格">
<ItemTemplate>
<asp:Label ID="lbl_ypgg" runat="server" Text='<%# Bind("ypgg") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="药品单位">
<ItemTemplate>
<asp:Label ID="lbl_ypdw" runat="server" Text='<%# Bind("ykdw") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="返利形式">
<ItemTemplate>
<asp:Label ID="txt_flxs" runat="server" Text='<%# Bind("flxs") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="厂家名称">
<ItemTemplate>
<asp:Label ID="lbl_sccj" runat="server" Text='<%# Bind("cjmc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="供货单位">
<ItemTemplate>
<asp:Label ID="lbl_ghdw" runat="server" Text='<%# Bind("ghdw") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="底 价">
<ItemTemplate>
<asp:Label ID="txt_dj" runat="server" Text='<%# Bind("dj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="折扣率">
<ItemTemplate>
<asp:Label ID="txt_zkl" runat="server" Text='<%# Bind("zkl") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="开票价">
<ItemTemplate>
<asp:Label ID="txt_kpj" runat="server" Text='<%# Bind("kpj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="零售价">
<ItemTemplate>
<asp:Label ID="lbl_lsj" runat="server" Text='<%# Bind("lsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="最新零售价">
<ItemTemplate>
<asp:Label ID="lbl_zxlsj" runat="server" Text='<%# Bind("zxlsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="售价差价">
<ItemTemplate>
<asp:Label ID="txt_sjcj" runat="server" Text='<%# Bind("lscj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="成本价">
<ItemTemplate>
<asp:Label ID="txt_cbj" runat="server" Text='<%# Bind("cbj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="折 扣">
<ItemTemplate>
<asp:Label ID="txt_zk" runat="server" Text='<%# Bind("zk") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="净利润">
<ItemTemplate>
<asp:Label ID="txt_jlr" runat="server" Text='<%# Bind("jlr") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="业务费">
<ItemTemplate>
<asp:Label ID="txt_ywf" runat="server" Text='<%# Bind("xywf") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="毛利率">
<ItemTemplate>
<asp:Label ID="txt_mll" runat="server" Text='<%# Bind("mll") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="年">
<ItemTemplate>
<asp:Label ID="lbl_nf" runat="server" Text='<%# Bind("nian") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="月">
<ItemTemplate>
<asp:Label ID="lbl_yf" runat="server" Text='<%# Bind("yue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="管理" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Update" Text="修改"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<cc2:PageTool ID="PageTool1" runat="server" Number="1" PageRows="10" PageCount="100">
</cc2:PageTool>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CenterBase.供应链管理
{
public partial class YPCGJCB : System.Web.UI.Page
{
CenterBaseBLL.YPCGMC cbby = new CenterBaseBLL.YPCGMC();
private static int pCount = 0;//总页数
private static int rCount = 0;//总数据数
private static int pageIndex = 1;//当前页
private static int pageSize = 20;//页面大小
private static string tableName = "View_YPJCB";//表名
private static string fields = "ID,yymc, ghsbm, zxbm, ypbm, ypmc, ypgg, ykdw, flxs,cjmc,ghdw,dj,zkl,kpj,lsj,zxlsj,lscj,cbj,zk,jlr,xywf,mll,nian,yue";//查询的字段
private static string order = "ID desc";//排序
private static string primaryKey = "ID";//关键字
private static string where = " ";
protected void Page_Load(object sender, EventArgs e)
{
//UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
//Response.AddHeader("P3P", "CP=CAO PSA OUR");
////检测用户是否登录
//if (!CheckUserLogin())
//{
// Response.Redirect("../LoginMain.aspx");
//}
////检测用户是否具有权限
//if (!CheckUserAuthority())
//{
// Response.Redirect("../LoginMain.aspx");
//}
if (!Page.IsPostBack)
{
//分页初始化
if (Request.QueryString["page"] == null)
{
pageIndex = 1;
}
else
{
pageIndex = CenterBaseTools.ConvertData.ConvertDataFromStringToInt(Request.QueryString["page"].ToString());
}
////绑定数据
//if (cbo_xs.Checked == true)
//{
//}
DataBinds();
//Sum();
}
}
protected void Btn_Download_Click(object sender, EventArgs e)
{
where = "";
if (sqlwhere() != "")
{
where = where + sqlwhere();
}
DataBinds();
}
public void DataBinds()
{
DataTable dt = new DataTable();
dt = CenterBaseBLL.PageView.GetAll(out pCount, out rCount, pageIndex, pageSize, tableName, fields, order, primaryKey, 2, 0, where);
YPZKXX.DataSource = dt;
YPZKXX.DataBind();
this.PageTool1.PageRows = pageSize;
this.PageTool1.PageCount = rCount;
}
public string sqlwhere()
{
string yymc = txtyymc.Value.ToString();
string kssk = txtkssj.Value.ToString();
string jssj = txtjssj.Value.ToString();
string ghs = txtgys.Value.ToString();
int res = kssk.CompareTo(jssj);
string sql = "";
if (yymc != "")
{
sql = sql + " where CYYMC = '" + yymc + "'";
}
if (yymc != "" && kssk != "" && jssj != "" && res < 0)
{
sql = sql + " where CYYMC='" + yymc + "' and DRKRQ between '" + kssk + "' and '" + jssj + "'";
}
if (yymc != "" && kssk != "" && jssj != "" && res < 0 && ghs != "")
{
sql = sql + " where CYYMC='" + yymc + "' and DRKRQ between '" + kssk + "' and '" + jssj + "' and CGHDW = '" + ghs + "'";
}
return sql;
}
protected void YPCGJCXX_RowEditing(object sender, GridViewEditEventArgs e)
{
YPZKXX.EditIndex = e.NewEditIndex;
DataBinds();
}
protected void YPCGJCXX_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
YPZKXX.EditIndex = -1;
DataBinds();
}
protected void YPCGJCXX_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32((YPZKXX.Rows[e.RowIndex].FindControl("lbl_ID") as Label).Text);
string yymc = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_yymc") as Label).Text);
string ghsbm = (YPZKXX.Rows[e.RowIndex].FindControl("txt_ghsbm") as TextBox).Text.ToString();
string zxbm = (YPZKXX.Rows[e.RowIndex].FindControl("txt_zxbm") as TextBox).Text.ToString();
string ypbm = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_ypbm") as Label).Text);
string ypmc = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_ypmc") as Label).Text);
string ypgg = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_ypgg") as Label).Text);
string ypdw = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_ypdw") as Label).Text);
string flxs = (YPZKXX.Rows[e.RowIndex].FindControl("txt_flxs") as TextBox).Text.ToString();
string cjmc = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_sccj") as Label).Text);
string ghdw = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_ghdw") as Label).Text);
string dj = (YPZKXX.Rows[e.RowIndex].FindControl("txt_dj") as TextBox).Text.ToString();
string zkl = (YPZKXX.Rows[e.RowIndex].FindControl("txt_zkl") as TextBox).Text.ToString();
string kpj = (YPZKXX.Rows[e.RowIndex].FindControl("txt_kpj") as TextBox).Text.ToString();
string lsj = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_lsj") as Label).Text);
string dykpj = (YPZKXX.Rows[e.RowIndex].FindControl("txt_dykpj") as TextBox).Text.ToString();
string zxlsj = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_zxlsj") as Label).Text);
string sjcj = (YPZKXX.Rows[e.RowIndex].FindControl("txt_sjcj") as TextBox).Text.ToString();
string cbj = (YPZKXX.Rows[e.RowIndex].FindControl("txt_cbj") as TextBox).Text.ToString();
string zk = (YPZKXX.Rows[e.RowIndex].FindControl("txt_zk") as TextBox).Text.ToString();
string jlr = (YPZKXX.Rows[e.RowIndex].FindControl("txt_jlr") as TextBox).Text.ToString();
string ywf = (YPZKXX.Rows[e.RowIndex].FindControl("txt_ywf") as TextBox).Text.ToString();
string mll = (YPZKXX.Rows[e.RowIndex].FindControl("txt_mll") as TextBox).Text.ToString();
string nf = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_nf") as Label).Text);
string yf = Convert.ToString((YPZKXX.Rows[e.RowIndex].FindControl("lbl_yf") as Label).Text);
DateTime time = System.DateTime.Now;
string sql = "insert into YPFXJCXX(yymc,ghsbm,zxbm,ypbm,ypmc,ypgg,ykdw,flxs,cjmc,ghdw,dj,zkl,kpj,lsj,dykpj,zxlsj,lscj,cbj,zk,jlr,xywf,mll,nian,yue,ITime)" +
"values(" + "'" + yymc + "'," + "'" + ghsbm + "'," + "'" + zxbm + "'," + "'" + ypbm + "'," + "'" + ypmc + "'," + "'" + ypgg + "'," + "'" + ypdw + "'," + "'" + flxs + "'," + "'" + cjmc + "'," + "'" + ghdw + "'," + "'" + dj + "'," + "'" + zkl + "'," + "'" + kpj + "'," + "'" + lsj + "'," + "'" + dykpj + "'," + "'" + zxlsj + "'," + "'" + sjcj + "'," + "'" + cbj + "'," + "'" + zk + "'," + "'" + jlr + "'," + "'" + ywf + "'," + "'" + mll + "'," + "'" + nf + "'," + "'" + yf + "'," + "'" + time + "'" + ")";
CenterBaseDAL.ExecuteSql exec = new CenterBaseDAL.ExecuteSql();
int falg = exec.run(sql);
if (falg == 1)
{
Response.Write("<script>alert('更新成功')</script>");
Response.Write("<script>window.location.href='YPCGJCB.aspx'</script>");
Response.End();
}
else
{
Response.Write("<script>alert('更新失败')</script>");
}
}
protected void btn_dr_Click(object sender, EventArgs e)
{
Response.Write("<script>window.open('药品销售明细查询/Improt.aspx','_blank')</script>");
}
}
}
我用自己方法添加了一列行号,求大神指点!!