C# 除非指定了UpdateCommand,否则数据源不支持更新

C# 除非指定了UpdateCommand,否则数据源不支持更新,c#,asp.net,C#,Asp.net,我想有条件地调用sqldatasource上的update语句。我为它分配了gridview和sqldatasource。我处理gridview\u更新事件。如果我没有将sqldatasource.updatecommand分配给它(当某些条件不满足时),我会得到错误:“数据源不支持更新,除非指定了updatecommand。” 如何抑制此错误消息 提前感谢:) 编辑: 这是gridview的我的命令事件: protected void GridView1_RowCommand(object s

我想有条件地调用sqldatasource上的update语句。我为它分配了gridview和sqldatasource。我处理gridview\u更新事件。如果我没有将sqldatasource.updatecommand分配给它(当某些条件不满足时),我会得到错误:“数据源不支持更新,除非指定了updatecommand。”

如何抑制此错误消息

提前感谢:)

编辑:

这是gridview的我的命令事件:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {


            if (e.CommandName == "Update")
            { 
               if(someconditionmatch)
                  sqldatasource1.UpdateCommand = "some update query";
               else
               {
                   //do nothing...but when do do nothing...it throws error that some update command has to compulsorily assigned...This is my problem...which updateCommand should I assign if I do not want to update anything???
               }
            }
        }

您可以在RowUpdate事件处理程序中尝试,而不是在RowCommand事件处理程序中处理此问题,因为您可以将Cancel属性设置为true:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   if(someconditionmatch)
       sqldatasource1.UpdateCommand = "some update query";
   else
       e.Cancel = true; //Cancels the impending update.
}
我不知道您是否必须在RowCommand事件处理程序中使用具有某些值的UpdateCommand-如果需要,您可以始终在RowCommand事件处理程序(或SqlDataSource的标记)中将UpdateCommand设置为用于条件的命令(假设您只有一个update命令),然后按如下方式执行RowUpdate:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   if (!someconditionmatch)
       e.Cancel = true;
}

希望上面的内容能有所帮助,或者至少能为您指明正确的方向。

为什么不在GridView\u更新事件中分配UpdateCommand并处理条件更新?例如,根据您的标准决定是否在事件处理程序中更新?@Tim:我一直在处理GridView_更新事件。但是我没有在其中指定UpdateCommand,我得到了错误。我希望我明白了。我必须强制将UpdateCommand分配给SqlDataSource,否则我会得到这个错误。我不太清楚。你能发布相关的代码和标记吗?@Tim:请阅读我更新的帖子。如果你还不清楚,请告诉我。太好了。我想根据一些条件和您的建议插入或更新一些查询,然后e.Cancel是极好的+1为什么e.Cancel=true仅在编辑模式下保留gridview?如何使其成为普通标签。