我要删除Gridview选中的行记录,但总是不行.代码如下:
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void linkDel_Click(object sender, EventArgs e)
{
gridbindData();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkNewsId = (CheckBox)GridView1.Rows[i].FindControl("chkNewsId");//调式到这里的时候chkNewsId 显示text=1 checked=false, 为什么获取不了选中???
int newsId = Convert.ToInt32(chkNewsId.Text);
if (chkNewsId.Checked)
{
susframe.Delete(newsId); }
}
gridbindData();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void linkDel_Click(object sender, EventArgs e)
{
gridbindData();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkNewsId = (CheckBox)GridView1.Rows[i].FindControl("chkNewsId");//调式到这里的时候chkNewsId 显示text=1 checked=false, 为什么获取不了选中???
int newsId = Convert.ToInt32(chkNewsId.Text);
if (chkNewsId.Checked)
{
susframe.Delete(newsId); }
}
gridbindData();
}
{
int newsId = Convert.ToInt32(chkNewsId.Text);
susframe.Delete(newsId);
}
这是什么意思?
重新绑定了数据
那你的check还在吗?
你先绑定了在想取已经选择的,怎么可能取到?
你应该先删除了在去绑定啊就是把gridbindData();写在删除的下面
gridbindData();
这个方法也贴出来给大家看看行不,现在大家都怀疑你回到后台获取选中的列之前又对datagrid进行了一次绑定,如果是这样,那肯定不行啦,你都又绑定一次了,这个datagrid已经不是你页面看到的datagrid了
for (int i = 0; i < GridView1.Rows.Count; i++)//GridView1.Rows.Count就获取不到值
{
CheckBox chkNewsId = (CheckBox)GridView1.Rows[i].FindControl("chkNewsId"); if (chkNewsId.Checked == true)
{
int newsId = Convert.ToInt32(chkNewsId.Text);
susframe.Delete(newsId);
}
}
bindData();
bindData();了吗?
好的!!! /// <summary>
/// 数据绑定
/// </summary>
private void gridbindData()
{
string sql = "select [user_id],[user_name],role_id,case isLock when 1 then '是' else '否' end isLock,last_time,login_time,login_times,dept_id,latest_time,latest_user_id from sys_user";
DataSet ds;
IDataParameter[] param ={ AdoHelper.MakeInParam("@SQL", SqlFieldType.NVarChar, sql),
AdoHelper.MakeInParam("@OrderBy", SqlFieldType.NVarChar, "id"),
AdoHelper.MakeInParam("@startIndex", SqlFieldType.Int, AspNetPager1.StartRecordIndex),
AdoHelper.MakeInParam("@endindex", SqlFieldType.Int, AspNetPager1.EndRecordIndex)};
user.GetDataset(param, out ds);
if (ds.Tables[0].Rows.Count == 0)
{
MessageBox.ShowMsg(Msg.NoRecord);
} GridView1.DataSource = ds;
GridView1.DataBind();
}pageload里
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
recordCount();
bindData();
} }
{
CheckBox chkNewsId = GridView1.Rows[i].FindControl("chkNewsId")as CheckBox;
if (cb.Checked == true)
{
//获得每行ID
int i = (Convert.ToInt32(this.GridView1.DataKeys[i].Value));
susframe.Delete(i);
}
}
gridbindData();
Ctrl + c Ctrl + v 到项目 然后F5运行.
调式后GridView1.Rows.Count=0,不行啊!
{ if (((CheckBox)gv_HAWB.Rows[i].FindControl("chkId")).Checked)
{ }
}
我看下你的Page_Load。
改成下面...
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridbindData(); //
}
}
aspx代码:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>账户管理</title>
<LINK rev="stylesheet" href="../css/Style.css" type="text/css" rel="stylesheet">
<script language="ecmascript" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
//判断选择编辑
function CheckEditSel()
{
var iSelEditNum=0;
for(var i= 0; i<oSel.length;i++)
{
if (oSel[i].type=="checkbox" && oSel[i].checked)
{
iSelEditNum++;
}
}
if(iSelEditNum==0){
alert("请选择其中一项进行编辑");
return false;
}else if(iSelEditNum>1){
alert("最多只能选择一项进行编辑");
return false;
}
}//判断选择删除
function CheckDelSel()
{
var iSelEditNum=0;
for(var i= 0; i<oSel.length;i++)
{
if (oSel[i].type=="checkbox" && oSel[i].checked)
{
iSelEditNum++;
}
}
if(iSelEditNum==0){
alert("请选择其中一项进行删除");
return false;
}
}
//全选
function CheckAllBox(obj){
var oSel = document.getElementsByTagName("input");
for(var i=0;i<oSel.length;i++)
{
if(oSel[i].type=="checkbox")
oSel[i].checked=(obj.checked)?true:false;
}
}var oSel = document.getElementsByTagName("input");
var iTotalNum=0; </script>
</head>
<body>
<form id="form1" runat="server">
<div style="width:98%">
<table cellpadding="0" cellspacing="0" height="28" style="width: 100%">
<tr>
<td align="right" background="../Images/navbg.jpg" height="25" style="width: 100px;
background-repeat: repeat-x;">
<div style="padding-right:20px; text-align: left;" nowrap="nowrap">
当前位置:系统管理 > 账号管理
<asp:Button ID="linAdd" runat="server" CssClass="button" CausesValidation="false" OnClick="linkAdd_Click" Text="添加" ></asp:Button>
<asp:Button ID="LinkDel" runat="server" CssClass="button" CausesValidation="false" OnClick="linkDel_Click" OnClientClick="return CheckDelSel()" Text="删除" ></asp:Button>
<asp:Button ID="LinkEdit" runat="server" CssClass="button" CausesValidation="false" OnClick="LinkEdit_Click" OnClientClick="return CheckEditSel()" Text="编辑"></asp:Button> </div></td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="Small" STYLE="border-collapse:separate" BorderWidth="0px" ForeColor="#333333" CellPadding="2" EnableViewState="False" Width="100%">
<HeaderStyle Font-Bold="False" HorizontalAlign="Center" BackColor="#77BFD8" ForeColor="Black" Height="22px"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="序号">
<HeaderTemplate>
<input id="chkSelAll" type="checkbox" onclick="CheckAllBox(this)" runat="server"/>全选/反选 </HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkNewsId" runat="server" Text='<%# Bind("RowNumber") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="账号">
<ItemTemplate>
<asp:Label ID="lblcad" runat="server" Text='<%# Bind("user_id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("user_name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="所属角色">
<ItemTemplate>
<asp:Label ID="lblrole" runat="server" Text='<%# Bind("role_id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="上次登录时间">
<ItemTemplate>
<asp:Label ID="lbltime" runat="server" Text='<%# Bind("last_time") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="本次登录时间">
<ItemTemplate>
<asp:Label ID="lblnowtime" runat="server" Text='<%# Bind("login_time") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="登录次数">
<ItemTemplate>
<asp:Label ID="lblcount" runat="server" Text='<%# Bind("login_times") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="是否锁定">
<ItemTemplate>
<asp:Label ID="lblisloak" runat="server" Text='<%# Bind("isLock") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作日期">
<ItemTemplate>
<asp:Label ID="lbldatatime" runat="server" Text='<%# Bind("latest_time") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作用户">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# Bind("latest_user_id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
抱歉,没有数据......
</EmptyDataTemplate>
<AlternatingRowStyle BackColor="#E1F5FB" />
<RowStyle Height="22px" />
</asp:GridView>
</div>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanged="AspNetPager1_PageChanged"
ShowPageIndexBox="Always" PageIndexBoxType="TextBox" FirstPageText="首页" LastPageText="尾页" NextPageText="下页" PrevPageText="上页" UrlPaging="True" CustomInfoHTML="共%RecordCount%条记录,第<font color='red'><b>%currentPageIndex%</b></font>/%PageCount%页,每页%PageSize%条" CustomInfoSectionWidth="60%" ShowCustomInfoSection="Left" PageSize="15" BackColor="#E1F5FB" LayoutType="Table" Width="96%">
</webdiyer:AspNetPager> </form>
</body>
</html>
cs代码:
namespace HexieSoft.Web.Sys
{
public partial class UserInfo : PageBase
{
SysUser user = new SysUser();
HexieSoft.FrameWork.SysUser susframe = new HexieSoft.FrameWork.SysUser();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
recordCount();
bindData();
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void linkDel_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkNewsId = (CheckBox)GridView1.Rows[i].FindControl("chkNewsId"); if (chkNewsId.Checked == true)
{
int newsId = Convert.ToInt32(chkNewsId.Text);
susframe.Delete(newsId);
}
}
bindData();
}
/// <summary>
/// 初始化分页控件总记录数
/// </summary>
/// <returns></returns>
private void recordCount()
{
int count = 0;
string sql = "select count(1) from sys_user";
count = user.GetRecordcount(this.Page, sql);
AspNetPager1.RecordCount = count;
} /// <summary>
/// 数据绑定
/// </summary>
private void bindData()
{
string sql = "select [user_id],[user_name],role_id,case isLock when 1 then '是' else '否' end isLock,last_time,login_time,login_times,dept_id,latest_time,latest_user_id from sys_user";
DataSet ds;
IDataParameter[] param ={ AdoHelper.MakeInParam("@SQL", SqlFieldType.NVarChar, sql),
AdoHelper.MakeInParam("@OrderBy", SqlFieldType.NVarChar, "id"),
AdoHelper.MakeInParam("@startIndex", SqlFieldType.Int, AspNetPager1.StartRecordIndex),
AdoHelper.MakeInParam("@endindex", SqlFieldType.Int, AspNetPager1.EndRecordIndex)};
user.GetDataset(param, out ds);
if (ds.Tables[0].Rows.Count == 0)
{
MessageBox.ShowMsg(Msg.NoRecord);
} GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bindData();
}
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="Small" STYLE="border-collapse:separate" BorderWidth="0px" ForeColor="#333333" CellPadding="2" EnableViewState="False" Width="100%">
请把EnableViewState="False" 设置成 true 或者 不设置默认是true
这个属性巨重要是 在页请求之间保持视图状态...
第二: 你上面的代码还是通过不了。
int newsId = Convert.ToInt32(chkNewsId.Text);???这句Very强大
请修正:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkNewsId = GridView1.Rows[i].FindControl("chkNewsId")as CheckBox;
if (chkNewsId .Checked == true)
{
//获得每行ID
int i = (Convert.ToInt32(this.GridView1.DataKeys[i].Value));
susframe.Delete(i);
}
}
bindData();应该没问题了
敢不敢...敢不敢...折腾到现在...
改成:<asp:CheckBox ID="chkNewsId" runat="server" />看LZ代码 让我想起了小时候的我
谢谢你的回答,可还是不行! 现在的问题是gridview.rows.count =0
就是 你每页10行对吧?
你i<10就行了
除非你第一页勾选了 再翻页勾选第二页 的
再点删除 是删除勾选的还是说
删除当前页的。
binddata();//代表你绑定的数据只在页面第一次载入时执行一次,后面页面回发时不再执行
}
去掉if(!ispostback){}后,直接在page_load中绑定数据,表示每次页面回发都会执行绑定数据