Hi,
I am working with the gridview in .net 3.5 and have run into something I can not explain. I am testing my delete methods when I realized that the row was actually being deleted before my event handler even finished processing. Am I dreaming or just missing something.
I setup a base gridview with event handlers for select, insert, update, and delete.
<asp:GridView ID="TableGridView"
OnRowEditing ="TableGridView_RowEditing"
OnRowCancelingEdit="TableGridView_RowCancelingEdit"
OnRowUpdating="TableGridView_RowUpdating"
OnRowDeleting="TableGridView_RowDeleting"
OnPageIndexChanging="TableGridView_PageIndexChanging"
runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowPaging="True"
AllowSorting="True" HorizontalAlign="Center">
<PagerSettings Mode="NumericFirstLast"
FirstPageImageUrl="~/Images/n_skip_first.gif" FirstPageText="First"
LastPageImageUrl="~/Images/n_skip_last.gif" LastPageText="Last"
NextPageImageUrl="~/Images/n_skip_next.gif" NextPageText="Next"
PreviousPageImageUrl="~/Images/n_skip_previous.gif"
PreviousPageText="Previous" PageButtonCount="5" Position="TopAndBottom" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
I then dynamically load the grid with buttons and data using ITemplate.
Here is my InstantiateIn Method.
public void InstantiateIn(System.Web.UI.Control Container)
{
switch (ItemType)
{
case ListItemType.Header:
Literal header_ltrl = new Literal();
header_ltrl.Text = "<b>" + FieldName + "</b>";
Container.Controls.Add(header_ltrl);
break;
case ListItemType.Item:
switch (ControlType)
{
case "Command":
ImageButton edit_button = new ImageButton();
edit_button.ID = "edit_button";
edit_button.ImageUrl = "~/images/edit.gif";
edit_button.CommandName = "Edit";
edit_button.Click += new ImageClickEventHandler(edit_button_Click);
edit_button.ToolTip = "Edit";
Container.Controls.Add(edit_button);
ImageButton delete_button = new ImageButton();
delete_button.ID = "delete_button";
delete_button.ImageUrl = "~/images/delete.gif";
delete_button.CommandName = "Delete";
delete_button.OnClientClick = "return confirm('Confirm you want to delete the record?')";
delete_button.ToolTip = "Delete";
Container.Controls.Add(delete_button);
ImageButton insert_button = new ImageButton();
insert_button.ID = "insert_button";
insert_button.ImageUrl = "~/images/insert.bmp";
insert_button.CommandName = "Edit";
insert_button.ToolTip = "Insert";
insert_button.Click += new ImageClickEventHandler(insert_button_Click);
Container.Controls.Add(insert_button);
break;
default:
Label field_lbl = new Label();
field_lbl.ID = FieldName;
field_lbl.Text = String.Empty;
field_lbl.DataBinding += new EventHandler(OnDataBinding);
Container.Controls.Add(field_lbl);
break;
}
break;
case ListItemType.EditItem:
if (ControlType == "Command")
{
ImageButton update_button = new ImageButton();
update_button.ID = "update_button";
update_button.CommandName = "Update";
update_button.ImageUrl = "~/images/update.gif";
update_button.ToolTip = "Update";
update_button.OnClientClick =
"return confirm('Confirm you want to update the record?')";
Container.Controls.Add(update_button);
// Similarly, add a button for Cancel
ImageButton cancel_button = new ImageButton();
cancel_button.ID = "cancel_button";
cancel_button.CommandName = "Cancel";
cancel_button.ImageUrl = "~/images/cancel.gif";
cancel_button.ToolTip = "Cancel";
cancel_button.OnClientClick =
"return confirm('Confirm you want to cancel?')";
Container.Controls.Add(cancel_button);
}
else
// if other key and non key fields then bind textboxes with texts
{
TextBox field_txtbox = new TextBox();
field_txtbox.ID = FieldName;
field_txtbox.Text = String.Empty;
// if to update then bind the textboxes with coressponding field texts
//otherwise for insert no need to bind it with text
if ((int)new Page().Session["InsertFlag"] == 0)
field_txtbox.DataBinding += new EventHandler(OnDataBinding);
Container.Controls.Add(field_txtbox);
}
break;
}
}
I have tested my event handler and it is working fine and deletes the row correctly and was just doing some final testing before moving onto another method when I noticed that if I break out of the app just before the event handler is fired the row has already been deleted.
I had a feeling it was something to do with on page load I am re-building the template but I put a break there and still the row was deleted. It looks like once I hit ok on the confirm return the record is deleted.
Does anyone have a better understanding of this control, am I doing something wrong or just not well informed.