473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Solving DataGrid's PushButton Problem

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
Nov 17 '05 #1
1 1929
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: shamila | last post by:
</asp:label><asp:datagrid id="DataGrid3" runat="server" cssclass="DataGrid" showfooter="True" onupdatecommand="DataGrid3_Update"ondeletecommand="DataGrid3_Delete" oneditcommand="DataGrid3_Edit"...
12
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the...
0
by: Curtis Hatter | last post by:
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...
9
by: tshad | last post by:
Is there a way to use your own image in place of the automatic one that ASP uses when doing editing in your DataGrid pages? We already have a style of button we are using and would like to be...
2
by: Tomek R. | last post by:
Hello ! I've got weird problem when adding new datagrid item. Here is the situation: my grid dgDeps is binded to DepartmentList arraylist, stored in Session between round-trips.. To add new...
1
by: Joe | last post by:
Hi, I have form with datagrid. This is what my form tag looks like, <form runat="server"> <asp:label runat="server" id="lblMessage" /> <asp:DataGrid runat="server" id="articleList"...
5
by: Vik | last post by:
If there are a few Select buttons in a datagrid, is there a way to distinguish in code which button was clicked? Thanks.
0
by: DotQuery | last post by:
Hello ASP.NET team : I have a user control that be added into page at runtime , in a postback event handler . I save the information to view state , and load the user control again in...
2
by: Iain | last post by:
Hi All Using Delphi 2006 developer - C# Project I have the following 2 event handlers for the datagrid - see botton of page Both events will fire off correctly but i have a problem collecting...
0
by: fripper | last post by:
I am using VB .Net 2003. I am really struggling to understand how to display a table (datagrid) in a web app. Let me explain what I want to do ... I want the table to be, say, six columns wide....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.