GridView的RowUpdating SqlDataSource.Update从代码隐藏 [英] GridView RowUpdating SqlDataSource.Update from CodeBehind

查看:157
本文介绍了GridView的RowUpdating SqlDataSource.Update从代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个OnRowUpdating活动期间做在GridView更新的问题。

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

我所试图做的是设置更新命令在将SqlDataSource然后更新使用该命令。本次活动是好的射击,但当事件完成后,似乎该行从未更新

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();
}



编辑:重新写我的示例更新命令......这是一个真正的更新命令,而不是选择命令哈哈。

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

推荐答案

这里的其他答案均符合指出这个问题非常正确的更新命令

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

我在评论前面提到的,我就指出,这是不是更容易了一点什么在你那里。当使用 GridView控件的SqlDataSource ,很多工作都是为你,只要你设置正确。

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.

首先,定义你的更新命令在标记为的SqlDataSource ,就像这样:

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>



Col​​PK 将是主键表中您要更新)

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

然后,您可以设置AutoGenerateEditButton属性为true,你的GridView标记和,噗!您可以更新 GridView控件(无需做代码隐藏任何东西)。

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>

现在,你仍然可以办理 OnRowUpdating 事件在你的代码,如果你需要做额外的处理,或取消更新基于某种逻辑等,但基本的功能更新是相当多是免费的!

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!

这篇关于GridView的RowUpdating SqlDataSource.Update从代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆