如何基于ASP.NET中的checkBox访问GridView行的值

时间:2013-02-25 08:41:08

标签: asp.net mysql asp.net-mvc vb.net

您好我有一个包含多行和多列的GridView。我还在网格视图中添加了一个复选框..

但是现在我遇到了访问检查了checkBox的特定行的值的麻烦。

因为按下按钮我想将一列的值从未注册更改为已注册。

另一个按钮应该将选中行的帐户ID转发到另一个页面,其中该条目的所有详细信息都将被输出。

有谁知道怎么做?

这是我正在使用的代码段:

Tis是GridView和Button的代码:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" Width="1500px">
        <Columns>

                    <asp:TemplateField >
                        <ItemTemplate>
                            <asp:CheckBox ID="myCheckBox" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>

              </Columns>
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>


     <asp:Button ID="DetailsBtn" runat="server" Text="See Details" />
     &nbsp; &nbsp; &nbsp; &nbsp;
     <asp:Button ID="RegBtn" runat="server" Text="Mark Registered" />

以下是填充GridView的代码

Try
        myconn.Open()
        Dim sqlstring As String = "SELECT a.account_id AS 'No', a.accountid_number .BLA BLA BLA"
        Dim smd As MySqlCommand
        smd = New MySqlCommand(sqlstring, myconn)
        smd.CommandType = CommandType.Text

        Dim da As New MySqlDataAdapter(smd)
        Dim cb As New MySqlCommandBuilder(da)
        Dim ds As New DataSet()
        da.Fill(ds)

        GridView1.DataSource = ds.Tables(0)
        GridView1.DataBind()

        myconn.Close()
    Catch ex As Exception
        'System.Diagnostics.Debug.WriteLine(ex.ToString())
        Dim exmess As String = "alert('" & ex.Message.ToString() & "')"
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", exmess, True)
        myconn.Close()
    End Try

我该怎么办这个按钮?

Protected Sub RegBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RegBtn.Click

End Sub

1 个答案:

答案 0 :(得分:2)

在gridview中查找已检查的行:

foreach (DataGridViewRow row in GridView1.Rows)
{
 Checkbox cbox = (Checkbox)row.FindControl("myCheckBox");
 if(cbox.Checked)
 {
   // do your stuff ...
 }
 else
 { // do your other stuff ... }
}