【问题标题】:How to access a valuer of GridView row based on checkBox in ASP.NET如何在 ASP.NET 中基于复选框访问 GridView 行的估值器
【发布时间】:2013-02-25 08:41:08
【问题描述】:

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

但现在我无法访问已检查复选框的特定行的值。

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

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

有人知道怎么做吗?

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

这是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

【问题讨论】:

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


【解决方案1】:

在 gridview 中查找选中的行:

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

【讨论】:

  • 谢谢。但是我怎样才能把价值放在数据库中。我必须以某种方式从行中提取 ID 号以将其放入查询中。不是吗?
  • 是的。但是我必须以某种方式从行中提取一个 ID 号才能将其放入查询中。这就是问题
  • 您可以简单地将 hiddenfield 添加到 gridview 中,您可以像复选框一样获取 hiddenfield 的值。将该隐藏字段放在复选框之后。 " /> 然后在代码隐藏中你可以得到这样的 id。 HiddenField id = (HiddenField)row.FindControl("hiddenId");
  • 我们是否将它分配给这段代码后面代码中的 vb/C3 变量:HiddenField id = (HiddenField)row.FindControl("hiddenId");
  • 其实是的,但我不喜欢这个。首先在 Checkbox 下添加 HiddenField。然后在代码隐藏中调用它。 stackoverflow.com/questions/2637330/databinder-eval-in-c-sharp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多