Solving DataGrid's PushButton Problem

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).

<asp:DataGrid 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>
<asp:placeHolder ID="plhDelete" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

The thing to notice in the above is I use a template column, with an
asp:placeHolder. 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
 
J

Jim

Thanks for the post. I was having a similar problem and have been
staring at the code for 2 days trying to figure out why the buttons were
no firing off the OnItemCommand. In my case, none of the controls in
the Template Columns would work (textboxes, buttons, etc.). I had to
use your method to access them all. I have tried to figure out what is
causing it and this is the closest I have come. About the time the
problem started for me, I was starting to put my Database code in a
component file and call it on the page when I need to bind the datagrid,
etc. I was also caching the datasets. If I get rid of all that and
just put all database calls (ie Sqlconnection, sqlcommand,
sqlDataAdapter, etc.) on the page where I am using them, then the
DataGrid appears to work fine by just putting the button and other
controls right in the DataGrid! Very annoying, but I wanted to pass the
info along. Thanks for your post it really helped me to diagnose the
problem.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top