GridView RowUpdating SqlDataSource.Update 来自 CodeBehind
因此,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的其他答案非常正确地指出了
UpdateCommand
的问题。正如我在前面的评论中提到的,我只是指出这比你那里的要容易一些。当将
GridView
与SQLDataSource
一起使用时,只要您设置正确,很多工作都会为您完成。首先,在
SQLDataSource
的标记中定义UpdateCommand
,如下所示:(ColPK
将是您所在表的主键更新)然后,您可以在 GridView 标记中将“AutoGenerateEditButton”设置为 true,然后,噗!您可以更新 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 aSQLDataSource
, alot of the work is done for you as long as you set up correctly.First off, define your
UpdateCommand
in the markup for theSQLDataSource
, like this:(
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).Now, you can still handle the
OnRowUpdating
event in your code if you need to do extra processing, or cancel theUpdate
based on some logic, etc. But the basic Update functionality is pretty much for free!您的 UpdateCommand 应该是
UPDATE
命令,而不是SELECT
命令。将该行更改为更接近此内容:
有关如何使用 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 aSELECT
command.Change that line to something closer to this:
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
首先,
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 likeUPDATE 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;