好久没做.NET了 今天遇到个问题 如图
gridview 的第一列是模板列里面有RadioButton
我现在要实现的是 点击删除按钮前 判断是否有选中的行。没有给予提示。
如果选择了 就跳出提示框 问是否要删除 点击否不执行 点击OK 执行删除谁能给个例子最好 我现在的做法是前台javascript验证是否选中 然后再去后台处理但是现在我的OnClientClick 执行完javascript的方法后就直接进入后台了 我就郁闷了 大家帮帮忙
gridview 的第一列是模板列里面有RadioButton
我现在要实现的是 点击删除按钮前 判断是否有选中的行。没有给予提示。
如果选择了 就跳出提示框 问是否要删除 点击否不执行 点击OK 执行删除谁能给个例子最好 我现在的做法是前台javascript验证是否选中 然后再去后台处理但是现在我的OnClientClick 执行完javascript的方法后就直接进入后台了 我就郁闷了 大家帮帮忙
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDelete" runat="server"
CommandArgument='<%# Eval("nId") %>' CommandName="De"
onclientclick="return confirm('是否刪除?')">刪除</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
注意加上CommandName="De"
事件:protected void gvNews_RowCommand(object sender, GridViewCommandEventArgs e)
{
//获取命令名称
string cmd = e.CommandName;
//获取命令参数
int nId = Convert.ToInt32(e.CommandArgument); if (cmd == "De")
{
//执行删除
NewsManager.DeleteNewsByNId(nId);
}
//刷新数据
this.gvNews.DataBind();
}
<SCRIPT LANGUAGE=javascript>function p_del() {
var msg = "您真的确定要删除吗?\n\n请确认!";
if (confirm(msg)==true){
return true;
}else{
return false;
}
}</SCRIPT>调用方法:
<a href="del.jsp?id=<%=id%>" onclick="javascript:return p_del()">删 除</a>
自己新建一个页,然后拷贝如下代码,你想要的功能都在里面呢,复选框多行删除、单行删除都有。
aspx代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AfficheSearch.aspx.cs" Inherits="MLFS_AfficheSearch" %><%@ Register Assembly="WebControlLibrary" Namespace="WebControlLibrary" TagPrefix="cs" %><!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 id="Head1" runat="server">
<title>无标题</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%--<asp:Label ID="Label1" runat="server" Text="按人员查询:"></asp:Label>
<asp:TextBox ID="Tbname" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="按部门查询:"></asp:Label>
<asp:TextBox ID="TbDpart" runat="server"></asp:TextBox><br />
<asp:Button ID="Btsubmit" runat="server" Text="提交" OnClick="btsubmit_Click" /><br />--%>
<table class="GridViewStyle_Grid_HTML_Blue" border="0" width="100%" cellspacing="1" cellpadding="2">
<tbody id="m_SearchArea" runat="server" style="text-align: center;">
</tbody>
<tr class="GridViewStyle_Row_HTML_Blue" >
<td id="m_ButtonArea" colspan="6" runat="server" style="text-align: left">
<asp:Label ID="Label1" runat="server" Text="公告标题"></asp:Label>
<asp:TextBox ID="Tbname" runat="server"></asp:TextBox>
<asp:Button ID="Btsubmit" runat="server" Text="查询" OnClick="btsubmit_Click" />
</td>
</tr>
<tr class="GridViewStyle_Row_HTML_Blue">
<td id="Td1" style="text-align:right;" colspan="6" runat="server">
<asp:Button ID="Btadd" runat="server" Text="新增" OnClick="Btadd_Click" />
<asp:Button ID="Btdel" runat="server" Text="删除" OnClientClick="return CheckDeleteSelected();" OnClick="Btdel_Click" />
</td>
</tr>
<tr class="GridViewStyle_Row_HTML_Blue">
<td colspan="6" style="text-align: center;">
<div id="m_GridScroll" runat="server">
<asp:GridView ID="m_Grid" runat="server" Width="100%" AllowPaging="True" PageSize="13" OnRowDataBound="m_Grid_RowDataBound" AutoGenerateColumns="False" OnRowDeleting="m_Grid_RowDeleting" OnRowEditing="m_Grid_RowEditing" >
<PagerSettings Visible="False" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBoxHead" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<HeaderStyle Width="3%" />
</asp:TemplateField>
<asp:BoundField DataField="IID" >
<HeaderStyle Width="0%" />
</asp:BoundField>
<asp:BoundField DataField="公告标题" HeaderText="公告标题" >
<HeaderStyle Width="70%" />
</asp:BoundField>
<asp:BoundField DataField="发布日期" HeaderText="发布日期" DataFormatString="{0:d}" >
<HeaderStyle Width="20%" />
</asp:BoundField>
<asp:TemplateField HeaderText="操作">
<HeaderTemplate>
<asp:Label ID="m_lblOperator" runat="server" Text="操作"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="IBdel" runat="server" ImageUrl="~/MLFS/Images/GridDeleteImg.gif" CommandName="delete" />
<asp:ImageButton ID="IBEdit" runat="server" ImageUrl="~/MLFS/Images/GridEditImg.gif" CommandName="Edit" />
</ItemTemplate>
<HeaderStyle Width="7%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
<tr class="GridViewStyle_Row_HTML_Blue">
<td colspan="6">
<cs:c2softgridviewnavigation id="m_Navigation" runat="server" gridviewid="m_Grid" height="30px" pagesize="13"></cs:c2softgridviewnavigation>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
cs代码:
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;
using System.Text;
using System.Data.SqlClient;/// <summary>
/// CSJ 2010-1-21 公告浏览
/// </summary>
public partial class MLFS_AfficheSearch : BasePageAdmin
{
/// <summary>
/// 页面加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Title = "公告查询";
this.WriteOperationLog("Enter");
m_Navigation.SQLString = "select * from Freights_BC001_公告 order by 发布日期 desc ";
m_Navigation.DataBind();
}
this.RegJavaScriptBlock(@"function CheckDelete()
{return window.confirm('" + "确定删除数据吗?" + "');}");
//这个JavaScript用来在客户端确认是否删除,如果没有选中给提示.
string javas = @"
function CheckDeleteSelected()
{
if(window.confirm('" + "确定删除数据吗?" + @"')==false)
{
return false;
}
//alert('kaishi');
var GridView = document.getElementById('" + this.m_Grid.ClientID + @"');
for(var i = 1; i < GridView.rows.length; i++)
{
var chkItem = GridView.rows[i].cells[0].getElementsByTagName('INPUT')[0];
if(chkItem != null && !chkItem.disabled)
{
if(chkItem.checked == true)
{
return true;
}
}
}
alert('" + "请至少选择一行需要删除的数据" + @"');
return false;
}";
this.RegJavaScriptBlock(javas);
} /// <summary>
/// 在列头的checkbox中加JavaScript,用来实现全选功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void m_Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果正在生成表头
if (e.Row.RowIndex < 0)
{
CheckBox cbox = (CheckBox)e.Row.FindControl("CheckboxHead");
if (cbox != null)
{
cbox.Attributes.Add("onclick", "CheckAll(this)");
StringBuilder builder = new StringBuilder();
builder.Append("\r\n function CheckAll(oCheckbox)\r\n {\r\n var GridView = document.getElementById('" + this.m_Grid.ClientID + "');\r\n for(i = 1; i < GridView.rows.length; i++)\r\n {\r\n var chkItem = GridView.rows[i].cells[0].getElementsByTagName('INPUT')[0];\r\n if(chkItem != null && !chkItem.disabled)\r\n {\r\n chkItem.checked = oCheckbox.checked;\r\n }\r\n }\r\n }\r\n ");
this.RegJavaScriptBlock(builder.ToString()); //这里将IID列隐藏 放在这里是因为这里能避免数据表为空的时候产生的错误.
e.Row.Cells[1].Visible = false;
}
}
else
{
////这里判断是否是超级管理员,超级管理员不能删除.
//if (e.Row.Cells[1].Text == "1")
//{
// CheckBox cb = (CheckBox)e.Row.Cells[0].FindControl("CheckBox1");
// cb.Enabled = false;
// ImageButton ibd = (ImageButton)e.Row.FindControl("IBdel");
// ibd.Enabled = false;
// ibd.ImageUrl = "~/MLFS/Images/GridDeleteImgEnabled.gif";
//}
ImageButton ib = (ImageButton)e.Row.FindControl("IBdel");
if (ib != null)
{
ib.OnClientClick = "javascript:return CheckDelete();";
} //这里将IID列隐藏 放在这里是因为这里能避免数据表为空的时候产生的错误.
e.Row.Cells[1].Visible = false;
}
} /// <summary>
/// 提交搜索
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btsubmit_Click(object sender, EventArgs e)
{
string sqlstr = "select * from Freights_BC001_公告 where 1=1 ";
if (Tbname.Text != "")
{
sqlstr = sqlstr + string.Format("and 公告标题 like '%{0}%' ", Tbname.Text);
}
m_Navigation.SQLString = sqlstr + " order by 发布日期 desc ";
m_Navigation.DataBind();
} /// <summary>
/// 删除选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Btdel_Click(object sender, EventArgs e)
{
string strExecDelSql = "";
foreach (GridViewRow row in m_Grid.Rows)
{
CheckBox cb = (CheckBox)row.Cells[0].FindControl("CheckBox1");
if (cb != null)
{
if (cb.Checked == true)
{
strExecDelSql += string.Format("Delete from [Freights_BC001_公告] where IID={0};", row.Cells[1].Text);
}
}
} IDbTransaction Tran = null;
try
{
Tran = BasePageAdmin.SqlConn.BeginTransaction(); BasePageAdmin.ExecuteNonQuery(strExecDelSql, Tran); Tran.Commit();
Tran = null;
m_Navigation.DataBind();
}
catch (Exception me)
{
if (Tran != null)
{
Tran.Rollback();
this.MsgBox(me.Message);
}
}
} /// <summary>
/// 逐条删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void m_Grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string IID = m_Grid.Rows[e.RowIndex].Cells[1].Text;
string strDelSql = string.Format("Delete from [Freights_BC001_公告] where IID={0}", IID);
BasePageAdmin.ExecuteNonQuery(strDelSql);
m_Navigation.DataBind();
} /// <summary>
/// gridview的编辑事件,跳转页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void m_Grid_RowEditing(object sender, GridViewEditEventArgs e)
{
string IID = m_Grid.Rows[e.NewEditIndex].Cells[1].Text;
Response.Redirect(string.Format("Affiche.aspx?IID={0}&EditStyle=Edit", IID));
} /// <summary>
/// 添加新数据,跳转页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Btadd_Click(object sender, EventArgs e)
{
Response.Redirect("Affiche.aspx?EditStyle=Add");
}
}
<script type="text/javascript">
function check()
{
if(window.confirm('确认修改数据?'))
{
return true;
}
else
{
return false;
}
} </script>然后再按钮上调用 check() <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="修改" Width="57px" OnClientClick="return check();" />