ASP.NET中GridView控件开发问题:无法触发GridView1_RowCommand(object sender, GridViewCommandEventArgs e)方法
问题描述:我在一个aspx页面中添加了一个GridView控件,为其添加自定义的“编辑”“删除”按钮(ImageButton类型)。
(1)页面头标记为:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listMap.aspx.cs" Inherits="listMap" EnableEventValidation ="false" %>
(2)GridView控件标记:
<asp:GridView ID="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:TemplateField HeaderImageUrl="~/images/delete.gif">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/images/delete.gif" AlternateText="删除" runat="server" ID="lbDelete" CommandName="MyDelete" CommandArgument=<%# DataBinder.Eval(Container, "DataItem.ID") %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/images/edit.gif" >
<ItemTemplate>
<asp:ImageButton ImageUrl="~/images/edit.gif" AlternateText="编辑" runat="server" ID="lbEdit1" CommandName="MyEdit" CommandArgument=<%# DataBinder.Eval(Container, "DataItem.ID") %> />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/images/cancel.jpg" CausesValidation="false" AlternateText="取消" runat="server"
ID="lbCancel1" CommandName="Cancel" />
<asp:ImageButton ImageUrl="~/images/fwd.gif" AlternateText="更新" runat="server"
ID="lbUpdate1" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/images/map.gif" >
<ItemTemplate>
<asp:ImageButton ImageUrl="~/images/map.gif" AlternateText="查看地图" runat="server" ID="lbView" CommandName="MapView" CommandArgument=<%# DataBinder.Eval(Container, "DataItem.ID") %> />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="地图名称" HeaderText="地图名称" ReadOnly="True" />
<asp:BoundField DataField="数据格式" HeaderText="数据格式" ReadOnly="True" />
<asp:TemplateField HeaderText="比例尺等级">
<EditItemTemplate>
<asp:TextBox ID="txtAvgVal11" runat="server" Width="50px" Text='<%# Bind("比例尺等级") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="txtAvgVal11"
ErrorMessage="*"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("比例尺等级") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="备注" HeaderText="备注" ReadOnly="True" />
</Columns>
</asp:GridView>
(3)对应的.cs文件中的代码如下:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
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 的条件下。 --------------------编程问答--------------------
支持
我也是这么弄的。 --------------------编程问答-------------------- xuexi --------------------编程问答-------------------- RowDeleting 事件里可用 --------------------编程问答-------------------- 先看基础概念:
http://www.google.com.hk/search?hl=zh-CN&lr=lang_zh-CN%7Clang_zh-TW&newwindow=1&safe=strict&rlz=1I7GGLD_zh-CN&biw=1366&bih=712&tbs=lr%3Alang_1zh-CN%7Clang_1zh-TW&q=asp.net+bubbleevent&oq=asp.net+bubbleevent&aq=f&aqi=&aql=&gs_l=serp.3...9176.9697.0.10241.5.4.0.0.0.0.0.0..0.0...0.0..0.IVXDgEezVtE
然后看看GridView是怎么写的:
protected override bool OnBubbleEvent(object source, EventArgs e)也就是说,你的事件第二个参数EventArgs必须是GridViewCommandEventArgs或者IButtonControl。
{
bool causesValidation = false;
string validationGroup = string.Empty;
GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
if (args != null)
{
IButtonControl commandSource = args.CommandSource as IButtonControl;
if (commandSource != null)
{
causesValidation = commandSource.CausesValidation;
validationGroup = commandSource.ValidationGroup;
}
}
return this.HandleEvent(e, causesValidation, validationGroup);
}
private bool HandleEvent(EventArgs e, bool causesValidation, string validationGroup)
{
bool flag = false;
this._modelValidationGroup = null;
if (causesValidation)
{
this.Page.Validate(validationGroup);
if (this.EnableModelValidation)
{
this._modelValidationGroup = validationGroup;
}
}
GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
if (args != null)
{
this.OnRowCommand(args);
flag = true;
string commandName = args.CommandName;
if (StringUtil.EqualsIgnoreCase(commandName, "Select"))
{
this.HandleSelect(this.GetRowIndex(args.Row, (string) args.CommandArgument));
return flag;
}
if (StringUtil.EqualsIgnoreCase(commandName, "Page"))
{
string commandArgument = (string) args.CommandArgument;
int pageIndex = this.PageIndex;
if (StringUtil.EqualsIgnoreCase(commandArgument, "Next"))
{
pageIndex++;
}
else if (StringUtil.EqualsIgnoreCase(commandArgument, "Prev"))
{
pageIndex--;
}
else if (StringUtil.EqualsIgnoreCase(commandArgument, "First"))
{
pageIndex = 0;
}
else if (StringUtil.EqualsIgnoreCase(commandArgument, "Last"))
{
pageIndex = this.PageCount - 1;
}
else
{
pageIndex = Convert.ToInt32(commandArgument, CultureInfo.InvariantCulture) - 1;
}
this.HandlePage(pageIndex);
return flag;
}
if (StringUtil.EqualsIgnoreCase(commandName, "Sort"))
{
this.HandleSort((string) args.CommandArgument);
return flag;
}
if (StringUtil.EqualsIgnoreCase(commandName, "Edit"))
{
this.HandleEdit(this.GetRowIndex(args.Row, (string) args.CommandArgument));
return flag;
}
if (StringUtil.EqualsIgnoreCase(commandName, "Update"))
{
this.HandleUpdate(args.Row, this.GetRowIndex(args.Row, (string) args.CommandArgument), causesValidation);
return flag;
}
if (StringUtil.EqualsIgnoreCase(commandName, "Cancel"))
{
this.HandleCancel(this.GetRowIndex(args.Row, (string) args.CommandArgument));
return flag;
}
if (StringUtil.EqualsIgnoreCase(commandName, "Delete"))
{
this.HandleDelete(args.Row, this.GetRowIndex(args.Row, (string) args.CommandArgument));
}
}
return flag;
}
补充:.NET技术 , ASP.NET