我的gridvew里放了一列checkbox按钮,在删除时我先判断选中的行数,然后弹出一个确认窗口,但是第一次点击时没有出,第二次时才弹出窗口。之后每次一次弹出窗口选择的数即下文中的目count都是上一次选择的,不知道各位有没有遇到过这个问题。怎么解决?
protected void btDelete_Click(object sender, EventArgs e)
{
int count = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox Cb = (CheckBox)GridView1.Rows[i].FindControl("Checkbox");
if (Cb.Checked)
{
count++;
}
}
if (0 == count)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('请选择删除项')</script>");
}
else
{
this.btDelete.Attributes.Add("onclick","return confirm('确定删除这" +count.ToString()+ "项么?');");
for (int j = 0; j < GridView1.Rows.Count; j++)
{
CheckBox Cb = (CheckBox)GridView1.Rows[j].FindControl("Checkbox");
if (Cb.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[j]["id"].ToString());
int flag = Productbll.proDel(id);
}
}
if (this.ddlProductLine.SelectedValue == "please choose")
{
MorenBind();
}
else
{
Bind();
}
}
}
protected void btDelete_Click(object sender, EventArgs e)
{
int count = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox Cb = (CheckBox)GridView1.Rows[i].FindControl("Checkbox");
if (Cb.Checked)
{
count++;
}
}
if (0 == count)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('请选择删除项')</script>");
}
else
{
this.btDelete.Attributes.Add("onclick","return confirm('确定删除这" +count.ToString()+ "项么?');");
for (int j = 0; j < GridView1.Rows.Count; j++)
{
CheckBox Cb = (CheckBox)GridView1.Rows[j].FindControl("Checkbox");
if (Cb.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[j]["id"].ToString());
int flag = Productbll.proDel(id);
}
}
if (this.ddlProductLine.SelectedValue == "please choose")
{
MorenBind();
}
else
{
Bind();
}
}
}
解决方案 »
- 问题没解决。。。。再次求问;datagrid简单操作。。。。恳请赐教。。在线,等等等!!!!!!!!
- DataSet 條件查詢問題 或DataView分頁問題
- 如何在直接在屏幕上直接显示字符串或者图片?
- textbox验证输入
- 100分,100分,给分少了没人来看 哎 100了
- panel上画图怎么让它不消失?
- 各位大侠,求一个具有挑战性的控件
- 请教正则表达问题,取两个字符中间部分,等待
- C#数据库问题
- 关于在C#中调用API的问题,我调用ExitWindowsEx()函数,中使用EWX_SHUTDOWN参数,但是程序没有响应
- .net datalist分页问题,设置了allowPaging=true,pagesize也设置了但是所有的数据还是在一个页面显示,求助
- c#求高手给小弟看看那里出错了怎么改
将这句代码放到数据绑定RowDataBound事件里面,因为你点击删除按钮触发事件后才会执行此方法,第一次是把方法注册上,然后才可以使用,所以会少一条数据
var GridView2 = document.getElementById("<%=GridView2.ClientID %>");
for(i = 1;i < GridView2.rows.length; i++) {
if(GridView2.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked)
count++;
}
if(count ==0)
alert("没选")
{
if (!IsPostBack)
{
Button1.Attributes.Add("onclick", "javascript:Confirm();");
}
}
protected void btDelete_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(HiddenField1.Value) && int.Parse(HiddenField1.Value) > 0)
{
for (int j = 0; j < GridView1.Rows.Count; j++)
{
CheckBox Cb = (CheckBox)GridView1.Rows[j].FindControl("Checkbox");
if (Cb.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[j]["id"].ToString());
int flag = Productbll.proDel(id); }
} if (this.ddlProductLine.SelectedValue == "please choose")
{
MorenBind();
}
else
{
Bind();
}
}
}ASPX前台代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>
<script language="javascript" type="text/javascript">
function Confirm()
{
var count=0;
var GridView = document.getElementById("<%=GridView1.ClientID %>");
for( i=1; i<GridView.rows.length; i++)
{
if(GridView.rows(i).cells(0).children(0).checked)
{
count++;
}
}
document.getElementById("<%=HiddenField1.ClientID %>").value = 0;
if(count > 0)
{
if(confirm("确定删除这" + count + "项么?"))
{
document.getElementById("<%=HiddenField1.ClientID %>").value = count;
}
}
else
{
alert("请选择删除项");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
</form>
</body>
</html>
你自己参照试下吧
去学一下jquery,你会有新的思路和新的认识。
http://jquery.com/
用Jquery获取更简单也更利于把JS放在单独的文件中$("[id$=控件的ID]")