Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何从GridView中的隐藏列检索数据?VB_Asp.net_Vb.net_Gridview - Fatal编程技术网

Asp.net 如何从GridView中的隐藏列检索数据?VB

Asp.net 如何从GridView中的隐藏列检索数据?VB,asp.net,vb.net,gridview,Asp.net,Vb.net,Gridview,我有点被困在这里,在做了一些研究之后,我似乎找不到答案。无论如何,我不知道如何在Gridview隐藏列中检索主键 这是我的网格视图 <asp:GridView ID="grdDent" runat="server" AutoGenerateColumns="False" Width="852px" DataKeyNames="app_id"> <Columns> <asp:Tem

我有点被困在这里,在做了一些研究之后,我似乎找不到答案。无论如何,我不知道如何在Gridview隐藏列中检索主键

这是我的网格视图

<asp:GridView ID="grdDent" runat="server" AutoGenerateColumns="False" Width="852px" DataKeyNames="app_id">
                    <Columns>
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:CheckBox ID="chkApp" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkApp" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="app_id" HeaderText="app_id" Visible="False" />
                        <asp:BoundField DataField="app_date" HeaderText="Date" SortExpression="Date" />
                        <asp:BoundField DataField="app_time" HeaderText="Time" SortExpression="Time" />
                        <asp:BoundField DataField="app_rsn" HeaderText="Reason" SortExpression="Reason" />
                        <asp:BoundField DataField="app_info" HeaderText="Comment" SortExpression="Comment" />
                        <asp:BoundField DataField="app_sts" HeaderText="Status" SortExpression="Status" />
                        <asp:BoundField DataField="Patient" HeaderText="Requested by" SortExpression="Patient" />
                        <asp:BoundField DataField="app_timestamp" HeaderText="Date requested" SortExpression="Date requested" />
                    </Columns>
                </asp:GridView>
这是我填充gridview的代码

Using cmd = New MySqlCommand("SELECT app_id, app_date, app_time, app_rsn, app_info, app_sts, group_concat(pat_lname, pat_fname) As Patient, app_timestamp FROM appointment_table INNER JOIN patient_table WHERE app_sts = 'Queue'", con)
            con.Open()
            Dim ds As New DataSet()
            Dim a As New MySqlDataAdapter(cmd)
            a.Fill(ds)
            grdDent.DataSource = ds
            grdDent.DataBind()
            con.Close()
        End Using

现在的问题是,每当我单击该按钮时,它都不会执行任何操作,甚至不会显示错误,因此我不知道该怎么办。

您可以使用以下方法:

For Each row As GridViewRow In yourgrid.Rows
   Dim chk As CheckBox = CType(row.FindControl("chkApp"), CheckBox)
   If chk IsNot Nothing And chk.Checked Then
      'get your selected row data here and perform update operation
End If
Next row
注意:我的建议是首先调试代码,确保代码工作正常,方法调用完美

您可以查看以获取更多详细信息


希望它能帮助你

首先调试代码并检查从哪一行获得错误

还可以尝试以下代码。 HideData先生 { 显示:无; }

按如下所示替换应用程序id绑定字段。


不要使用visible false属性。

当您设置
visible=false
时,该列将不会在gridview的html中呈现。 因此,不要使用
visible=false
使其
style='display:none'

这样使用:

  <asp:BoundField DataField="ItemDesc" HeaderText="app_id"  >
                 <ItemStyle CssClass="hidden"/>

                 </asp:BoundField>



<style type="text/css">
     .hidden
     {
         display:none;
     }
</style>

我看到您将主键存储在DataKeyNames=“app\u id”中 所以你可以在代码中用

grdDent.DataKeys.Item(a row index here).Value

可以删除包含此值的边界字段。“Visible=False”不会保存任何值。

我这样做的方式是

首先删除您的边界字段,只需向其中一个模板字段添加标签,如下所示

<asp:GridView ID="grdDent" runat="server" AutoGenerateColumns="False" Width="852px" DataKeyNames="app_id">
                <Columns>
                    <asp:TemplateField>
                        <EditItemTemplate>
                            <asp:CheckBox ID="chkApp" runat="server" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkApp" runat="server" />
                            <asp:Label ID="lblApp_ID" Visible="False" runat="server" Text='<%# Bind("app_id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="app_date" HeaderText="Date" SortExpression="Date" />
                    <asp:BoundField DataField="app_time" HeaderText="Time" SortExpression="Time" />
                    <asp:BoundField DataField="app_rsn" HeaderText="Reason" SortExpression="Reason" />
                    <asp:BoundField DataField="app_info" HeaderText="Comment" SortExpression="Comment" />
                    <asp:BoundField DataField="app_sts" HeaderText="Status" SortExpression="Status" />
                    <asp:BoundField DataField="Patient" HeaderText="Requested by" SortExpression="Patient" />
                    <asp:BoundField DataField="app_timestamp" HeaderText="Date requested" SortExpression="Date requested" />
                </Columns>
            </asp:GridView>

通过这样做,它将把标签放在每一行中,但它不会显示给用户和将文本绑定到每一行标签上的“
Text=”
”部分

现在,对于按钮上的代码,请单击您应该使用以下命令:

Dim i As Integer = 0
    Dim app_ID As Integer '<- I was using an integer but you can use any datatype
    For Each row As GridViewRow In grdDent.Rows
        If CType(row.FindControl("chkApp"), CheckBox).Checked = True Then
            app_ID = CType(grdDent.Rows(i).FindControl("lblApp_ID"), Label).Text
            cmd = New MySqlCommand("UPDATE appointment_table SET app_sts = 'Approved' WHERE app_id =" & app_ID , con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End If
        i += 1
    Next
Dim i作为整数=0

Dim app_ID为整数“您调试了吗?单击按钮时代码是否运行不正常?我的意思是,如果chkApp.Checked,它是否满足条件?我满足了,当我单击按钮时,代码没有运行。确定,在单击按钮之前,您的GridView是否加载了数据?即,检查grdDent.Rows.Count>0I是否正在使用Visual Studio Web 2013,并在调试时设置了解决方案配置。尝试了相同的代码。您确定在调试之前设置了断点吗?还可以使用asp.net按钮更新您的问题,以及单击事件的整个代码隐藏代码,这样我可以确定并检查是否存在任何问题。
<asp:GridView ID="grdDent" runat="server" AutoGenerateColumns="False" Width="852px" DataKeyNames="app_id">
                <Columns>
                    <asp:TemplateField>
                        <EditItemTemplate>
                            <asp:CheckBox ID="chkApp" runat="server" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkApp" runat="server" />
                            <asp:Label ID="lblApp_ID" Visible="False" runat="server" Text='<%# Bind("app_id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="app_date" HeaderText="Date" SortExpression="Date" />
                    <asp:BoundField DataField="app_time" HeaderText="Time" SortExpression="Time" />
                    <asp:BoundField DataField="app_rsn" HeaderText="Reason" SortExpression="Reason" />
                    <asp:BoundField DataField="app_info" HeaderText="Comment" SortExpression="Comment" />
                    <asp:BoundField DataField="app_sts" HeaderText="Status" SortExpression="Status" />
                    <asp:BoundField DataField="Patient" HeaderText="Requested by" SortExpression="Patient" />
                    <asp:BoundField DataField="app_timestamp" HeaderText="Date requested" SortExpression="Date requested" />
                </Columns>
            </asp:GridView>
Dim i As Integer = 0
    Dim app_ID As Integer '<- I was using an integer but you can use any datatype
    For Each row As GridViewRow In grdDent.Rows
        If CType(row.FindControl("chkApp"), CheckBox).Checked = True Then
            app_ID = CType(grdDent.Rows(i).FindControl("lblApp_ID"), Label).Text
            cmd = New MySqlCommand("UPDATE appointment_table SET app_sts = 'Approved' WHERE app_id =" & app_ID , con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End If
        i += 1
    Next