我在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
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
For Each item In dtgOrder.Items
If CType(item.FindControl("chkOrder"), System.Web.UI.WebControls.CheckBox).Checked Then
你要做的事情
End If
Next
* 急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急 *
************************************************************************
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的值。 急~~~这个解决马上加分结帖!!!
* 急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急 *
************************************************************************
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() + ",";
}
}
}
}