在有人回答这个问题之前,我在我的代码中发现了这个错误,并在下面发布了我的答案。我把这个问题留了下来(除非有人真的在乎)作为一个警示故事,以始终约束你的 bleepingGridView
。
前言/背景
我(ab)以一种奇怪的方式使用 ASP.NET WebForms 控件。我知道。
我正在开发一个应用程序,希望学生列出他们参加过的一些学校活动。学生可以添加几行,并且——仅出于一般工作流程的原因——我决定将GridView
显示他们“已保存”条目的行分开从他们添加/更新活动的“详细信息”区域。
编辑/更新作品精美。我可以添加新项目,使用绑定到我的类中的内置验证(这是所有具有属性的额外span
标签显示的内容)。class="error"
我真的很想让这个白痴证明,所以他们单击添加新活动的按钮也将验证并将他们的信息添加到GridView
(如果项目通过验证),清除详细信息控件,并正常运行。
问题描述
我对此开始了一些基本测试,发现如果我编辑一个活动,打破该类中的一个(或多个)验证规则,然后单击“添加新活动”按钮,我GridView
将该行从EditItemTemplate
后面恢复到正常ItemTemplate
。
据我所知,我EditIndex
从未偏离我仍在编辑的行。事实上,如果我触发另一个回发,则GridView
显示正确EditIndex
!
我已经尝试重新设置EditIndex
并切换我e.Cancel = true
的return
声明。什么都没奏效。考虑到EditIndex's
值通过多次回发保持不变,我不得不认为这里发生了一些奇怪的事情。
我在下面发布了标记和 C# 代码。两者都非常荒谬 - 我重构了我的功能 - 所以我提前道歉,并感谢任何足够勇敢(或足够无聊)提供建议的人。
此外,如果有人愿意看到这种情况发生而不是(或除此之外)阅读我的标记/代码,我可以提供屏幕截图。
GridView
<asp:GridView ID="schoolActivities" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="edit" CommandName="Edit" runat="server" Text="Edit" />
</ItemTemplate>
<EditItemTemplate></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<%# DataBinder.GetPropertyValue(Container.DataItem, "Details") %>
</ItemTemplate>
<EditItemTemplate>Editing...</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Months per Year
</HeaderTemplate>
<ItemTemplate><%# DataBinder.GetPropertyValue(Container.DataItem, "MonthsPerYear") %></ItemTemplate>
<EditItemTemplate></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Date</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.GetPropertyValue(Container.DataItem, "ActivityDate") %>
</ItemTemplate>
<EditItemTemplate></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Total Hours Outside Class Time
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.GetPropertyValue(Container.DataItem, "TotalHours") %>
</ItemTemplate>
<EditItemTemplate></EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
“详细信息”部分
<br />
<button type="button" id="newActivity" runat="server">Add New Activity</button>
<br />
<fieldset id="schoolActivityFields" runat="server" visible="false">
<legend>Activity</legend>
<span>Details:</span>
<span>
<textarea id="schoolActivityDetails" class="schoolActivityDetails" runat="server"></textarea>
<br />
<span id="schoolActivityDetailsError" class="error" runat="server"></span>
</span>
<br />
<span>Months per Year:</span>
<span>
<select id="schoolActivityMonthsPerYear" runat="server">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
</span>
<span id="schoolActivityMonthsPerYearError" class="error" runat="server"></span>
<br />
<span>Date:</span>
<span>
<select id="schoolActivityDate" runat="server">
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
</select>
</span>
<span id="schoolActivityDateError" class="error" runat="server"></span>
<br />
<span>Total Hours:</span>
<span>
<input type="text" id="schoolActivityTotalHours" runat="server" />
</span>
<span id="schoolActivityTotalHoursError" class="error" runat="server"></span>
<br />
<button type="button" id="addActivity" runat="server">Add Activity</button>
<button type="button" id="updateActivity" runat="server" visible="false">Update Activity</button>
<button type="button" id="cancelEditActivity" runat="server" visible="false">Cancel Editing Activity</button>
<button type="button" id="deleteActivity" runat="server" visible="false">Delete Activity</button>
</fieldset>
C# 的长度很长,但对于任何勇敢的人来说:
void addActivity_ServerClick(object sender, EventArgs e)
{
AddSchoolActivity();
}
private bool AddSchoolActivity()
{
ResetSchoolActivityErrors();
SchoolActivity activityToAdd = GetSchoolActivity();
try
{
SessionApplication.SchoolActivities.Add(activityToAdd);
BindSchoolActivities();
ResetSchoolActivityFields();
HideSchoolActivityFields();
}
catch (BrokenRuleException)
{
MapSchoolActivityErrors(activityToAdd);
return false;
}
return true;
}
void updateActivity_ServerClick(object sender, EventArgs e)
{
UpdateSchoolActivity();
}
private bool UpdateSchoolActivity()
{
ResetSchoolActivityErrors();
SchoolActivity activityToUpdate = GetSchoolActivity();
try
{
SessionApplication.SchoolActivities[schoolActivities.EditIndex] = activityToUpdate;
ResetSchoolActivitiesEditIndex();
BindSchoolActivities();
ResetSchoolActivityFields();
HideSchoolActivityFields();
}
catch (BrokenRuleException)
{
MapSchoolActivityErrors(activityToUpdate);
SetSchoolActivitiesEditIndex(schoolActivities.EditIndex);
return false;
}
return true;
}
private SchoolActivity GetSchoolActivity()
{
SchoolActivity currentActivity = new SchoolActivity();
currentActivity.Details = schoolActivityDetails.Value;
TryGetSchoolActivityMonthsPerYear(currentActivity);
TryGetSchoolActivityDate(currentActivity);
TryGetSchoolActivityTotalHours(currentActivity);
return currentActivity;
}
private void TryGetSchoolActivityMonthsPerYear(SchoolActivity currentActivity)
{
byte monthsPerYear;
if (byte.TryParse(schoolActivityMonthsPerYear.Value, out monthsPerYear))
{
currentActivity.MonthsPerYear = monthsPerYear;
}
}
private void TryGetSchoolActivityDate(SchoolActivity currentActivity)
{
short activityDate = -1;
if (short.TryParse(schoolActivityDate.Value, out activityDate))
{
currentActivity.ActivityDate = activityDate;
}
}
private void TryGetSchoolActivityTotalHours(SchoolActivity currentActivity)
{
short totalHours = -1;
if (schoolActivityTotalHours.Value.IsNullOrWhiteSpace() == false
&& short.TryParse(schoolActivityTotalHours.Value, out totalHours))
{
currentActivity.TotalHours = totalHours;
}
}
private void ResetSchoolActivityFields()
{
schoolActivityDetails.Value = string.Empty;
schoolActivityMonthsPerYear.SelectedIndex = 0;
schoolActivityDate.SelectedIndex = 0;
schoolActivityTotalHours.Value = string.Empty;
}
private void HideSchoolActivityFields()
{
schoolActivityFields.Visible = false;
}
private void MapSchoolActivityErrors(int activityIndex)
{
SchoolActivity updatedActivity = SessionApplication.SchoolActivities[activityIndex];
MapSchoolActivityErrors(updatedActivity);
}
private void MapSchoolActivityErrors(SchoolActivity updatedActivity)
{
foreach (RuleViolation currentViolation in updatedActivity.GetRuleViolations())
{
schoolActivityErrorMapping[currentViolation].InnerText = currentViolation.ErrorMessage;
}
}
private void ResetSchoolActivityErrors()
{
foreach (RuleViolation currentViolation in schoolActivityErrorMapping.Keys)
{
schoolActivityErrorMapping[currentViolation].InnerText = string.Empty;
}
}
void schoolActivities_RowEditing(object sender, GridViewEditEventArgs e)
{
if (schoolActivities.EditIndex != e.NewEditIndex
&& schoolActivities.EditIndex != -1)
{
ResetSchoolActivityErrors();
SchoolActivity activityToUpdate = GetSchoolActivity();
try
{
SessionApplication.SchoolActivities[schoolActivities.EditIndex] = activityToUpdate;
}
catch (BrokenRuleException)
{
MapSchoolActivityErrors(activityToUpdate);
e.Cancel = true;
}
}
if (e.NewEditIndex != -1)
{
SchoolActivity activityToEdit = SessionApplication.SchoolActivities[e.NewEditIndex];
SetSchoolActivityFields(activityToEdit);
ShowSchoolActivityFields();
ShowModifyActivityButtons();
}
SetSchoolActivitiesEditIndex(e.NewEditIndex);
BindSchoolActivities();
}
private void ShowModifyActivityButtons()
{
addActivity.Visible = false;
updateActivity.Visible = true;
cancelEditActivity.Visible = true;
deleteActivity.Visible = true;
}
private void SetSchoolActivityFields(SchoolActivity activityToEdit)
{
schoolActivityDetails.Value = activityToEdit.Details;
schoolActivityMonthsPerYear.Value = activityToEdit.MonthsPerYear.ToString();
schoolActivityDate.Value = activityToEdit.ActivityDate.ToString();
schoolActivityTotalHours.Value = activityToEdit.TotalHours.ToString();
}
private bool ShowSchoolActivityFields()
{
return schoolActivityFields.Visible = true;
}
private void BindSchoolActivities()
{
schoolActivities.DataSource = SessionApplication.SchoolActivities;
schoolActivities.DataBind();
}
void newActivity_ServerClick(object sender, EventArgs e)
{
if (SessionApplication.SchoolActivities.Count < SchoolActivityList.Rules.MaxCount)
{
if (schoolActivities.EditIndex != -1)
{
if (UpdateSchoolActivity() == false)
{
return;
}
}
else if (schoolActivityFields.Visible)
{
if (AddSchoolActivity() == false)
{
return;
}
}
ShowSchoolActivityFields();
ShowAddActivityButtons();
}
}
private void ShowAddActivityButtons()
{
addActivity.Visible = true;
updateActivity.Visible = false;
cancelEditActivity.Visible = false;
deleteActivity.Visible = false;
}
void ResetSchoolActivitiesEditIndex()
{
SetSchoolActivitiesEditIndex(-1);
}
void SetSchoolActivitiesEditIndex(int rowIndex)
{
schoolActivities.EditIndex = rowIndex;
}