【问题标题】:ASP.NET Page does not refresh after running a DELETE SQL Query运行 DELETE SQL 查询后 ASP.NET 页面不刷新
【发布时间】:2011-11-17 10:04:22
【问题描述】:

我正在使用 VS2005。目前,我的 gridview 中有一个删除链接按钮,如果我按下它来删除一行,GridView 就会变空,我将不得不再次单击该链接才能访问该页面。

以下是我的 .aspx 代码:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" ondatabound="gv_DataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
                         <asp:TemplateField>
                            <ItemTemplate>

                              <asp:CheckBox ID="UserSelection" OnCheckedChanged="UserSelector_CheckedChanged" runat="server" />
                                 <asp:LinkButton ID="lnkDelete" runat="server" onclick="lnkDelete_Click" Text="Delete" CommandArgument='<%# Eval("Employee") %>' ></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
    </asp:GridView>

下面是我的 .cs 代码

protected void lnkDelete_Click(object sender, EventArgs e)
    {
        LinkButton lnk = (LinkButton)sender;
        string stid = lnk.CommandArgument.ToString();
        SqlDataSource1.SelectCommand = "DELETE FROM [UserDB]where Employee like '%"+stid+"%'";
        SqlDataSource1.DataBind();

    }

我已尝试将我的删除查询分配给 SqlDataSource1 并将选择查询分配给 source2,但如果是这种情况,我的删除查询将不起作用。

我也尝试过在我的 PageLoad 方法上使用 IsPostBack,但在单击删除按钮后 GridView 也会变空。

目前我的两个查询都分配给 SqlDataSource1,一旦查询被删除,页面就会变成空白,尽管查询已被删除。

我可以知道在删除查询运行后如何刷新页面或重新加载 GridView 表吗?

谢谢。


感谢各位的帮助,问题解决了。目前我的工作代码如下:

LinkButton lnk = (LinkButton)sender; 
    string stid = lnk.CommandArgument.ToString();
    SqlConnection conn = new SqlConnection("DATA-SOURCE");
    string sql = string.Format("DELETE FROM [UserDB] where Employee like '%{0}%'",stid);
    SqlCommand cmd = new SqlCommand(sql,conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    SqlDataSource1.SelectCommand = "SELECT * FROM [UserDB]";
    SqlDataSource1.DataBind();
    Response.Redirect("/Project/UserList.aspx");

我使用sql连接字符串进行删除查询,然后使用sqldatasource进行选择查询,最后使用response.redirect刷新了页面。希望它对像我这样也遇到此错误的新手有所帮助。

非常感谢所有提供帮助的人

【问题讨论】:

  • 尝试将 autopostback=true 放入您的删除按钮标记代码中。
  • 我猜你可以做一个 gridview.DataBind() 而不是 sqlDatasource.databind。 gridviews 数据绑定在内部执行 sqldatasource 数据绑定并刷新 gridview。

标签: c# asp.net sql visual-studio


【解决方案1】:

为什么要将DELETE 命令分配给SelectCommand 属性?

只需在带有ExecuteNonQuery 的单独SqlCommand 实例上运行DELETE 并提交即可。另一种选择是使用现有SqlDataSource 中的DeleteCommand

还有一点:

构建DELETE SQL 的方式容易受到称为 SQL 注入的严重安全问题的影响 - 避免这种情况的最简单和最好的方法是使用绑定参数!

【讨论】:

  • 在单独的实例上意味着什么?
  • @RUiHAO 只需创建一个 SqlCommand,为其分配 SQL 和参数等 - 不要使用 SqlDataSource 来尝试执行您的 DELETE。
  • @RUiHAO 另一种选择是使用现有SqlDataSource 中的DeleteCommand
【解决方案2】:

首先,您应该了解 ASP.NET 中的数据绑定可以为您做什么。 (那么您就不必为此场景编写任何代码隐藏。)

示例:(未在 VS 中测试)

您应该使用 SqlDataSource 的 DeleteCommand 来设置删除命令及其参数,如下所示:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..."
        SelectCommand="..."
        DeleteCommand="DELETE FROM [UserDB] WHERE Employee=@Employee">
    <DeleteParameters>
        <asp:Parameter Name="Employee" />
    </DeleteParameters>
</asp:SqlDataSource>

在 GridView 上,您应该设置 DataKeyNames 属性,并在 ItemTemplate 的 Linkbutton 内将 CommandName 属性设置为“Delete”,如下所示:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
        ondatabound="gv_DataBound" DataKeyNames="Employee"
        OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="UserSelection" runat="server"
                        OnCheckedChanged="UserSelector_CheckedChanged" />
                <asp:LinkButton ID="lnkDelete" runat="server"
                        onclick="lnkDelete_Click" Text="Delete"
                        CommandArgument='<%# Eval("Employee") %>'
                        CommandName="Delete" ></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

【讨论】:

    【解决方案3】:

    这样试试

    protected void lnkDelete_Click(object sender, EventArgs e)  
    { 
            LinkButton lnk = (LinkButton)sender; 
            string stid = lnk.CommandArgument.ToString(); 
            SqlConnection conn = new SqlConnection(<put your connectionstring here>);
            string sql = string.Format("DELETE FROM [UserDB] where Employee like '%{0}%'",stid);
            SqlCommand cmd = new SqlCommand(sql,conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();   
            BindTheGridView();
    
                //or you can use SelectCommand of your SqlDataSource1 and can bind again simply.
    } 
    

    编辑:

    public void BindTheGridView()
        {
            SqlConnection conn = new SqlConnection(Connstring);
            string sql = "Select * from [UserDB]";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtb = new DataTable();
            da.Fill(dtb);
            conn.Close();
            SqlDataSource1.DataSource = dtb;
            SqlDataSource1.DataBind();
        }
    

    您可以随时调用它来绑定值。

    【讨论】:

    • 我没有投反对票...但是您发布的代码有一些怪癖-例如,它容易受到 SQL 注入的攻击,并且conn 变量是凭空引用的...
    • 嗨,skk。我了解您的代码在做什么。即使我看到 SqlDataSource1.DataBind(); ,我也不知道为什么我的 GridView 在删除后不刷新。运行删除查询后应该刷新它。
    • @skk GridView 仍然没有刷新
    • 您忘记在 DataBind 之前设置 DataSource。
    • @Ofer 你的意思是我应该插入 SqlDataSource1.SelectCommand = "SELECT * FROM [UserDB]";在数据绑定之前?
    【解决方案4】:

    SelectCommand 运行更改操作(在您的示例中为DELETE)是错误的。 SelectCommand 必须按原样使用 - 仅限于 SELECTs。

    单独运行DELETE 查询,然后运行SELECT 命令以显示网格的相关记录。

    【讨论】:

      【解决方案5】:

      SqlDataSource1.SelectCommand = "DELETE FROM [UserDB]where Employee like '%"+stid+"%'"; SqlDataSource1.DataBind();

      为什么要在 SelectCommand 中使用 Delete 查询。该查询对 SqlInjection 攻击开放。在将参数传递给过程之前,我会使用存储过程并验证参数。

      此外,网格不会显示/绑定任何数据,因为您的代码不会返回任何数据。它只是删除数据。

      【讨论】:

        【解决方案6】:

        我猜你可以用 gridview.DataBind() 代替 sqlDatasource.databind。 gridviews 数据绑定在内部执行 sqldatasource 数据绑定并刷新 gridview。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-23
          • 1970-01-01
          • 2010-10-19
          • 2013-04-10
          相关资源
          最近更新 更多