一个关于datagrid批量删除的问题

iStringTheory 2002-10-26 09:48:55
问题描述:
datagrid分页显示,在第二页选择一些数据后(注:使用的checkbox),点删除返回的是第一页的同行数据,举例如下,
第一页:
口 id=1
口 id=2
口 id=3
第二页
口 id=4
口 id=5
口 id=6
当我选择id=4和id=5两条数据后,返回的却是id=1和id=2两条数据,不知如何解决。请帮忙看看!谢谢!

涉及代码:
前台代码:
<asp:datagrid id="DataList" runat="server" OnPageIndexChanged="change_page_index" EnableViewState="False" PageSize="12" BackColor="White" BorderStyle="None" BorderWidth="0px" BorderColor="#999999" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="False" Width="100%" AllowPaging="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="选定">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<INPUT type=checkbox id=checkbox value='<%# DataBinder.Eval(Container.DataItem,"Data_Id") %>' runat=server NAME="checkbox">
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<%# ShowImg(DataBinder.Eval(Container.DataItem,"img")) %>
<div id="img" runat="server"></div>
</ItemTemplate>
</asp:TemplateColumn>
<asp:HyperLinkColumn DataNavigateUrlField="Data_Id" DataNavigateUrlFormatString="hits.aspx?id={0}" DataTextField="Data_Title" HeaderText="标题">
<HeaderStyle Width="55%"></HeaderStyle>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="Data_Hits" HeaderText="点击数">
<HeaderStyle HorizontalAlign="Center" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="发布日期">
<HeaderStyle HorizontalAlign="Center" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Data_Pbdate", "{0:d}") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="附件">
<HeaderStyle Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<%# ShowAttach(DataBinder.Eval(Container.DataItem,"attach")) %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="管理">
<HeaderStyle HorizontalAlign="Center" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<a href="<%# DataBinder.Eval(Container.DataItem, "Data_id", "newsEdit.aspx?id={0}") %>">
修改</a> | <a href="<%# DataBinder.Eval(Container.DataItem, "Data_id", "Del.aspx?id={0}") %>">
删除</a>-->
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="Control" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
<TABLE borderColor="buttonface" cellSpacing="1" cellPadding="1" width="100%" bgColor="gainsboro" border="0">
<TR>
<TD style="FONT-SIZE: 9pt" align="middle" height="26"><span id="selectall" style="CURSOR: hand" onclick="selectAll()">全部选定</span>
<asp:linkbutton id="Del_Selected" runat="server" EnableViewState="False">删除选定</asp:linkbutton> 
<span style="CURSOR: hand" onclick="javascript:window.navigate('addNews.aspx');">增加新闻</span></TD>
</TR>
</TABLE>

后台代码:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
DataConnect(1)
If Not IsPostBack Then
Try
Sort_bind() '#加载主栏目
newslist_bind(1)
Catch err As Exception
End Try
Else
newslist_bind(1)
End If
DataConnect(0)
End Sub

Sub newslist_bind(ByVal load_type As String)
Dim Ds As New DataSet()
Dim Sql
Dim objAdapter
Try
If load_type = 0 Then
Sql = "SELECT Data_Id,Data_Title,Data_Pbdate,Data_Hits,Topic_id,Sort_id,Column_id,img,attach FROM Info_record_data ORDER BY Data_Id DESC"
Else
Sql = "SELECT Data_Id,Data_Title,Data_Pbdate,Data_Hits,Topic_id,Sort_id,Column_id,img,attach FROM Info_record_data WHERE Column_id=" & columnList.SelectedItem.Value & " ORDER BY Data_Id DESC"
End If
objAdapter = New SqlDataAdapter(Sql, link)
objAdapter.Fill(Ds, "Info_record_data")
'设置DataGrid显示样式
DataList.Style.Add("font-size", "9pt")
DataList.DataSource = Ds.Tables("Info_record_data").DefaultView
DataList.DataBind()

Catch err As Exception

Finally
unloadThis(Ds)
unloadThis(objAdapter)
End Try
End Sub

Private Sub Del_Selected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Del_Selected.Click
'Dim sql As String = "DELETE FROM Info_record_data WHERE Data_Id<>null"
Dim list As ArrayList = New ArrayList()
Dim tmpNewsId As String
Dim i As Integer = 0
For i = 0 To DataList.Items.Count - 1
Dim chkbox As HtmlInputCheckBox = DataList.Items(i).Cells(0).FindControl("checkbox")
If chkbox.Checked Then
Dim chkbox_value = chkbox.Value
tmpNewsId = tmpNewsId & chkbox_value & ","
End If
Next
If Len(Trim(tmpNewsId)) > 1 Then
tmpNewsId = Left(tmpNewsId, Len(tmpNewsId) - 1)
Response.Redirect("del.aspx?id=" & tmpNewsId & "")
End If
End Sub
...全文
38 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
iStringTheory 2002-10-28
  • 打赏
  • 举报
回复
以下是解决后的程序:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls.DataGrid

Public Class newsList
Inherits CommonFunction
Protected WithEvents DataList As System.Web.UI.WebControls.DataGrid
Protected WithEvents sortList As System.Web.UI.WebControls.DropDownList
Protected WithEvents columnList As System.Web.UI.WebControls.DropDownList
Protected WithEvents Del_Selected As System.Web.UI.WebControls.LinkButton
Protected img As System.Web.UI.HtmlControls.HtmlGenericControl
Public Info_record_data
Protected WithEvents tmpValue As System.Web.UI.WebControls.TextBox
Protected WithEvents isChecked As System.Web.UI.WebControls.DropDownList
Public index As Integer = 0

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
DataConnect(1)
If Not IsPostBack Then
Try
Sort_bind() '#加载主栏目
newslist_bind(1)
Catch err As Exception
End Try
Else
If tmpValue.Text <> "" Then
index = CType(tmpValue.Text, Integer)
End If
newslist_bind(1)
End If
DataConnect(0)
End Sub

Sub isChecked_bind()
isChecked.Items.Add("请选择过滤条件")
isChecked.Items.Add("已签发")
isChecked.Items.Add("未签发")
End Sub

Sub newslist_bind(ByVal load_type As String)
Dim Ds As New DataSet()
Dim Sql
Dim objAdapter
Try
If load_type = 0 Then
Sql = "SELECT Data_Id,Data_Title,Data_Pbdate,Data_Hits,Topic_id,Sort_id,Column_id,img,attach FROM Info_record_data ORDER BY Data_Id DESC"
Else
Sql = "SELECT Data_Id,Data_Title,Data_Pbdate,Data_Hits,Topic_id,Sort_id,Column_id,img,attach FROM Info_record_data WHERE Column_id=" & columnList.SelectedItem.Value & " ORDER BY Data_Id DESC"
End If
objAdapter = New SqlDataAdapter(Sql, link)
objAdapter.Fill(Ds, "Info_record_data")
'设置DataGrid显示样式
DataList.Style.Add("font-size", "9pt")
DataList.DataSource = Ds.Tables("Info_record_data").DefaultView
DataList.CurrentPageIndex = index '#重点!翻页后设定新页
DataList.DataBind()
Catch err As Exception

Finally
unloadThis(Ds)
unloadThis(objAdapter)
End Try
End Sub

Sub Sort_bind()
'#为主栏目绑定数据
Dim Ds As New DataSet()
Dim objAdapter As New SqlDataAdapter("SELECT Sort_id,Sort_name FROM webbuild_sort_dict", link)
Try
objAdapter.Fill(Ds, "webbuild_sort_dict")
sortList.DataSource = Ds.Tables("webbuild_sort_dict").DefaultView
sortList.DataTextField = "Sort_name"
sortList.DataValueField = "Sort_id"
sortList.DataBind()
sortList.Items.Add("请选择一级栏目")
sortList.SelectedIndex = sortList.Items.Count - 1
Catch e As Exception
Alert("加载一级栏目时未知错误")
Finally
unloadThis(Ds)
unloadThis(objAdapter)
End Try
End Sub

Sub Column_bind(ByVal sort_id As Integer)
'#为二级栏目绑定数据
Dim Ds As New DataSet()
Dim objAdapter As New SqlDataAdapter("SELECT Column_id,Column_name FROM webbuild_Column_dict WHERE Sort_id=" & sort_id, link)
Try
objAdapter.Fill(Ds, "webbuild_Column_dict")
columnList.DataSource = Ds.Tables("webbuild_Column_dict").DefaultView
columnList.DataTextField = "Column_name"
columnList.DataValueField = "Column_id"
columnList.DataBind()
columnList.Items.Add("请选择二级栏目")
columnList.SelectedIndex = columnList.Items.Count - 1
isChecked_bind()
Catch e As Exception
Alert("加载二级栏目时未知错误")
Response.Write(e.Message)
Finally
unloadThis(Ds)
unloadThis(objAdapter)
End Try
End Sub

Private Sub sortList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sortList.SelectedIndexChanged
Try
DataConnect(1)
Column_bind(sortList.SelectedItem.Value) '#根据一级栏目选定项为二级栏目赋值
Catch err As Exception

Finally
DataConnect(0)
End Try
End Sub

Private Sub columnList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles columnList.SelectedIndexChanged
newslist_bind(1) '#重新绑定数据
End Sub

Sub change_page_index(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Handles DataList.PageIndexChanged
DataList.CurrentPageIndex = e.NewPageIndex '#在此处datagrid控件实现翻页功能
DataList.DataBind()
index = DataList.CurrentPageIndex '#把翻页后的新页数赋值给index变量
tmpValue.Text = index.ToString '#将index变量赋值给一个文本框,以便翻页时传递
End Sub

Private Sub Del_Selected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Del_Selected.Click
Dim list As ArrayList = New ArrayList()
Dim tmpNewsId As String
Dim i As Integer = 0
For i = 0 To DataList.Items.Count - 1
Dim chkbox As HtmlInputCheckBox = DataList.Items(i).Cells(0).FindControl("checkbox")
If chkbox.Checked Then
Dim chkbox_value = chkbox.Value
tmpNewsId = tmpNewsId & chkbox_value & ","
End If
Next
If Len(Trim(tmpNewsId)) > 1 Then
tmpNewsId = Left(tmpNewsId, Len(tmpNewsId) - 1)
Response.Redirect("del.aspx?id=" & tmpNewsId & "")
End If
End Sub

Public Function ShowImg(ByVal s As Integer) As String
If s = 1 Then
ShowImg = "<img src='images/img.gif'>"
ElseIf s = 0 Then
ShowImg = ""
End If
End Function

Public Function ShowAttach(ByVal s As Integer) As String
If s = 1 Then
ShowAttach = "<img src='images/new-attach.gif'>"
ElseIf s = 0 Then
ShowAttach = ""
End If
End Function
End Class
yirenboy 2002-10-26
  • 打赏
  • 举报
回复
to:angel_lee(随风而逝)
meetweb (niky)的思路也可取得的!
angel_lee 2002-10-26
  • 打赏
  • 举报
回复
icyer的方法确实不错
yirenboy 2002-10-26
  • 打赏
  • 举报
回复
To:abigfrog (☆★千年精灵★☆)
我刚才查到对你非常有帮助的帖子: http://expert.csdn.net/Expert/topic/874/874387.xml?temp=.9264643

如果方法可以的话告诉我一下,以后我也采用,谢谢!
liqilinlove 2002-10-26
  • 打赏
  • 举报
回复
从数据库中取值应该连同id取出,然后你可以把datakeyfield属性设置为id,
或者用datagrid.item(index).cell(0).text找到被(cell(0)中的0是我默认你把id列设置为第一列)checked的行的id,再进行删除。

另外对你提个建议,
Try
Sort_bind() '#加载主栏目
newslist_bind(1)
Catch err As Exception
End Try
Else
newslist_bind(1)
End If
这段代码最好用函数封装起来,我开发项目的时候绝对不会写这样的重复代码到page_load事件中。
iStringTheory 2002-10-26
  • 打赏
  • 举报
回复
bolar()
不行,问题依旧
请问datakeys怎么用的?
bolar 2002-10-26
  • 打赏
  • 举报
回复
试试将
Dim chkbox As HtmlInputCheckBox = DataList.Items(i).Cells(0).FindControl("checkbox")
改为
Dim chkbox As HtmlInputCheckBox = DataList.Items(i+DataList.CurrentPageIndex*DataList.PageSize).Cells(0).FindControl("checkbox")
iStringTheory 2002-10-26
  • 打赏
  • 举报
回复
我是在前台通过<script language="javascript">
function selectAll()
{
var coll = document.all.tags("input");
if (coll!=null)
var i=0;
for (var j=0;j<coll.length;j++)
{
if(coll[j].type=="checkbox")
{
if(coll[j].name.substr(0,8)=="DataList")
{
if(coll[j].checked==true)
{
coll[j].checked=false
document.all.selectall.innerText="全部选定"
}
else
{
coll[j].checked=true
document.all.selectall.innerText="取消选择"
}
}
}
}
}

function delete_confirm(e) {
if (event.srcElement.outerText=="删除")
event.returnValue=confirm("确认删除否?");
}
document.onclick=delete_confirm;
</script>
在客户端进行数据选择的,请问这样没有问题吧?
iStringTheory 2002-10-26
  • 打赏
  • 举报
回复
还是不大明白,可不可以详细一些,或者有代码说明?谢谢!解决马上结贴!
yirenboy 2002-10-26
  • 打赏
  • 举报
回复
觉得 liqilinlove(MasterCSharp) 说得有道理,用DataKeyField值来判断
up一下!
liqilinlove 2002-10-26
  • 打赏
  • 举报
回复
说明你没有根据当前页来判断,如果用判断当前页的方法比较复杂。
你也可以用datagrid.datekeys(id)
id 应该是你取出数据的id号,在属性生成器中把“可见”去掉
heqinchen 2002-10-26
  • 打赏
  • 举报
回复
关注!
visualcpu 2002-10-26
  • 打赏
  • 举报
回复
mark
yirenboy 2002-10-26
  • 打赏
  • 举报
回复
其实你可以参考这个帖子:
http://expert.csdn.net/Expert/topic/874/874387.xml?temp=.9264643
里面已经很清楚讲得很清楚。
-------------------------------------------
在那个帖子里
for (int i = 0; i < DLPolice.Items.Count; i++)
{
chk = (CheckBox)DLPolice.Items[i].FindControl("chkSelection");
...
}
取得CheckBox是否被选中。
如果被选中的话,也可通过上面的方法取得lable的值
labValue = (Lable)DLPolice.Items[i].FindControl("policeid");这就是数据库绑定在lable的值。
iStringTheory 2002-10-26
  • 打赏
  • 举报
回复
我如何通过checkbox取得label的值呢?
yirenboy 2002-10-26
  • 打赏
  • 举报
回复
lable是隐藏的是用来存放DataGrid每一行的在数据库所对应的那条字段!
所以返回的时候就知道对哪条数据进行操作!
iStringTheory 2002-10-26
  • 打赏
  • 举报
回复
还是不大明白,我点击checkbox和隐藏lable有什么关系?请明示!谢谢!
liqilinlove 2002-10-26
  • 打赏
  • 举报
回复
呵呵,解决了就好,绑一个不可见的列也就是我的id列隐藏的相同意思:)
yirenboy 2002-10-26
  • 打赏
  • 举报
回复
To: abigfrog (☆★千年精灵★☆)
在DataGrid的每一行绑定一个不可见的Lable(可以是数据库的主键),这样就不管分页的麻烦了.
另外你也不需用javascript的脚本了,我觉得后台处理比这方便!

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧