我在CSDN这里搜了一下,发现用/////////////////////////////////////////////////每次刷新页面都重新绑定,所有checkbox的状态没有保存,
If(!Pages.IsPostBack)
{
    //数据绑定
}///////////////////////////////////////////////////是可以取数据了,但在下面的 PagerButtonClick 里就出现页面不对了,比如我有 “ 1  2  3” 这三页,我要是点第“1”页后再点第二页,再点第“3” 页,反过来在点“2”的时候就出现跳到 第“1”页了,而且数据在第“3”页显示的为一半。请问这是怎么回事情?
我的程序是:
在 DataGrid 控件里我运用的模板是:
<asp:TemplateColumn HeaderText="Delete">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox Runat="server" ID="chkDelete"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
在 VB 程序里面我运用到的程序如下:
'---------------------------------   DataGrid 数据绑定  ---------------------------------    Sub BindGrid()        If Not IsPostBack Then            Dim myConnection As New SqlConnection
            myConnection.ConnectionString = "Server=localhost;Database=DB;UID=Sa;PWD="
            Dim ds As DataSet = New DataSet
            Dim selFile As String            selFile = "Select * from T_TEST order by ID"            Dim adapter As SqlDataAdapter = New SqlDataAdapter(selectFile, myConnection)            adapter.Fill(ds, "T_TEST")
            MyDataGrid.DataSource = ds.Tables("T_TEST").DefaultView
            MyDataGrid.DataBind()        End If    End Sub'---------------------------------   DataGrid 数据显示 ---------------------------------    Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)        Dim startIndex As Integer = 0
        startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
        MyDataGrid.CurrentPageIndex = e.NewPageIndex        BindGrid()    End Sub'---------------------------------   页面导航   --------------------------------------    Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)        BindGrid()    End Sub
'---------------------------------   测试按钮  ---------------------------------      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim i As Int32
        Dim cb As CheckBox
        For i = 0 To MyDataGrid.Items.Count - 1
            cb = CType(MyDataGrid.Items(i).Cells(1).FindControl("chkDelete"), CheckBox)
            If cb.Checked = True Then
                Response.Write(MyDataGrid.Items(i).Cells(0).Text)
            Else
                Response.Write("no")
            End If
            Response.Write("<br>")
        Next    End Sub

解决方案 »

  1.   

    Dim item As DataGridItem
                  
                  For Each item In dtgOrder.Items
                If CType(item.FindControl("chkOrder"), System.Web.UI.WebControls.CheckBox).Checked Then
                    你要做的事情
                End If
            Next
      

  2.   

    参考:http://dotnet.aspx.cc/ShowDetail.aspx?id=FF130C7F-3650-4DA6-8943-8AA4AF3E3459
      

  3.   

    既然 Sub BindGrid() 里边要判断 not ispostback,那么这个方法放到事件中就没有作用了(这个方法什么也不做),因为此时必然在页面被提交之后才触发。没有 Handles 的事件是怎么绑定的,用AddEventHandler 吗?StartIndex 没有任何用?PagerButtonClick 没有任何用?最后一个最为关键的:不要去测试CheckBox的Checked状态。写事件(在OnItemDataBound中传给item),然后让Checked事件自己来找你,你不要去找它们。
      

  4.   

    "在OnItemDataBound中传给item"为“OnItemDataBound中传给CheckBox”更准确一点。另外看不出你的程序会造成你开头出现的现象。而且点换页并不是重新装载新页,PageLoad中的not ispostback里边的代码就不能运行了,难道你把换页用重定向实现?你把整个html以及pageLoad事件贴出来。
      

  5.   

    ************************************************************************
    * 急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急 *
    ************************************************************************
    MyDataGrid控件中,我运用了以下CheckBox模板:=====================================================================
    <asp:TemplateColumn HeaderText="Delete">
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
        <ItemTemplate>
        <asp:CheckBox Runat="server" ID="chkDelete"></asp:CheckBox>
        </ItemTemplate>
    </asp:TemplateColumn>
    ======================================================================在vb.net里面,我写了以下程序:======================================================================
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim i As Int32
            Dim cb As CheckBox
            Dim n As Int32 = MyDataGrid.Items.Count - 1        For i = 0 To n            cb = CType(MyDataGrid.Items(i).Cells(1).FindControl("chkDelete"), CheckBox)            If cb.Checked = True Then
                    Response.Write(MyDataGrid.Items(i).Cells(0).Text)
                Else
                    Response.Write("No")
                End If            Response.Write("<br>")        Next======================================================================    但为什么我在MyDataGrid控件里的CheckBox选择几个,在页面上显示的都是“No”?也就是说取不到cb.Checked的值。    急~~~这个解决马上加分结帖!!!
      

  6.   

    我要的值就是 cb.Checked 的True 或 False,还有该行的ID************************************************************************
    * 急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急 *
    ************************************************************************
      

  7.   

    楼主要问的是什么问题,如果要问最上面的那个问题,那你最好不要用数字,用传参数的方法以上页|下页形式分,如果要问下面的问题,那你在Page_load写上If Not IsPostBack Then end IF 的语句就行了
      

  8.   

    这是一个点击BUTTON得到选中的CHECKBOX的行的编号,楼主参考
    foreach(DataGridItem dgi in dgUserInfo.Items)
    {
    for(int i=0;i<dgi.Cells[0].Controls.Count;i++)
    {
    if(dgi.Cells[0].Controls[i].GetType().ToString()=="System.Web.UI.WebControls.CheckBox")
    {
    if(((CheckBox)dgi.Cells[0].Controls[i]).Checked==true)\\判断CheckBox是否选中
    {
    selectedstring += dgUserInfo.DataKeys[dgi.ItemIndex].ToString() + ",";
    }
    }
    }
    }