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

时间:2011-07-28 06:08:13

标签: c# asp.net

我想在sqldatasource上有条件地调用update语句。我有一个gridview和sqldatasource分配给它。我处理gridview_updating事件。如果我没有为它分配sqldatasource.update命令(当某些条件不符合时),我会收到错误:“除非指定了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???
               }
            }
        }

1 个答案:

答案 0 :(得分:7)

您可以在RowUpdating事件处理程序中尝试使用它,而不是在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一些值 - 如果你这样做,你总是可以将UpdateCommand设置为你用于条件的那个(假设你只有一个更新命令) )在RowCommand事件处理程序(或SqlDataSource的标记)中,然后像这样执行RowUpdating:

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

希望以上内容会有所帮助,或者至少让你指出正确的方向。