我一生都无法弄清楚 OnItemUpdated 何时被解雇。我一直在尝试学习 ASP.NET,所以你在这段代码中看到的一些东西可能是故意用困难的方式完成的(所以我可以更好地理解幕后发生的事情)
基本上,我有一个 GridView,它是使用 formview 作为细节的主控件。
这是SelectedIndexChanged
方法GridView
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var context = new DataAccessLayer.SafetyEntities();
var se = (from c in context.Employees
where c.EID == (long)GridView1.SelectedDataKey.Value
select c).ToList();
FormView1.DataSource = se;
FormView1.DataKeyNames = new string[] { "EID" };
FormView1.DataBind();
}
这很好,它在表单中显示选定的详细信息以进行编辑。这是看起来的formview
样子:
<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" OnItemUpdating = "FormView1_ItemUpdating" OnItemUpdated="BLAH">
<ItemTemplate>
Select an employee!
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<th>Name:
</th>
<td>
<asp:TextBox runat="server" ID ="NameEdit" Text='<%#Bind("Name") %>' />
</td>
<br />
</tr>
<tr>
<th>Manager:
</th>
<td>
<asp:DropDownList ID = "DDLEdit1" DataSourceID = "ManagerEntitySource" runat="server"
DataTextField = "Option_Value" DataValueField = "Option_Value"
SelectedValue = '<%#Bind("Manager") %>'
AppendDataBoundItems="true">
</asp:DropDownList>
</td>
<br />
</tr>
<tr>
<th>Location:
</th>
<td>
<asp:DropDownList ID="DDLEdit2" DataSourceID = "LocationEntitySource" runat="server"
DataTextField = "Option_Value" DataValueField = "Option_Value"
SelectedValue='<%#Bind("Building") %>'
AppendDataBoundItems="true">
</asp:DropDownList>
</td>
<br />
</table>
<asp:Button ID="Button2" Text="Submit Changes" runat="server" CommandName="Update" />
<!--<asp:LinkButton ID = "LB1" Text="Update" CommandName="Update" runat="server" /> -->
</EditItemTemplate>
</asp:FormView>
这也有效。您可以从FormView
我指定的OnItemUpdating
和的属性中看到OnItemUpdated
。
这里是OnItemUpdating
:
protected void FormView1_ItemUpdating(object source, FormViewUpdateEventArgs e)
{
DebugBox.Text = FormView1.DataKey.Value.ToString();
DataAccessLayer.SafetyEntities se = new DataAccessLayer.SafetyEntities();
var key = Convert.ToInt32(FormView1.DataKey.Value.ToString());
DataAccessLayer.Employee employeeToUpdate = se.Employees.Where(emp => emp.EID == key).First();
employeeToUpdate.Name = e.NewValues["Name"].ToString();
employeeToUpdate.Manager = e.NewValues["Manager"].ToString();
employeeToUpdate.Building = e.NewValues["Building"].ToString();
se.SaveChanges();
GridView1.DataBind();
}
这也很好。这些项目正在适当地更新并且GridView
令人耳目一新。
这里是OnItemUpdated
:
protected void BLAH(object source, FormViewUpdatedEventArgs e)
{
DebugBox2.Text = "BLAH!!!!";
}
这就是问题所在。这永远不会被调用!我是否在某个地方错过了让此事件触发的步骤?我以为我明白该按钮会调用 Command="Update",它会触发 ItemUpdating,然后触发 ItemUpdated。它肯定在调用 ItemUpdating,但仅此而已。我需要额外的东西来触发 ItemUpdated 吗?