这几天刚接触asp.net这东西,不是懂,老师给了一个大概的框架,叫我们完善一下。我弄个删除一直搞不定,<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserMng.aspx.cs" Inherits="UserMng" %><!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="~/Css/main.css" rel="stylesheet" type="text/css" />
<link href="~/Css/css.css" rel="stylesheet" type="text/css" />
</head>
<script type="text/javascript">
function checkAll(checked, lstName)
{
var input = document.getElementsByTagName("INPUT");
for (var i = 0; i < input.length; i++)
{
if (lstName == null)
{
input[i].checked = checked;
}
else if (input[i].type == "checkbox" && input[i].id.length > lstName.length && input[i].id.indexOf(lstName) > 0 )
{
input[i].checked = checked;
}
}
}
</script> <body>
<form id="form1" runat="server">
<div id="MainContentCommon">
<div class="tit" id="mcTitle" runat="server" style="display: none" title="testss" visible="false">
>>>
<asp:Label ID="lblTabName" runat="server" Visible="False"></asp:Label>
</div>
<table cellpadding="0" cellspacing="0" style="background-color: lightsteelblue; width: 100%; height: 24px;">
<tr>
<td>
<asp:Button ID="BtnExport" runat="server" CausesValidation="False" CssClass="flatbuttonface iconPrint"
Text="打印/导出 " UseSubmitBehavior="False" EnableViewState="false" Visible="true" />
<%--普通查询的"清空查询条件"--%>
<input id="BtnClear_S" runat="server" type="button" value="清空查询条件" class="flatbuttonface iconClear" onclick="ClearData();" visible="true" />
<input id="mschart" runat="server" type="button" class="flatbuttonface iconChart" value="统计图" onclick="location.href='MsChart.aspx'" visible="true" />
<asp:Button ID="DelButton" CausesValidation="False" runat="server" Text="选中删除"
EnableViewState="false" OnClientClick="return checkDel();" CssClass="flatbuttonface iconDel"
Visible="false" />
<input type="button" value="返回" class="flatbuttonface iconReturn" onclick="history.back()" />
</td>
</tr>
</table>
</div>
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging"
AllowSorting="True" OnSorting="GridView1_Sorting" onrowdatabound="GridView1Sel_RowDataBound" >
<FooterStyle BorderStyle="None" Font-Bold="True" ForeColor="White" />
<%-- 数据行的背景色 --%>
<RowStyle BackColor="#EFF3FB" />
<%-- 下面是添加选中框(一列) --%>
<Columns>
<asp:TemplateField >
<HeaderTemplate >
<asp:CheckBox ID="ChkAll" runat="server" onclick="checkAll(this.checked)" Text ="全选" ></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat ="server" ID="cboxTemple" />
</ItemTemplate>
</asp:TemplateField>
<%-- 下面是删除列 --%>
<asp:TemplateField >
<HeaderTemplate >
<asp:Label ID="label1" runat="server" Text="删除" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%-- 下面是编辑列 --%>
<asp:TemplateField >
<HeaderTemplate >
<asp:Label ID="label2" runat="server" Text="编辑" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>cs代码为using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;//
using Maticsoft.DBUtility;public partial class UserMng : System.Web.UI.Page
{
private DbHelper db = new DbHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
} /// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind(); //重新绑定GridView数据的函数
} private void GridViewBind()
{
// 获取GridView排序数据列及排序方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"]; String strSql = "select * from tab_user";
DataTable dt = db.DataTable(strSql); // 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
} ///下面这段代码可以动态添加dt中的列
//for (int i = 0; i < dt.Columns.Count; i++)
//{
// BoundField bc = new BoundField();
// bc.DataField = dt.Columns[i].ColumnName.ToString();
// bc.HeaderText = dt.Columns[i].Caption.ToString();
// GridView1.Columns.Add(bc);
//}
////下面动态添加编辑按钮
//GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
//CommandField cf = new CommandField();
//cf.ButtonType = ButtonType.Link; //如果是ButtonType.Button 则显示的是按钮
//cf.ShowEditButton = true;
//cf.CausesValidation = false;
//GridView1.Columns.Add(cf);
////下面动态添加删除按钮
//CommandField cf2 = new CommandField();
//cf2.ButtonType = ButtonType.Link;
//cf2.ShowDeleteButton = true;
//cf2.CausesValidation = false;
//GridView1.Columns.Add(cf2);
// GridView绑定并显示数据
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
} /// <summary>
/// GridView排序事件
/// </summary>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// 从事件参数获取排序数据列
string sortExpression = e.SortExpression.ToString(); // 假定为排序方向为“顺序”
string sortDirection = "ASC"; // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.GridView1.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
} // 重新设定GridView排序数据列及排序方向
this.GridView1.Attributes["SortExpression"] = sortExpression;
this.GridView1.Attributes["SortDirection"] = sortDirection; this.GridViewBind();
} protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{ } //由于是手工绑定,鼠标经过的时候颜色变化,需要自己实现:
protected void GridView1Sel_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) //判断是否是DataRow,以防止鼠标经过Header也有效果
{
e.Row.Attributes.Add("onmouseover", "e=this.style.backgroundColor; this.style.backgroundColor='#cccccc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e");
}
}
}一直GridView“GridView1”激发了未处理的事件“RowDeleting”。在下对这个是在不太懂,求指教。
<head runat="server">
<title>用户管理</title>
<link href="~/Css/main.css" rel="stylesheet" type="text/css" />
<link href="~/Css/css.css" rel="stylesheet" type="text/css" />
</head>
<script type="text/javascript">
function checkAll(checked, lstName)
{
var input = document.getElementsByTagName("INPUT");
for (var i = 0; i < input.length; i++)
{
if (lstName == null)
{
input[i].checked = checked;
}
else if (input[i].type == "checkbox" && input[i].id.length > lstName.length && input[i].id.indexOf(lstName) > 0 )
{
input[i].checked = checked;
}
}
}
</script> <body>
<form id="form1" runat="server">
<div id="MainContentCommon">
<div class="tit" id="mcTitle" runat="server" style="display: none" title="testss" visible="false">
>>>
<asp:Label ID="lblTabName" runat="server" Visible="False"></asp:Label>
</div>
<table cellpadding="0" cellspacing="0" style="background-color: lightsteelblue; width: 100%; height: 24px;">
<tr>
<td>
<asp:Button ID="BtnExport" runat="server" CausesValidation="False" CssClass="flatbuttonface iconPrint"
Text="打印/导出 " UseSubmitBehavior="False" EnableViewState="false" Visible="true" />
<%--普通查询的"清空查询条件"--%>
<input id="BtnClear_S" runat="server" type="button" value="清空查询条件" class="flatbuttonface iconClear" onclick="ClearData();" visible="true" />
<input id="mschart" runat="server" type="button" class="flatbuttonface iconChart" value="统计图" onclick="location.href='MsChart.aspx'" visible="true" />
<asp:Button ID="DelButton" CausesValidation="False" runat="server" Text="选中删除"
EnableViewState="false" OnClientClick="return checkDel();" CssClass="flatbuttonface iconDel"
Visible="false" />
<input type="button" value="返回" class="flatbuttonface iconReturn" onclick="history.back()" />
</td>
</tr>
</table>
</div>
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging"
AllowSorting="True" OnSorting="GridView1_Sorting" onrowdatabound="GridView1Sel_RowDataBound" >
<FooterStyle BorderStyle="None" Font-Bold="True" ForeColor="White" />
<%-- 数据行的背景色 --%>
<RowStyle BackColor="#EFF3FB" />
<%-- 下面是添加选中框(一列) --%>
<Columns>
<asp:TemplateField >
<HeaderTemplate >
<asp:CheckBox ID="ChkAll" runat="server" onclick="checkAll(this.checked)" Text ="全选" ></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat ="server" ID="cboxTemple" />
</ItemTemplate>
</asp:TemplateField>
<%-- 下面是删除列 --%>
<asp:TemplateField >
<HeaderTemplate >
<asp:Label ID="label1" runat="server" Text="删除" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%-- 下面是编辑列 --%>
<asp:TemplateField >
<HeaderTemplate >
<asp:Label ID="label2" runat="server" Text="编辑" />
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>cs代码为using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;//
using Maticsoft.DBUtility;public partial class UserMng : System.Web.UI.Page
{
private DbHelper db = new DbHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
} /// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind(); //重新绑定GridView数据的函数
} private void GridViewBind()
{
// 获取GridView排序数据列及排序方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"]; String strSql = "select * from tab_user";
DataTable dt = db.DataTable(strSql); // 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
} ///下面这段代码可以动态添加dt中的列
//for (int i = 0; i < dt.Columns.Count; i++)
//{
// BoundField bc = new BoundField();
// bc.DataField = dt.Columns[i].ColumnName.ToString();
// bc.HeaderText = dt.Columns[i].Caption.ToString();
// GridView1.Columns.Add(bc);
//}
////下面动态添加编辑按钮
//GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
//CommandField cf = new CommandField();
//cf.ButtonType = ButtonType.Link; //如果是ButtonType.Button 则显示的是按钮
//cf.ShowEditButton = true;
//cf.CausesValidation = false;
//GridView1.Columns.Add(cf);
////下面动态添加删除按钮
//CommandField cf2 = new CommandField();
//cf2.ButtonType = ButtonType.Link;
//cf2.ShowDeleteButton = true;
//cf2.CausesValidation = false;
//GridView1.Columns.Add(cf2);
// GridView绑定并显示数据
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
} /// <summary>
/// GridView排序事件
/// </summary>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// 从事件参数获取排序数据列
string sortExpression = e.SortExpression.ToString(); // 假定为排序方向为“顺序”
string sortDirection = "ASC"; // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.GridView1.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
} // 重新设定GridView排序数据列及排序方向
this.GridView1.Attributes["SortExpression"] = sortExpression;
this.GridView1.Attributes["SortDirection"] = sortDirection; this.GridViewBind();
} protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{ } //由于是手工绑定,鼠标经过的时候颜色变化,需要自己实现:
protected void GridView1Sel_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) //判断是否是DataRow,以防止鼠标经过Header也有效果
{
e.Row.Attributes.Add("onmouseover", "e=this.style.backgroundColor; this.style.backgroundColor='#cccccc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e");
}
}
}一直GridView“GridView1”激发了未处理的事件“RowDeleting”。在下对这个是在不太懂,求指教。
解决方案 »
- 奇怪的Models的问题
- 使用IHttpModule接口重写url的效率?
- 关于论坛的数据库设计,请指点一下!!
- asp.net下怎样使html控件利用javascript操作服务器控件的显示与隐藏
- ?关于gridwiew表格弹出选择
- 哇~一个奇怪的问题:页面中所有的Button都不管用了!!!
- 做安装包时候:下列文件可能具有不能自动确定的依赖项,请确认所有依赖项添加到项目中
- C# gridview 点击Linkbutton通过某一条件修改数据
- response.redirect重定向出现问题,请看一下!!
- 怎么捕获文本框中的双击事件?
- 关于cookie过期时间设置
- 扩展程序所在的 UpdatePanel 不能与它所扩展的控件不同。
哈哈。。这个问题。。很经典的新手错误 <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete">删除</asp:LinkButton>
————-》红色部分换个名字:比如lbtnDel...i不要用Delete
{
//先设置DataKeys 不会设置百度一下
int i = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());//获取主键
//数据库的删除
//绑定数据库
}
protected void dvListChild_RowCommand(object sender, GridViewCommandEventArgs e)
{
string strID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
if(CommandName == "Delete")
{
string strsql="Delete from tableName where ID = '"+strID+"'";
if(DBHELP.EXECUTENONQeury(strsql) != -9999)
{
Response.Write("<script>alert('删除成功!');</script>");
}
else
{
Response.Write("<script>alert('删除失败!')</script>");
} }
}