C
Curtis Hatter
I apologize if this has been answered, but on the .NET 247 site I saw a
thread titled "Datagrid's ItemCommand Event", and had similar troubles with
the Datagrid not properly firing the delete event if using a push button.
I could not find the thread here, so I'm posting my finding's here in hopes
that it will help others that are having the same problem.
The problem: In a DataGrid the "Delete" command does not fire properly from
a PushButton, be it using a ButtonColumn or a TemplateColumn with a
asp:Button element.
I've not been able to figure out why it doesn't work sometimes, but in my
case I can only theorize that the MultiPage I have the DataGrid nested in is
somehow affecting it. (The MultiPage is from the microsoft released ie web
controls).
<aspataGrid OnItemDataBound="DataGrid1_OnItemDataBound"
OnDeleteCommand="DataGrid1_OnDeleteCommand" Runat="server"
DataKeyField="id">
<Columns>
<asp:BoundColumn DataField="id" HeaderText="Article ID">
<ItemStyle Width="100px" HorizontalAlign="Center" />
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asplaceHolder ID="plhDelete" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
The thing to notice in the above is I use a template column, with an
asplaceHolder. This will be replaced by a button that will work. The
relevant event handler is "OnItemDataBound". This is where we will replace
the PlaceHolder with a button.
protected void DataGrid1_OnItemDataBound(object sender,
DataGridItemEventArgs e) {
ListItemType itemType = e.Item.ItemType;
if (itemType == ListItemType.Item || itemType ==
ListItemType.AlternatingItem) {
// get our placeholder control
PlaceHolder ctl = (PlaceHolder)e.Item.Cells[1].FindControl("plhDelete");
// create our button, and assign proper properties
Button b = new Button();
b.ID = "btnDelete";
b.Text = "Delete";
b.CommandName = "Delete";
// ConfirmDelete is a just a confirm dialog run clientside before
deleting
b.Attributes.Add("onclick", "return ConfirmDelete();");
ctl.controls.Add(b);
}
}
protected void DataGrid1_OnDeleteCommand(object sender,
DataGridCommandEventArgs e) {
// put your delete code here, i had a textbox that assigned text to so as
to test this was being fired
}
That's all there is. Using a placeholder in a templated column and then
replacing it with a button during grid construction properly registers the
button with the DataGrid. I've tried this approach by creating the button in
the OnItemCreated event handler, but again it does not register properly
with the DataGrid. So it seems for this approach to work it must be done in
the OnItemDataBound event handler.
This seems to be the most elegant solution I've found so far to the problem,
and I hope others may find it useful as it can be an annoying problem.
-Curtis
thread titled "Datagrid's ItemCommand Event", and had similar troubles with
the Datagrid not properly firing the delete event if using a push button.
I could not find the thread here, so I'm posting my finding's here in hopes
that it will help others that are having the same problem.
The problem: In a DataGrid the "Delete" command does not fire properly from
a PushButton, be it using a ButtonColumn or a TemplateColumn with a
asp:Button element.
I've not been able to figure out why it doesn't work sometimes, but in my
case I can only theorize that the MultiPage I have the DataGrid nested in is
somehow affecting it. (The MultiPage is from the microsoft released ie web
controls).
<aspataGrid OnItemDataBound="DataGrid1_OnItemDataBound"
OnDeleteCommand="DataGrid1_OnDeleteCommand" Runat="server"
DataKeyField="id">
<Columns>
<asp:BoundColumn DataField="id" HeaderText="Article ID">
<ItemStyle Width="100px" HorizontalAlign="Center" />
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asplaceHolder ID="plhDelete" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
The thing to notice in the above is I use a template column, with an
asplaceHolder. This will be replaced by a button that will work. The
relevant event handler is "OnItemDataBound". This is where we will replace
the PlaceHolder with a button.
protected void DataGrid1_OnItemDataBound(object sender,
DataGridItemEventArgs e) {
ListItemType itemType = e.Item.ItemType;
if (itemType == ListItemType.Item || itemType ==
ListItemType.AlternatingItem) {
// get our placeholder control
PlaceHolder ctl = (PlaceHolder)e.Item.Cells[1].FindControl("plhDelete");
// create our button, and assign proper properties
Button b = new Button();
b.ID = "btnDelete";
b.Text = "Delete";
b.CommandName = "Delete";
// ConfirmDelete is a just a confirm dialog run clientside before
deleting
b.Attributes.Add("onclick", "return ConfirmDelete();");
ctl.controls.Add(b);
}
}
protected void DataGrid1_OnDeleteCommand(object sender,
DataGridCommandEventArgs e) {
// put your delete code here, i had a textbox that assigned text to so as
to test this was being fired
}
That's all there is. Using a placeholder in a templated column and then
replacing it with a button during grid construction properly registers the
button with the DataGrid. I've tried this approach by creating the button in
the OnItemCreated event handler, but again it does not register properly
with the DataGrid. So it seems for this approach to work it must be done in
the OnItemDataBound event handler.
This seems to be the most elegant solution I've found so far to the problem,
and I hope others may find it useful as it can be an annoying problem.
-Curtis