问题
I have a gridview. Markup is like this...
<asp:GridView ID="gvwServers" runat="server" class="gvwServers"
AutoGenerateColumns="false" OnRowEditing="gvwServers_Edit"
onrowcancelingedit="gvwServers_Cancelling" onrowdeleting="gvwServers_Deleting"
onrowupdated="gvwServers_Updated" onrowupdating="gvwServers_Updating"
AutoGenerateEditButton=true AutoGenerateDeleteButton=true>
<columns>
<asp:CommandField ButtonType="Button" EditText="Edit" ShowEditButton="true" />
<asp:CommandField ButtonType="Button" EditText="Delete" ShowDeleteButton="true" />
...Bound fields and template fields etc...
My Code-behind has this...
protected void gvwServers_Edit(object sender, GridViewEditEventArgs e)
{
gvwServers.EditIndex = e.NewEditIndex;
gvwServers.DataBind();
}
protected void gvwServers_Updated(object sender, GridViewUpdatedEventArgs e)
{
gvwServers.DataBind();
}
protected void gvwServers_Updating(object sender, GridViewUpdateEventArgs e)
{
gvwServers.DataBind();
}
protected void gvwServers_Deleting(object sender, GridViewDeleteEventArgs e)
{
gvwServers.DataBind();
}
protected void gvwServers_Cancelling(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
gvwServers.EditIndex = -1;
gvwServers.DataBind();
}
The thing is that most of these routines fire correctly ie Edit, Delete, Cancel. Except when I click the Update button, gvwServers_Edit()
fires. Why is this happening? How do I make the Update button click fire gvwServers_Updating()
?
Update: My issue is resolved here.
回答1:
You can always re-map the event to the function..
Well almost...the "Edit" events can only map to a function that has (object sender, GridViewCancelEditEventArgs e)
as params, likewise update functions must have (object sender, GridViewUpdatedEventArgs e)
params...
You can use the pre-made event handling function or make your own, again the key being the 'e' param type.
If you want to have common functionality for both the Edit() and Updating() event, put that code into a separate function that is called by the handlers when those events fire.
来源:https://stackoverflow.com/questions/4852474/update-button-fires-edit-event-handler