关于GridView 控件的问题
小弟想用GridView读取数据的时候能够直接显示一个删除按钮,但是在做的过程中出现了错误,也不知道要怎么调试,希望高手指教~
代码
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "AlternateView.aspx.cs " Inherits= "AlternateView " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " DataKeyNames= "UserID "
DataSourceID= "SqlDataSource1 " AllowPaging= "True " AutoGenerateDeleteButton= "True ">
<Columns>
<asp:BoundField DataField= "UserID " HeaderText= "UserID " InsertVisible= "False " ReadOnly= "True "
SortExpression= "UserID " />
<asp:BoundField DataField= "UserName " HeaderText= "UserName " SortExpression= "UserName " />
<asp:BoundField DataField= "RealName " HeaderText= "RealName " SortExpression= "RealName " />
<asp:BoundField DataField= "Address " HeaderText= "Address " SortExpression= "Address " />
<asp:BoundField DataField= "RoleID " HeaderText= "RoleID " SortExpression= "RoleID " />
<asp:CheckBoxField DataField= "Status " HeaderText= "Status " SortExpression= "Status " />
<asp:BoundField DataField= "CreateDate " HeaderText= "CreateDate " SortExpression= "CreateDate " />
<asp:BoundField DataField= "Email " HeaderText= "Email " SortExpression= "Email " />
<asp:BoundField DataField= "Mobile " HeaderText= "Mobile " SortExpression= "Mobile " />
<asp:BoundField DataField= "Phone " HeaderText= "Phone " SortExpression= "Phone " />
<asp:BoundField DataField= "Remark " HeaderText= "Remark " SortExpression= "Remark " />
</Columns>
<AlternatingRowStyle BackColor= "#8080FF " BorderColor= "White " BorderWidth= "0px " Font-Size= "12px "
HorizontalAlign= "Left " VerticalAlign= "Top " />
</asp:GridView>
</div>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= " <%$ ConnectionStrings:ShoppingDBConnectionString2 %> "
SelectCommand= "SELECT [UserID], [UserName], [RealName], [Address], [RoleID], [Status], [CreateDate], [Email], [Mobile], [Phone], [Remark] FROM [Users] ">
</asp:SqlDataSource>
</form>
</body>
</html>
错误提示
除非指定了 DeleteCommand,否则数据源“SqlDataSource1”不支持删除操作。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NotSupportedException: 除非指定了 DeleteCommand,否则数据源“SqlDataSource1”不支持删除操作。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[NotSupportedException: 除非指定了 DeleteCommand,否则数据源“SqlDataSource1”不支持删除操作。]
System.Web.UI.WebControls.SqlDataSourceView.ExecuteDelete(IDictionary keys, IDictionary oldValues) +215
System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +176
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +914
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1067
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +215
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3838
[解决办法]
你的SqlDataSource1在配置数据源 配置Select语句时,右边有个高级点一下,选中生成insert,update和delete语句