急!急!datagrid控件绑定记录最后一条丢失问题
我用datagrid控件绑定数据库中的记录,点最后一页时无内容,也无删除按钮,但是在附近点时,容易造成最后一条记录被删除,我添加了删除确认按钮,也不能解决此问题?请问是什么原因造成的?怎么解决?后台代码:
'每绑定一行,将会触发该事件过程,在其中将给删除按钮添加JavaScript事件
Sub htqp_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
'这个判断语句表示,只有对于数据行才执行,对于标题栏和脚注栏则不执行
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'下面找到删除按钮控件,它其实是一个LinkButton控件
Dim lbtnDel As LinkButton '定义一个LinkButton控件
lbtnDel = e.Item.Cells(4).Controls(0) '它位于第0列第0个控件
'下面添加JavaScript事件
lbtnDel.Attributes.Add("onclick", "return confirm('您真的要删除 " & e.Item.DataItem("bzzrm_mc") & " 吗?');")
End If
End Sub
Sub dg_htqp_delete(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
'取得登陆用户的用户类型
Dim stryhlx As String
stryhlx = Session("yhlx")
'If Session("yhm") = "admin" Then
strsql = "delete from tab_bzzrm where id=" & dg_htqp.DataKeys(CInt(e.Item.ItemIndex))
Dim cmd As New OleDbCommand(strsql, conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
dg_htqp.EditItemIndex = -1
Call mydatabind_htqp()
'Else
'Response.Write("<script language='javascript'>alert('对不起,您无删除权限。');</" & "script>")
'End If
End Sub
前台代码:
<asp:DataGrid ID="dg_htqp" OnItemDataBound="htqp_ItemDataBound" Font-Size="14px" Width="100%" AutoGenerateColumns="false" AllowPaging="true" AllowCustomPaging="true" DataKeyField="id" PageSize="50" OnPageIndexChanged="dg_htqp_page" OnDeleteCommand="dg_htqp_delete" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<img id="Img1" alt="~/img/jt_green.jpg" src="~/img/jt_green.jpg" runat="server" visible='<%# IIF (container.dataitem("beian")="", True, False) %>' /><img id="Img2" src="~/img/jt_red.jpg" alt="~/img/jt_green.jpg" runat="server" visible='<%# IIF (container.dataitem("beian")<>"", True, False) %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="班组长任免">
<HeaderStyle Width="400px" />
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Text='<%# container.dataitem("bzzrm_mc") %>' NavigateUrl='<%# "bzzrm_lct.aspx?id=" & container.dataitem("id") %>' Target="_blank" runat="server" >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="时间" DataField="bzzrm_date" DataFormatString="{0:d}">
<HeaderStyle Width="120px" />
</asp:BoundColumn>
<asp:BoundColumn HeaderText="单位" DataField="bzzrm_dw">
<HeaderStyle Width="120px" />
</asp:BoundColumn>
<asp:ButtonColumn Text="删除" CommandName="delete" ItemStyle-Wrap="false" />
</Columns>
</asp:DataGrid>
--------------------编程问答-------------------- --------------------编程问答-------------------- 关键地方应该在绑定列表和分页的地方,以上代码没发现有什么问题 --------------------编程问答-------------------- 删除的地方加断点调试 --------------------编程问答--------------------
分页代码如下:
'分页功能数据绑定--------------------编程问答-------------------- VB真心不懂 --------------------编程问答--------------------
Sub mydatabind_htqp()
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
If stryhjb = "cjgly" Then
strsql = "select * from tab_bzzrm order by id desc"
Else
strsql = "select * from tab_bzzrm where bzzrm_yhm= '" & stryhm & "' order by id desc"
End If
Dim adp As New OleDbDataAdapter(strsql, conn)
Dim ds As New DataSet()
Dim intstart As Long = dg_htqp.CurrentPageIndex * dg_htqp.PageSize
adp.Fill(ds, intstart, dg_htqp.PageSize, "tab_bzzrm")
dg_htqp.VirtualItemCount = counttotal()
dg_htqp.DataSource = ds.Tables("tab_bzzrm").DefaultView
dg_htqp.DataBind()
End Sub
'计算总页数
Function counttotal()
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
If stryhjb = "cjgly" Then
strsql = "select * from tab_bzzrm order by id desc"
Else
strsql = "select * from tab_bzzrm where bzzrm_yhm= '" & stryhm & "' order by id desc"
End If
Dim cmd As New OleDbCommand(strsql, conn)
conn.Open()
counttotal = cmd.ExecuteScalar()
conn.Close()
End Function
'If Session("yhm") = "admin" Then
strsql = "delete from tab_bzzrm where id=" & dg_htqp.DataKeys(CInt(e.Item.ItemIndex))
我只看到这边就知道为什么会出现楼主说的问题了 --------------------编程问答--------------------
那是为什么呢?请指教 --------------------编程问答-------------------- 大侠们,帮看看啊 --------------------编程问答-------------------- 设置断点看看strsql = "delete from tab_bzzrm where id=" & dg_htqp.DataKeys(CInt(e.Item.ItemIndex))
strsql的SQL语句
datagrid过时了 --------------------编程问答-------------------- private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
。。。。
cmd.Parameters.Add( "@id ",SqlDbType.VarChar,50);
cmd.Parameters[ "@id"].Value=DataGrid1.DataKeys[e.Item.ItemIndex];
--------------------编程问答-------------------- 也许我的意思没表达清楚!
点最后一页,没有任何记录,也没删除按钮,只是在最后一页页码处随便点,有时候就能删除最后的记录! --------------------编程问答-------------------- 没人帮忙啊 --------------------编程问答-------------------- LZ你问题的点可能不在LS这么多大牛的讨论范围之内 --------------------编程问答-------------------- dg_htqp_page是怎么写的 --------------------编程问答--------------------
End Sub--------------------编程问答-------------------- 我在删除事件了设置了断点,发现点分页时触发了删除事件!
'单击导航栏的页码时执行该过程
Sub dg_htqp_page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
dg_htqp.CurrentPageIndex = e.NewPageIndex
Call mydatabind_htqp()
End Sub
'分页功能数据绑定
Sub mydatabind_htqp()
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
If stryhjb = "cjgly" Then
strsql = "select * from tab_bzzrm order by id desc"
Else
strsql = "select * from tab_bzzrm where bzzrm_yhm= '" & stryhm & "' order by id desc"
End If
Dim adp As New OleDbDataAdapter(strsql, conn)
Dim ds As New DataSet()
Dim intstart As Long = dg_htqp.CurrentPageIndex * dg_htqp.PageSize
adp.Fill(ds, intstart, dg_htqp.PageSize, "tab_bzzrm")
dg_htqp.VirtualItemCount = counttotal()
dg_htqp.DataSource = ds.Tables("tab_bzzrm").DefaultView
dg_htqp.DataBind()
End Sub
点分页是为什么触发删除事件呢
谁给我解释下!谢谢 --------------------编程问答--------------------
你检查一下你写的分页和删除是不是把名字搞错了,有可能是复制原有代码时名字忘了没改 --------------------编程问答-------------------- 在计算总页数counttotal里面,应该用count(*)吧:
strsql = "select count(*) from tab_bzzrm order by id desc"
strsql = "select count(*) from tab_bzzrm where bzzrm_yhm= '" & stryhm & "' order by id desc"
--------------------编程问答-------------------- 我试过了跟这个没关系 --------------------编程问答--------------------
补充:.NET技术 , ASP.NET