GridView RowUpdating SqlDataSource.Update 来自 CodeBehind

发布于 2024-12-08 16:56:37 字数 440 浏览 0 评论 0原文

因此,我在 OnRowUpdating 事件期间在 Gridview 中进行更新时遇到问题。

我想做的是在 SqlDataSource 中设置 UpdateCommand,然后使用该命令进行更新。该事件正常触发,但是当事件完成后,该行似乎永远不会更新。

C#:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlDataSource1.UpdateCommand = "UPDATE A_Table SET Something = @AValue WHERE ID = " + e.RowIndex;

    SqlDataSource1.Update();
}

编辑:重写我的示例更新命令...它实际上是更新命令,而不是选择命令哈哈。

So I am having an issue with doing an update in a Gridview during an OnRowUpdating event.

What I am trying to do is set the UpdateCommand in an SqlDataSource then update using that command. The event is firing okay, but when the event is done it appears that the row never updates.

C#:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlDataSource1.UpdateCommand = "UPDATE A_Table SET Something = @AValue WHERE ID = " + e.RowIndex;

    SqlDataSource1.Update();
}

Edit: Re-wrote my example update command...it is really an update command, not a select command haha.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把回忆走一遍 2024-12-15 16:56:37

这里的其他答案非常正确地指出了 UpdateCommand 的问题。

正如我在前面的评论中提到的,我只是指出这比你那里的要容易一些。当将 GridViewSQLDataSource 一起使用时,只要您设置正确,很多工作都会为您完成。

首先,在 SQLDataSource 的标记中定义 UpdateCommand,如下所示:(

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="Your Connection String Here" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT ColPK, ColA, ColB, ColC FROM Table_Name" 
    UpdateCommand="UPDATE Table_Name 
                   SET ColA=@ColA,  ColB=@ColB, ColC=@ColC, 
                   WHERE ColPK=@ColPK">
</asp:SqlDataSource>

ColPK 将是您所在表的主键更新)

然后,您可以在 GridView 标记中将“AutoGenerateEditButton”设置为 true,然后,噗!您可以更新 GridView(无需在后面的代码中执行任何操作)。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    AutoGenerateEditButton="True" DataKeyNames="ColPK" 
    DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="ColPK" HeaderText="Column PK" 
            SortExpression="ColA" ReadOnly="True" />
        <asp:BoundField DataField="ColA" HeaderText="Column A" 
            SortExpression="ColA" />
        <asp:BoundField DataField="ColB" HeaderText="Column B" 
            SortExpression="ColB" />
        <asp:BoundField DataField="ColC" HeaderText="Column C" 
            SortExpression="ColC" />
    </Columns>
</asp:GridView>

现在,如果您需要进行额外的处理,或者根据某些逻辑取消 Update 等,您仍然可以在代码中处理 OnRowUpdating 事件。但是基本的 Update 功能几乎是免费的!

The other answers here are VERY correct in pointing out the issue with your UpdateCommand.

As I mentioned in the comment earlier, I'll just point out that this is a bit easier than what you have there. When using a GridView with a SQLDataSource, alot of the work is done for you as long as you set up correctly.

First off, define your UpdateCommand in the markup for the SQLDataSource, like this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="Your Connection String Here" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT ColPK, ColA, ColB, ColC FROM Table_Name" 
    UpdateCommand="UPDATE Table_Name 
                   SET ColA=@ColA,  ColB=@ColB, ColC=@ColC, 
                   WHERE ColPK=@ColPK">
</asp:SqlDataSource>

(ColPK would be the primary key of the table you're updating)

Then, you can set "AutoGenerateEditButton" to true in your GridView markup and, poof! You can update the GridView (without having to do anything in code behind).

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    AutoGenerateEditButton="True" DataKeyNames="ColPK" 
    DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="ColPK" HeaderText="Column PK" 
            SortExpression="ColA" ReadOnly="True" />
        <asp:BoundField DataField="ColA" HeaderText="Column A" 
            SortExpression="ColA" />
        <asp:BoundField DataField="ColB" HeaderText="Column B" 
            SortExpression="ColB" />
        <asp:BoundField DataField="ColC" HeaderText="Column C" 
            SortExpression="ColC" />
    </Columns>
</asp:GridView>

Now, you can still handle the OnRowUpdating event in your code if you need to do extra processing, or cancel the Update based on some logic, etc. But the basic Update functionality is pretty much for free!

挥剑断情 2024-12-15 16:56:37

您的 UpdateCommand 应该是 UPDATE 命令,而不是 SELECT 命令。

SqlDataSource1.UpdateCommand = "SELECT Something FROM A_Table WHERE ID = " + e.RowIndex;

将该行更改为更接近此内容:

SqlDataSource1.UpdateCommand = "UPDATE A_Table SET Something = @Something WHERE ID = " + e.RowIndex;

有关如何使用 UpdateCommand 的详细信息,请参阅此 msdn 链接:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updatecommand.aspx

Your UpdateCommand is supposed to be an UPDATE command, not a SELECT command.

SqlDataSource1.UpdateCommand = "SELECT Something FROM A_Table WHERE ID = " + e.RowIndex;

Change that line to something closer to this:

SqlDataSource1.UpdateCommand = "UPDATE A_Table SET Something = @Something WHERE ID = " + e.RowIndex;

See this msdn link for more info on how to use the UpdateCommand:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updatecommand.aspx

长发绾君心 2024-12-15 16:56:37

首先, UpdateCommand 不应该像 UPDATE A_Table SET ID = .. WHERE ...

我认为你错过了一些东西。阅读在 GridView 中插入更新编辑删除记录。这对你来说是一篇很棒的文章。

编辑:就像 Kiley 说的:尝试 SqlDataSource1.UpdateCommand = "UPDATE A_Table SET Something = @Something WHERE ID = " + e.RowIndex;

First of all, UpdateCommand shouln't be like UPDATE A_Table SET ID = .. WHERE ... ?

I think you missing something. Read Insert Update Edit Delete record in GridView. This is a great article for you.

EDIT: Like Kiley say: Try SqlDataSource1.UpdateCommand = "UPDATE A_Table SET Something = @Something WHERE ID = " + e.RowIndex;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文