如何通过gridview标题中的复选框检查所有复选框

编程入门 行业动态 更新时间:2024-10-27 23:25:31
本文介绍了如何通过gridview标题中的复选框检查所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我选中gridview标题中的复选框时,检查所有复选框都在gridview的所有页面中自动... 此错误<< 对象引用未设置为对象的实例。 >> 此行的

when i checked the checkbox in the header of gridview checked all checkboxes auto in all pages in gridview ... this error <<Object reference not set to an instance of an object. >> for this line

dt = ds.Tables(0)

我尝试过: 受保护的子CheckAll_CheckedChanged( ByVal sender As Object,ByVal e As System.EventArgs) Dim dt As DataTable = Session(LoadData) Dim ds As DataSet dt = ds.Tables(0) Session(LoadData)= ds.Tables(tabel1) For i = 0 to dt.Rows.Count - 1 如果CType(GridNatRel.Rows(i).FindControl(CheckAll),CheckBox)。检查然后 ElseIf dt.Rows(i).Item( check1)= 1然后 否则 dt.Rows(i).Item(check1)= 0 结束如果 下一页 CHECK1() 结束次级 <<这是子CHECK1()>> 受保护的子CHECK1() Dim dt As DataTable = Session(LoadData) Dim startIndex As Integer = GridNatRel.PageIndex * GridNatRel.PageSize For x = 0 To GridNatRel.Rows.Count - 1 If Convert.IsDBNull(dt.Rows( startIndex + x)。Item(check1))OrElse CInt(dt.Rows(startIndex + x).Item(check1))= 1然后 '如果dt.Rows(startIndex + x).Item(check1)= 1那么 CType(GridNatRel.Rows(x).FindControl(CheckBox1) ),CheckBox).Checked = True Else CType(GridNatRel.Rows(x).FindControl(CheckBox1),CheckBox).Checked = False 结束如果 下一页 结束Sub

What I have tried: Protected Sub CheckAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Dim dt As DataTable = Session("LoadData") Dim ds As DataSet dt = ds.Tables(0) Session("LoadData") = ds.Tables("tabel1") For i = 0 To dt.Rows.Count - 1 If CType(GridNatRel.Rows(i).FindControl("CheckAll"), CheckBox).Checked Then ElseIf dt.Rows(i).Item("check1") = 1 Then Else dt.Rows(i).Item("check1") = 0 End If Next CHECK1() End Sub << and this is sub CHECK1() >> Protected Sub CHECK1() Dim dt As DataTable = Session("LoadData") Dim startIndex As Integer = GridNatRel.PageIndex * GridNatRel.PageSize For x = 0 To GridNatRel.Rows.Count - 1 If Convert.IsDBNull(dt.Rows(startIndex + x).Item("check1")) OrElse CInt(dt.Rows(startIndex + x).Item("check1")) = 1 Then 'If dt.Rows(startIndex + x).Item("check1") = 1 Then CType(GridNatRel.Rows(x).FindControl("CheckBox1"), CheckBox).Checked = True Else CType(GridNatRel.Rows(x).FindControl("CheckBox1"), CheckBox).Checked = False End If Next End Sub

推荐答案

查看您的代码段问题: Look at this code segment from your question: Dim dt As DataTable = Session("LoadData") Dim ds As DataSet dt = ds.Tables(0) Session("LoadData") = ds.Tables("tabel1")

您已声明 dt 并为其指定了值...

You have declared dt and assigned it a value...

Dim dt As DataTable = Session("LoadData")

但是 ds 你只是声明它

Dim ds As DataSet

你实际上并没有给它一个值甚至是s ay

You don't actually give it a value or even say

ds = New DataSet()

所以 ds 目前 Nothing 。它没有桌子。所以,当您点击

So ds is currently Nothing. It has no tables. So when you hit the line

dt = ds.Tables(0)

时,您将收到错误

对象引用未设置为对象的实例。

Object reference not set to an instance of an object.

您没有使用 ds 代码中的任何其他位置。所以只需删除行

You are not using ds anywhere else in your code. So just remove the lines

Dim ds As DataSet dt = ds.Tables(0)

那将是

That is going to leave you with

Dim dt As DataTable = Session("LoadData") Session("LoadData") = ds.Tables("tabel1")

显然会因为我们摆脱 ds 而失败,所以你需要摆脱

Obviously that will fail because we got rid of ds so you need to get rid of

Session("LoadData") = ds.Tables("tabel1")

同样。 另一种选择是实际编写一些代码那个人口tes ds

更多推荐

如何通过gridview标题中的复选框检查所有复选框

本文发布于:2023-11-07 08:59:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   标题   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!