我有两个gridview,一个是父视图,另一个是child,每个gridview都有自己的OnRowCommand事件。当gridview rowcommand为一个子行触发时,父命令的rowcommand事件被触发。这导致gvProject行命令错误跟踪错误(当我们有很少的项目和很多文件夹时)
错误:索引超出范围。必须是非负数且小于 集合。参数名称:index
我该如何解决这个问题?这是我的gridView:
<asp:GridView ID="gvProject" runat="server" SkinID="ExpandableGrid" DataSourceID="odsProject" DataKeyNames="ID" OnRowDataBound="gvProject_RowDataBound" OnRowCommand="gvProject_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<i class="btnCollapseExpand icon contrast-color collapse icon-ellipsis-h fright" onclick="ExpandCollapseGridView(this);" style="cursor: pointer"></i>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Select %>">
<ItemTemplate>
<asp:CheckBox ID="chbSelect" runat="server" TabIndex="9" CssClass="contrast-checkbox" />
</ItemTemplate>
<ItemStyle CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:BoundField HeaderText="<%$ Resources:Titles,Code %>" DataField="Code" SortExpression="Code"></asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Titles,Name %>" DataField="Name" SortExpression="Name">
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-4" />
</asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Titles, Customer %>" DataField="Customer" SortExpression="Customer.Name">
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-4" />
</asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Titles,ProgressStatus %>" DataField="ProjectStatus" SortExpression="ProjectStatus.Name">
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-2" />
</asp:BoundField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, ActivityStatus%>" SortExpression="Status">
<ItemTemplate>
<%#System.Convert.ToString(Convert.ToBoolean(Eval("Status") ) ? Resources.Titles.Enable : Resources.Titles.Disable) %>
</ItemTemplate>
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-2" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Edit %>">
<ItemTemplate>
<a id="IbtnEdit" tabindex="11" href="ProjectDetail.aspx?Edit=<%# Eval("ID").ToString() %>" class="btnIcon hasTooltip" title="<%= Resources.Titles.EditProject %>"><i class="icon icon-edit"></i></a>
</ItemTemplate>
<ItemStyle CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Delete %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteProject" TabIndex="12" runat="server" CommandName="DeleteProject" title="<%$ Resources:Titles,DeleteProject %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icon-trash-o"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, AddFolder %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnAddFolder" TabIndex="13" runat="server" CommandName="AddFolder" title="<%$ Resources:Titles,AddFolder %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icomoon-icon-AddFolder"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="5%" CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr class="collapsible_panel" height="50px" id="detailRow<%# ((GridViewRow)Container).RowIndex %>">
<td colspan="100%">
<div class="collapsible_panel_content">
<asp:GridView ID="gvFolders" runat="server" DataKeyNames="ID" OnRowCommand="gvFolders_RowCommand">
<Columns>
<asp:BoundField HeaderText="<%$ Resources:Titles,FolderName %>" DataField="Name" SortExpression="Name">
<ItemStyle CssClass="col-xs-2 col-sm-2 col-md-2 col-lg-2" />
</asp:BoundField>
<asp:TemplateField HeaderText="<%$ Resources:Titles,FolderCode %>">
<ItemTemplate>
<%#System.Convert.ToString(Eval("Code").ToString().Trim())%>
</ItemTemplate>
<ItemStyle CssClass="col-xs-2 col-sm-2 col-md-2 col-lg-2" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles,Creator %>">
<ItemTemplate>
<%#System.Convert.ToString(GetUser(Eval("OwnerID")))%>
</ItemTemplate>
<ItemStyle CssClass="col-xs-2 col-sm-2 col-md-2 col-lg-2" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Edit %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnEditFolder" TabIndex="7" runat="server" CommandName="EditFolder" title="<%$ Resources:Titles,EditFolder %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icon-edit"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle CssClass="center col-xs-1 col-sm-1 col-md-1 col-lg-1" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Delete %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteFolder" TabIndex="7" runat="server" CommandName="DeleteFolder" title="<%$ Resources:Titles,DeleteFolder %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icon-trash-o"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle CssClass="center col-xs-1 col-sm-1 col-md-1 col-lg-1" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
<HeaderStyle CssClass="noPadding" />
<ItemStyle Width="0px" CssClass="noPadding" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<%= Resources.Messages.NoProjectIsForView %>
</EmptyDataTemplate>
</asp:GridView>
这是我的rowcommand事件:
protected void gvProject_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex, id;
rowIndex = Convert.ToInt32(e.CommandArgument);
GridView grid = (GridView)sender;
if (grid.ID == "gvProject")
{
id = Convert.ToInt32(grid.DataKeys[rowIndex].Value);
switch (e.CommandName)
{
case "DeleteProject":
ClearForm();
hfID.Value = id.ToString();
HiddenField1.Value = id.ToString();
lblDeleteMessage.Text = string.Empty;
Global.ShowModalBox(upProjectDelete, "delete_modal");
break;
case "AddFolder":
ClearForm();
hfID.Value = id.ToString();
Global.ShowModalBox(upAddFolder, "AddFolderModal");
break;
}
}
}
protected void gvFolders_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex, id;
rowIndex = Convert.ToInt32(e.CommandArgument);
GridView grid = (GridView)sender;
id = Convert.ToInt32(grid.DataKeys[rowIndex].Value);
switch (e.CommandName)
{
case "EditFolder":
if (grid.ID == "gvFolders")
{
ClearForm();
hfID.Value = id.ToString();
Folder folder = Providers.Folder.GetById(id);
txtFolderName.Text = folder.Name;
EditType = eEditType.Edit;
lblBoxTitle.Text = Resources.Titles.EditFolder;
Global.ShowModalBox(upAddFolder, "AddFolderModal");
}
break;
case "DeleteFolder":
if (grid.ID == "gvFolders")
{
ClearForm();
hfID.Value = id.ToString();
lblDeleteFolderMessage.Text = string.Empty;
Global.ShowModalBox(upAddFolder, "DeleteFolderModal");
}
break;
}
}
答案 0 :(得分:0)
我认为解决此问题的唯一方法是在gvproject rowcommand事件中检查命令名:
protected void gvProject_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex, id;
rowIndex = Convert.ToInt32(e.CommandArgument);
GridView grid = (GridView)sender;
if (e.CommandName == "DeleteProject" || e.CommandName == "AddFolder")
{
id = Convert.ToInt32(grid.DataKeys[rowIndex].Value);
...
}
}