gridview中的最后一行没有到达rowCommand代码块
本文关键字:rowCommand 代码 一行 最后 gridview | 更新日期: 2023-09-27 18:03:50
我有一个gridview与rowCommand设置为其中一列。一切工作正常,但我得到这个奇怪的错误,为每一行在gridview。RowCommand块不执行。除了最后一行,其他行都可以。如果我对网格视图进行排序,最后一行出现在顶部,它可以正常工作,但任何一行出现在底部,仍然不工作。
<asp:GridView id="gvSchedules" runat="server" AutoGenerateColumns="False"
DataSourceID="odsSchedules" AllowPaging="True" AllowSorting="True" EmptyDataText="gvSchedules"
OnRowCommand="gvSchedules_RowCommand" CssSelectorClass="NormalGridView"
EnableViewState="false">
<Columns>
<asp:TemplateField SortExpression="ScheduleName" HeaderText="gvSchedulesName">
<ItemStyle Width="100%"></ItemStyle>
<HeaderStyle Wrap="false" />
<ItemTemplate>
<asp:hyperlink id="lnkEdit" runat="server" NavigateUrl='<%# EditUrl("ScheduleID", Eval("ScheduleID").ToString(), "edit", "SpaceID=" + PanelSecurity.PackageId) %>'>
<%# PortalAntiXSS.Encode((string)Eval("ScheduleName")) %>
</asp:hyperlink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="ScheduleTypeID" HeaderText="gvSchedulesType"
ItemStyle-Wrap="false" HeaderStyle-Wrap="false">
<ItemTemplate>
<%# GetSharedLocalizedString("ScheduleType." + Eval("ScheduleTypeID").ToString()) %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NextRun" SortExpression="NextRun" HeaderText="gvSchedulesNextRun"
ItemStyle-Wrap="false" HeaderStyle-Wrap="false"></asp:BoundField>
<asp:BoundField DataField="LastRun" SortExpression="LastRun" HeaderText="gvSchedulesLastRun"
ItemStyle-Wrap="false" HeaderStyle-Wrap="false"></asp:BoundField>
<asp:TemplateField HeaderText="gvSchedulesStatus" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:ImageButton ID="cmdStart" runat="server" ToolTip="Start" SkinID="StartMedium" Visible='<%# !IsScheduleActive((int)Eval("StatusID")) %>'
CommandName="start" CommandArgument='<%# Eval("ScheduleID") %>' />
<asp:ImageButton ID="cmdStop" runat="server" ToolTip="Stop" SkinID="StopMedium" Visible='<%# IsScheduleActive((int)Eval("StatusID")) %>'
CommandName="stop" CommandArgument='<%# Eval("ScheduleID") %>' />
<%# GetScheduleStatus((int)Eval("StatusID")) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="gvSchedulesResult" HeaderStyle-Wrap="false" ItemStyle-Width="150px">
<ItemTemplate>
<%# GetAuditLogRecordSeverityName((int)Eval("LastResult"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="PackageName" HeaderText="gvSchedulesSpace">
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:hyperlink id="lnkSpace" runat="server"
NavigateUrl='<%# GetSpaceHomePageUrl((int)Eval("PackageID")) %>'>
<%# Eval("PackageName") %>
</asp:hyperlink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="Username" HeaderText="gvSchedulesUser">
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:hyperlink id="lnkUser" runat="server"
NavigateUrl='<%# GetUserHomePageUrl((int)Eval("UserID")) %>'>
<%# Eval("Username") %>
</asp:hyperlink>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<div class="GridFooter">
<asp:Label ID="lblScheduledTasks" runat="server" meta:resourcekey="lblScheduledTasks" Text="Scheduled Tasks:"></asp:Label>
<uc4:Quota ID="quotaTasks" runat="server" QuotaName="OS.ScheduledTasks" />
</div>
<asp:ObjectDataSource ID="odsSchedules" runat="server" EnablePaging="True" SelectCountMethod="GetSchedulesPagedCount"
SelectMethod="GetSchedulesPaged" SortParameterName="sortColumn" TypeName="WebsitePanel.Portal.SchedulesHelper" OnSelected="odsSchedules_Selected">
<SelectParameters>
<asp:ControlParameter ControlID="chkRecursive" Name="recursive" PropertyName="Checked" />
<asp:ControlParameter ControlID="searchBox" Name="filterColumn" PropertyName="FilterColumn" />
<asp:ControlParameter ControlID="searchBox" Name="filterValue" PropertyName="FilterValue" />
</SelectParameters>
</asp:ObjectDataSource>
和我的服务器端代码
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("zarejdame stranitsata <br />");
//BindServerTime();
// set display preferences
gvSchedules.PageSize = UsersHelper.GetDisplayItemsPerPage();
if (!IsPostBack)
{
chkRecursive.Visible = (PanelSecurity.EffectiveUser.Role != UserRole.User);
// toggle controls
//btnAddItem.Enabled = PackagesHelper.CheckGroupQuotaEnabled(
// PanelSecurity.PackageId, ResourceGroups.Statistics, Quotas.STATS_SITES);
searchBox.AddCriteria("ScheduleName", GetLocalizedString("Text.ScheduleName"));
searchBox.AddCriteria("Username", GetLocalizedString("Text.Username"));
searchBox.AddCriteria("FullName", GetLocalizedString("Text.FullName"));
searchBox.AddCriteria("Email", GetLocalizedString("Text.Email"));
bool isUser = PanelSecurity.SelectedUser.Role == UserRole.User;
gvSchedules.Columns[gvSchedules.Columns.Count - 1].Visible = !isUser;
gvSchedules.Columns[gvSchedules.Columns.Count - 2].Visible = !isUser;
Response.Write("page load za parvi pat<br />");
}
Response.Write("krai na page load<br />");
}
protected void odsSchedules_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
ProcessException(e.Exception);
e.ExceptionHandled = true;
}
}
protected void gvSchedules_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Write("1111");
Response.Write("aaaa " + e.CommandArgument + "<br />");
int scheduleId = Utils.ParseInt(e.CommandArgument.ToString(), 0);
if (e.CommandName == "start")
{
Response.Write("start schedule<br />");
try
{
int result = ES.Services.Scheduler.StartSchedule(scheduleId);
if (result < 0)
{
ShowResultMessage(result);
return;
}
}
catch (Exception ex)
{
ShowErrorMessage("SCHEDULE_START_TASK", ex);
return;
}
}
else if (e.CommandName == "stop")
{
try
{
int result = ES.Services.Scheduler.StopSchedule(scheduleId);
if (result < 0)
{
ShowResultMessage(result);
return;
}
}
catch (Exception ex)
{
ShowErrorMessage("SCHEDULE_STOP_TASK", ex);
return;
}
}
// rebind grid
gvSchedules.DataBind();
}
我没有任何东西在我的PageLoad。通过调试,我可以看到输入触发了最后一行的回发,它到达了页面加载,但从未到达rowCommand代码块。Response.Write("1111");从不露面。有什么想法都会有帮助的。
您可以在gridview中添加OnRowCommand="gvSchedules_RowCommand"
我也遇到过类似的问题,我知道原因了。你的问题可能已经解决了,但我把它写在这里,因为它可能会帮助别人陷入同样的情况。
onrowcommand事件将自己与网格的rowinindex相关联,这有助于跟踪被单击的行。
如果在绑定数据后向网格中添加了任何标题或行,那么行索引将被更改,row命令将仅对找到索引的那些行执行。
在我的例子中,我添加了一个标题,并且rowindex偏移了一个。这就是它无法在最后一行上找到行命令控件的原因。也许这也是你的问题。
要解决这个问题,可以在onrowcreated事件期间添加标题,这不会弄乱行索引。