Insert/Update/Delete is disabled for this control. message of EntityDataSource

…衆ロ難τιáo~ 提交于 2019-12-12 20:10:44

问题


I use ASPxGridView and EntityDataSource as its datasource. In EntityDataSource, I write CommandText, so I can not set "EnableInsert", "EnableUpdate", or "EnableDelete" to true. That's why, I manipulate (insert, update, delete) data manually. Changes are made manually pass through to database. But at the side of the GridView these errors are given:

For inserting: "Insert is disabled for this control."
For updating: "Update is disabled for this control."
For deleting: "Delete is disabled for this control."

How can I solve this problem?

(The reason of using CommandText is where parameters and joining of more than 1 tables for showing in GridView.)


回答1:


First you must have primary ID in table, and you must have it in form so you can insert it, or ideally, you set incrementing key, and just input values. Then you have to set value property for control. And then it should work.




回答2:


I made workaround to this problem. in gridview I have template column (I'm passing two arguments in CommandArgument):

<asp:TemplateField>
                   <ItemTemplate>                           
                       <asp:ImageButton ToolTip="Delete" ID="button4" ButtonType="Image" ImageUrl="~/Projectimages/img_del.png" Text="" CommandName="Select" CommandArgument='<%#Eval("ID") + ";" +"Delete"%>' runat="server"/>
                   </ItemTemplate>
               </asp:TemplateField>

in code behind I'm splitting CommandArgument and saving values in variables and then using them in SelectedIndexChanged event:

string selectCommand = "";
    int selectCommandID = -1;
    protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {                
            if (!e.CommandArgument.ToString().Contains(";"))
                selectCommand = "Select";
            else
            {
                selectCommandID = Convert.ToInt32(e.CommandArgument.ToString().Split(';')[0]);
                selectCommand = e.CommandArgument.ToString().Split(';')[1];
            }
        }            
    }

    protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (selectCommand == "Select")
        {
            //Select Code Here
        }
        else if (selectCommand == "Delete")
        {
            MyTestEntities context = new MyTestEntities();
            Table1 selectedRow = context.Table1.Single(a => a.ID == selectCommandID);
            context.Table1.DeleteObject(selectedRow);
            context.SaveChanges();

            EntityDataSource1.DataBind();
        }
    }

this works. You can use this for updating gridview row too.



来源:https://stackoverflow.com/questions/9002927/insert-update-delete-is-disabled-for-this-control-message-of-entitydatasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!