如何在gridview

时间:2016-12-20 08:46:16

标签: asp.net vb.net gridview

我希望你能帮助我解决这个问题。我知道这个问题已被问过太多次了,但我搜索到的答案和解决方案对我来说并不适用。

我试图删除gridview中显示的数据库中的记录。

我在gridview中删除了一个图像按钮。当我点击时会出现一个对话框,询问我是否真的要永久删除记录并进入数据库。

这是我的asp代码

<asp:GridView ID="gvUsers" runat="server" AllowPaging="true" 
                    AllowSorting="True" AutoGenerateColumns="False" BorderColor="Silver" 
                    BorderWidth="1px" Width="100%">
                    <RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkEdit" runat="server" 
                                    CommandArgument='<%#Bind("EMPNO")%>' CommandName="EditEmp">
                                <asp:Image ID="imgEdit" runat="server" AlternateText="Edit Employee" 
                                    ImageUrl="eHR_Images/edit.png" />
                                </asp:LinkButton>
                            </ItemTemplate>
                            <HeaderStyle Width="60px" />
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField Visible="true">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkDelete" runat="server" 
                                    CommandArgument='<%#Bind("EMPNO")%>' CommandName="DeleteEmp">
                                <asp:Image ID="imgDelete" runat="server" AlternateText="Delete Employee" 
                                    ImageUrl="eHR_Images/delete.gif" />
                                </asp:LinkButton>
                            </ItemTemplate>
                            <HeaderStyle Width="60px" />
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Empno" SortExpression="EMPNO">
                            <ItemTemplate>
                                <asp:Label ID="lblempno" runat="server" Text='<%#Bind("EMPNO")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="First Name" SortExpression="FIRSTNAME">
                            <ItemTemplate>
                                <asp:Label ID="lblFirstName" runat="server" Text='<%#Bind("FIRSTNAME")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Last Name" SortExpression="LASTNAME">
                            <ItemTemplate>
                                <asp:Label ID="lblLastName" runat="server" Text='<%#Bind("LASTNAME")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Entity" SortExpression="ENTITY">
                            <ItemTemplate>
                                <asp:Label ID="lblEntity" runat="server" Text='<%#Bind("ENTITY")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Location" SortExpression="LOCATION">
                            <ItemTemplate>
                                <asp:Label ID="lblLocation" runat="server" Text='<%#Bind("LOCATION")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
                    <EmptyDataTemplate>
                        <div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
                            No record found.
                        </div>
                    </EmptyDataTemplate>
                    <HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial" 
                        Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
                    <AlternatingRowStyle BackColor="Gainsboro" />
                </asp:GridView>

当我点击“是”按钮时,记录将被删除。

这是btnYes_Click

的代码
 Try
        Dim firstname As String = DirectCast(gvUsers.SelectedRow.FindControl("lblFirstName"), Label).Text
        Dim lastname As String = DirectCast(gvUsers.SelectedRow.FindControl("lblLastName"), Label).Text
        Dim entity As String = DirectCast(gvUsers.SelectedRow.FindControl("lblEntity"), Label).Text
        Dim location As String = DirectCast(gvUsers.SelectedRow.FindControl("lblLocation"), Label).Text

        UpdateInsDelRecord("DELETE FROM emp_mastertbl WHERE empno='" & Me.lblRowVal.Text & "' AND FIRSTNAME = '" & firstname & "' AND LASTNAME = '" & lastname & "' AND ENTITY = '" & entity & "' AND LOCATION '" & location & "';" & _
                           "DELETE FROM empgroup_tbl WHERE SEQID ='" & Me.lblRowVal.Text & "';")

        'MessageBox("alert('Employee successfully deleted.');window.location='eHR_EmpMaintenance.aspx';")
        MessageBox("alert('Employee successfully deleted.');")
        Response.Redirect("eHR_EmpMaintenance.aspx?def_ent2=" + Me.ddlEntity.SelectedValue.ToString() + "&def_loc2=" + Me.ddlLocation.SelectedValue.ToString())
        Me.divDialog.Visible = False
    Catch ex As Exception
        MessageBox("alert('Error occured during deletion.');")
        Throw
    End Try

rowcommand的代码

If e.CommandName = "EditEmp" Then
        Response.Redirect("eHR_EditDetails.aspx?emp_id=" + e.CommandArgument + "&opt=opt_edit")
    ElseIf e.CommandName = "DeleteEmp" Then
        val_e = e.CommandArgument
        Me.divDialog.Visible = True
        Me.lblRowVal.Text = val_e
    End If

我试图删除girdview列表中显示的员工记录 但是出现了错误

  

对象引用未设置为对象的实例。

请帮我解决这个问题。谢谢。

0 个答案:

没有答案