问题描述
问题描述:我在一个aspx页面中添加了一个GridView控件,为其添加自定义的“编辑”“删除”按钮(ImageButton类型)。(1)页面头标记为:<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="listMap.aspx.cs"Inherits="listMap"EnableEventValidation="false"%>(2)GridView控件标记:<asp:GridViewID="GridView1"runat="server"SkinID="gridviewSkin"Width="100%"AutoGenerateColumns="False"OnRowCancelingEdit="GridView1_RowCancelingEdit"OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"OnRowCommand="GridView1_RowCommand"OnRowDataBound="GridView1_RowDataBound"><Columns><asp:TemplateFieldHeaderImageUrl="~/images/delete.gif"><ItemTemplate><asp:ImageButtonImageUrl="~/images/delete.gif"AlternateText="删除"runat="server"ID="lbDelete"CommandName="MyDelete"CommandArgument=<%#DataBinder.Eval(Container,"DataItem.ID")%>/></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderImageUrl="~/images/edit.gif"><ItemTemplate><asp:ImageButtonImageUrl="~/images/edit.gif"AlternateText="编辑"runat="server"ID="lbEdit1"CommandName="MyEdit"CommandArgument=<%#DataBinder.Eval(Container,"DataItem.ID")%>/></ItemTemplate><EditItemTemplate><asp:ImageButtonImageUrl="~/images/cancel.jpg"CausesValidation="false"AlternateText="取消"runat="server"ID="lbCancel1"CommandName="Cancel"/><asp:ImageButtonImageUrl="~/images/fwd.gif"AlternateText="更新"runat="server"ID="lbUpdate1"CommandName="Update"/></EditItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderImageUrl="~/images/map.gif"><ItemTemplate><asp:ImageButtonImageUrl="~/images/map.gif"AlternateText="查看地图"runat="server"ID="lbView"CommandName="MapView"CommandArgument=<%#DataBinder.Eval(Container,"DataItem.ID")%>/></ItemTemplate></asp:TemplateField> <asp:BoundFieldDataField="ID"HeaderText="ID"ReadOnly="True"/><asp:BoundFieldDataField="地图名称"HeaderText="地图名称"ReadOnly="True"/><asp:BoundFieldDataField="数据格式"HeaderText="数据格式"ReadOnly="True"/><asp:TemplateFieldHeaderText="比例尺等级"><EditItemTemplate><asp:TextBoxID="txtAvgVal11"runat="server"Width="50px"Text='<%#Bind("比例尺等级")%>'></asp:TextBox><asp:RequiredFieldValidatorID="RequiredFieldValidator11"runat="server"ControlToValidate="txtAvgVal11"ErrorMessage="*"></asp:RequiredFieldValidator></EditItemTemplate><ItemTemplate><asp:LabelID="Label11"runat="server"Text='<%#Bind("比例尺等级")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:BoundFieldDataField="备注"HeaderText="备注"ReadOnly="True"/></Columns></asp:GridView>(3)对应的.cs文件中的代码如下:protectedvoidGridView1_RowCommand(objectsender,GridViewCommandEventArgse){if(e.CommandName=="MyDelete"){//删除操作,更新表格,略}}问题描述:基于以下开发设置,调试,点击“删除”按钮,程序却没有运行进入以上的GridView1_RowCommand中的代码if(e.CommandName=="MyDelete")中:请问,为什么没有处罚这个函数GridView1_RowCommand?????????????
解决方案
解决方案二:
delete的CommandName就用Delete好了,不要用MyDelete
解决方案三:
我更改了*.aspx,*.cs中的MyDelete为Delete可是还是无法触发GridView1_RowCommand?????????????
解决方案四:
直接在GirdView里面双击删除添加删除事件事件空的也没关系这样应该就好了
解决方案五:
你可以EnableEventValidation设置为true,然后将你的所有关于GRIDVIEW的数据绑定操作都放倒Page.IsPostBack=true的条件下。
解决方案六:
引用4楼havenlau的回复:
你可以EnableEventValidation设置为true,然后将你的所有关于GRIDVIEW的数据绑定操作都放倒Page.IsPostBack=true的条件下。
支持我也是这么弄的。
解决方案七:
xuexi
解决方案八:
RowDeleting事件里可用
解决方案九:
先看基础概念:然后看看GridView是怎么写的:protectedoverrideboolOnBubbleEvent(objectsource,EventArgse){boolcausesValidation=false;stringvalidationGroup=string.Empty;GridViewCommandEventArgsargs=easGridViewCommandEventArgs;if(args!=null){IButtonControlcommandSource=args.CommandSourceasIButtonControl;if(commandSource!=null){causesValidation=commandSource.CausesValidation;validationGroup=commandSource.ValidationGroup;}}returnthis.HandleEvent(e,causesValidation,validationGroup);}privateboolHandleEvent(EventArgse,boolcausesValidation,stringvalidationGroup){boolflag=false;this._modelValidationGroup=null;if(causesValidation){this.Page.Validate(validationGroup);if(this.EnableModelValidation){this._modelValidationGroup=validationGroup;}}GridViewCommandEventArgsargs=easGridViewCommandEventArgs;if(args!=null){this.OnRowCommand(args);flag=true;stringcommandName=args.CommandName;if(StringUtil.EqualsIgnoreCase(commandName,"Select")){this.HandleSelect(this.GetRowIndex(args.Row,(string)args.CommandArgument));returnflag;}if(StringUtil.EqualsIgnoreCase(commandName,"Page")){stringcommandArgument=(string)args.CommandArgument;intpageIndex=this.PageIndex;if(StringUtil.EqualsIgnoreCase(commandArgument,"Next")){pageIndex++;}elseif(StringUtil.EqualsIgnoreCase(commandArgument,"Prev")){pageIndex--;}elseif(StringUtil.EqualsIgnoreCase(commandArgument,"First")){pageIndex=0;}elseif(StringUtil.EqualsIgnoreCase(commandArgument,"Last")){pageIndex=this.PageCount-1;}else{pageIndex=Convert.ToInt32(commandArgument,CultureInfo.InvariantCulture)-1;}this.HandlePage(pageIndex);returnflag;}if(StringUtil.EqualsIgnoreCase(commandName,"Sort")){this.HandleSort((string)args.CommandArgument);returnflag;}if(StringUtil.EqualsIgnoreCase(commandName,"Edit")){this.HandleEdit(this.GetRowIndex(args.Row,(string)args.CommandArgument));returnflag;}if(StringUtil.EqualsIgnoreCase(commandName,"Update")){this.HandleUpdate(args.Row,this.GetRowIndex(args.Row,(string)args.CommandArgument),causesValidation);returnflag;}if(StringUtil.EqualsIgnoreCase(commandName,"Cancel")){this.HandleCancel(this.GetRowIndex(args.Row,(string)args.CommandArgument));returnflag;}if(StringUtil.EqualsIgnoreCase(commandName,"Delete")){this.HandleDelete(args.Row,this.GetRowIndex(args.Row,(string)args.CommandArgument));}}returnflag;}
也就是说,你的事件第二个参数EventArgs必须是GridViewCommandEventArgs或者IButtonControl。