ASP.NET中的ASP.NET编辑和删除问题

时间:2012-11-15 20:57:56

标签: c# asp.net sql-server gridview

我创建了带有插入,编辑和删除功能的GridView。插入工作很好但不是删除和编辑。当我单击“删除”时,它会认为是自动删除行但它永远不会发生,因为它仍然存在。第二个问题对于编辑,我单击编辑,然后我在那里更改某些内容然后单击更新,单击后没有任何更改,并且GridView中的数据仍然相同。我相信我错过了一些小事......请帮忙!

这里是HTML背后的C#代码,

protected void GridViewServer_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewServer.EditIndex = e.NewEditIndex;
        BindGridView();          

    }

    protected void GridViewServer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewServer.EditIndex = -1;
        BindGridView();           
    }

    protected void GridViewServer_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {           
        GridViewRow _row = GridViewServer.Rows[e.RowIndex];
        Label servername = (Label)_row.FindControl("lblServerName");

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("delete from tblServer where ServerName = '" + servername + "'", conn);
        cmd.CommandType = CommandType.Text;
        conn.Open();
        cmd.ExecuteNonQuery();

        GridViewServer.EditIndex = -1;
        BindGridView();
        conn.Close();

    }

    protected void GridViewServer_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {


        GridViewRow _row = GridViewServer.Rows[e.RowIndex];
        Label servername = (Label)_row.FindControl("lblServerName");
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("update tblServer set ClusterName = @ClusterName, Architecture = @Architecture, Database_Type = @DataType, Restriction = @Restriction, Business_Continuity_Plan = @BCP, Tenant_Code = @TenantCode where ClusterName =  '" + servername + "'", conn);
        cmd.CommandType = CommandType.Text;
        //cmd.Parameters.AddWithValue("@ServerName", ((Label)_row.FindControl("lblServerName")).Text);
        cmd.Parameters.AddWithValue("@ClusterName", ((DropDownList)_row.FindControl("DropDownClusterName")).Text);
        cmd.Parameters.AddWithValue("@Architecture", ((DropDownList)_row.FindControl("DropDownArchitecture")).Text);
        cmd.Parameters.AddWithValue("@DataType", ((DropDownList)_row.FindControl("DropDownDataType")).Text);
        cmd.Parameters.AddWithValue("@Restriction", ((DropDownList)_row.FindControl("DropDownRestriction")).Text);
        cmd.Parameters.AddWithValue("@BCP", ((TextBox)_row.FindControl("txtBCP")).Text);
        cmd.Parameters.AddWithValue("@TenantCode", ((TextBox)_row.FindControl("txtTenantCode")).Text);

        conn.Open();
        cmd.ExecuteNonQuery();

        GridViewServer.EditIndex = -1;
        BindGridView();
        conn.Close();

    }

public void BindGridView()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);

        conn.Open();

        string sqlquery = ("SELECT * FROM tblServer");

        SqlCommand command = new SqlCommand(sqlquery, conn);

        SqlDataAdapter adp = new SqlDataAdapter(command);

        DataSet ds = new DataSet();

        adp.Fill(ds);

        GridViewServer.DataSource = ds.Tables[0];

        GridViewServer.DataBind();
    }

如果您需要查看GridView HTML代码,

<asp:GridView ID="GridViewServer" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="ServerName" GridLines="None" ShowFooter="True" onrowcancelingedit="GridViewServer_RowCancelingEdit" 
        onrowdeleting="GridViewServer_RowDeleting" onrowediting="GridViewServer_RowEditing" 
        onrowupdating="GridViewServer_RowUpdating">
        <Columns>               
            <asp:TemplateField HeaderText="ServerName" SortExpression="ServerName">
                <ItemTemplate>
                    <asp:Label ID="lblServerName" runat="server" Text='<%# Eval("ServerName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="txtServerName" runat="server" Text='<%# Eval("ServerName") %>'></asp:Label>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtServerName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvServerName" runat="server" ControlToValidate="txtServerName" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ClusterName" SortExpression="ClusterName">
                <ItemTemplate>
                    <asp:Label ID="lblClusterName" runat="server" Text='<%# Eval("ClusterName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownClusterName" runat="server" AutoPostBack="True" DataSourceID="ClusterName" DataTextField="ClusterName" DataValueField="ClusterName">
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropDownClusterName" runat="server" DataSourceID="ClusterName" DataTextField="ClusterName" DataValueField="ClusterName">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="ClusterName" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ClusterName] FROM [tblCluster]"></asp:SqlDataSource>                       
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Architecture" SortExpression="Architecture">
                <ItemTemplate>
                    <asp:Label ID="lblArchitecture" runat="server" Text='<%# Eval("Architecture") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownArchitecture" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>BSN EAP</asp:ListItem>
                        <asp:ListItem>BSN NEA</asp:ListItem>
                        <asp:ListItem>BSNConnect.com</asp:ListItem>
                        <asp:ListItem>BSNDMZ.COM</asp:ListItem>
                        <asp:ListItem>Dow EAP</asp:ListItem>
                        <asp:ListItem>Dow NEA</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropDownArchitecture" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>BSN EAP</asp:ListItem>
                        <asp:ListItem>BSN NEA</asp:ListItem>
                        <asp:ListItem>BSNConnect.com</asp:ListItem>
                        <asp:ListItem>BSNDMZ.COM</asp:ListItem>
                        <asp:ListItem>Dow EAP</asp:ListItem>
                        <asp:ListItem>Dow NEA</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvArchitecture" runat="server" ControlToValidate="DropDownArchitecture" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Database_Type" SortExpression="Database_Type">
                <ItemTemplate>
                    <asp:Label ID="lblDatabaseType" runat="server" Text='<%# Eval("Database_Type") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownDataType" runat="server" >
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Oracle</asp:ListItem>
                        <asp:ListItem>SQL Server</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:DropDownList ID="DropDownDataType" runat="server" AutoPostBack="True">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Oracle</asp:ListItem>
                        <asp:ListItem>SQL Server</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvDatabaseType" runat="server" ControlToValidate="DropDownDataType" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Life_Cycle_Stage" SortExpression="Life_Cycle_Stage">
                <ItemTemplate>
                    <asp:Label ID="lblLCS" runat="server" Text='<%# Eval("Life_Cycle_Stage") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownLCS" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Dev</asp:ListItem>
                        <asp:ListItem>Prod</asp:ListItem>
                        <asp:ListItem>Test</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:DropDownList ID="DropDownLCS" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Dev</asp:ListItem>
                        <asp:ListItem>Prod</asp:ListItem>
                        <asp:ListItem>Test</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvLCS" runat="server" ControlToValidate="DropDownLCS" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Restriction" SortExpression="Restriction">
                <ItemTemplate>
                    <asp:Label ID="lblRestriction" runat="server" Text='<%# Eval("Restriction") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownRestriction" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Export Controlled</asp:ListItem>
                        <asp:ListItem>SOX Critical</asp:ListItem>
                        <asp:ListItem>Standard</asp:ListItem>
                     </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:DropDownList ID="DropDownRestriction" runat="server">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem>Export Controlled</asp:ListItem>
                        <asp:ListItem>SOX Critical</asp:ListItem>
                        <asp:ListItem>Standard</asp:ListItem>
                     </asp:DropDownList>
                     <asp:RequiredFieldValidator ID="rfvRestriction" runat="server" ControlToValidate="DropDownRestriction" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Business_Continuity_Plan" SortExpression="Business_Continuity_Plan">
                <ItemTemplate>
                    <asp:Label ID="lblBCP" runat="server" Text='<%# Eval("Business_Continuity_Plan") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtBCP" runat="server" Text='<%# Eval("Business_Continuity_Plan") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:TextBox ID="txtBCP" runat="server"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="rfvBCP" runat="server" ControlToValidate="txtBCP" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Tenant_Code" SortExpression="Tenant_Code">
                <ItemTemplate>
                    <asp:Label ID="lblTenantCode" runat="server" Text='<%# Eval("Tenant_Code") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtTenantCode" runat="server" Text='<%# Eval("Tenant_Code") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:TextBox ID="txtTenantCode" runat="server"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="rfvTenantCode" runat="server" ControlToValidate="txtTenantCode" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </FooterTemplate>
             </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" />
            <asp:TemplateField HeaderText="" SortExpression="Insert">                  
                <FooterTemplate>
                    <asp:LinkButton ID="lnkInsert" runat="server" Text ="Insert" OnClick="lnkInsert_Click"></asp:LinkButton>
                    </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <EditRowStyle BackColor="White" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>

1 个答案:

答案 0 :(得分:0)

您使用Label代替Label.Text属性。

所以你正在编辑/删除0行。 代码应该是:

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("delete from tblServer where ServerName = '" + servername.Text + "'", conn);

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("update tblServer set ClusterName = @ClusterName, Architecture = @Architecture, Database_Type = @DataType, Restriction = @Restriction, Business_Continuity_Plan = @BCP, Tenant_Code = @TenantCode where ClusterName =  '" + servername.Text + "'", conn);

注意已修改的servername.Text