Delete button on DataGrid not firing

J

Jeff User

Hello
..NET 1.1, VS 2003, C# & asp.net
I have tried to follow msdn instructions and samples but I can not get
an event to fire for this button on the datagrid.

There has to be something obvious missing here, but after 2 days I am
ready to explode ! Please help.

To create the Delete button I selected the grid in design view,
clicked the "Columns" property and added the Delete button in the
properties page.
Then, I switched to the lightening bolt (events) and double clicked
the
"DeleteCommand" entry. On the C# page this created the :
private void dgPrivs_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{ etc....}
event procedure. It also apparently created the
initialize component entry:
this.dgPrivs.DeleteCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgPrivs_DeleteCommand);

Clicking the Delete button on the pages causes it to post back, but
this event code never runs.

aspx page bit:
.......
<asp:datagrid id="dgPrivs" style="Z-INDEX: 110; LEFT: 256px; POSITION:
absolute; TOP: 176px" runat="server"
BackColor="#E0E0E4" BorderStyle="Outset" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Grantor" ReadOnly="True"
HeaderText="Grantor"></asp:BoundColumn>
<asp:BoundColumn DataField="Grantee" ReadOnly="True"
HeaderText="Grantee"></asp:BoundColumn>
<asp:BoundColumn DataField="Object_Type" ReadOnly="True"
HeaderText="Object Type"></asp:BoundColumn>
<asp:BoundColumn DataField="Object_Name" ReadOnly="True"
HeaderText="Object Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Privilege_Type" ReadOnly="True"
HeaderText="Privilege"></asp:BoundColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
</Columns>

C# code bits
private void InitializeComponent()
{
this.dgPrivs.DeleteCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgPrivs_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}


private void dgPrivs_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string t ="ldsld";
t = t + "Resotr";
}


Do I need to add something else in here anywhere?
Is there anything else I should be looking for?

Thanks
Jeff
 
A

Alec MacLean

Jeff,

I use VB, but the syntax is very similar, so you should be able to grok
this.

First off, trap the ItemCommand event of the datagrid:

Private Sub dgOuter_ItemCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgOuter.ItemCommand
'Capture events from datagrid
Dim b As LinkButton
b = e.CommandSource
If b.Text = "Delete" Then
'// <your specifics here>: call your delete function
me.lblMyMessageToTheUser.Text = "You clicked to delete row: " &
me.dgOuter.SelectedIndex.ToString
End If
End Sub

What I commonly do is have a hidden panel above the datagrid that holds a
confirmation request plus two buttons, the cancel and the confirm. I then
set the CommandArgument of the confirm button to equal the ID of the item
displayed in the datagrid. I can then pass that ID to the SQL procedure
performing the delete.

Like this (assuming your datagrid has the primarykey ID in column zero,
which may optionally be hidden):

Private Sub dgOuter_ItemCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgOuter.ItemCommand
'Capture events from datagrid
Dim b As LinkButton
b = e.CommandSource
If b.Text = "Delete" Then
'<your specifics here>
me.cmdDeleteConfirm.CommandArgument = e.Item.Cells(0).Text
'call your delete function
me.ShowDeleteConfirm()
End If
End Sub

private sub ShowDeleteConfirm()
' Show the confirm panel
me.pnlDeleteConfirm.visible = true
end sub

private sub cmdDeleteConfirm_Click(...) handles cmdDeleteConfirm.click
'User has clicked delete confirm
'Action the delete on datasource
'... sql connection setup, etc.
'... (I use the MS - DAAB for this)
'...
'e.g. Add PK of item as parameter to sql sproc
cmd.AddInParameter("@yourPK_ID", dbtype.Int32,
cint(me.cmdDeleteConfirm.CommandArgument))
end sub

If you are wondering about the MS - DAAB comment, see MSDN Mag's Data Points
article by Jon Papa at:
http://msdn.microsoft.com/msdnmag/issues/05/07/DataPoints/. But note that a
new version is coming for 2.0.

Hope that helps.

Al
 
G

Guest

Hi Jeff,

Most common reason to cause datagrid events not being properly fired is that
datagrid’s EnableViewState to be disabled (sometimes indirectly, e.g. its
disabled viewstate in container, page, usercontrol, and so on). Hence, please
check the property first.

And sometimes, some other reasons also cause this kind of problem.
Yesterday, I found one developer’s datagrid data-binding function in wrong
place. In that case, if we don’t read whole code, we can never find out
what’s wrong.


HTH

Elton Wang
 
J

Jeff User

Elton
In the properties window for the datagrid, the EnableViewState is set
to true. I searched the entire project for the phrase "viewstate" and
it was not found, so I take it, it is not being changed
programatically.

Every piece of my code that has any references at all to either my
datagrid or the button is listed in my code below. Not sure what you
meant by the "datagrid data-binding function", but if you don't see
something in my code that should be there, could you let me know?

I also tried adding the dgPrivs_ItemCommand event to my C# code, but
that also does not fire when button is clicked.

Thanks again
Jeff
 
G

Guest

I suppose you must have following code:

datagrid.DataSource = dataObject;
datagrid.DataBind();

Generally, the code is in Page_Load event like
if(!this.IsPostback)
{
datagrid.DataSource = dataObject;
datagrid.DataBind();
}

Now you comment out IsPostback condition, to see waht happens.

Elton
 
J

Jeff User

Sorry, yes, I do have those two lines and they work fine.

It appears to me that the C# page knows nothing about the aspx
DataGrid event. I think I will create a new post, worded differently,
asking about the connection between aspx control events and C# code
behind pages.

If you have any other ideas, or would like to see all the code posted,
Please let me know.
I have to run out for awhile, will be back at this later.

Thanks again
jeff
 

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

Top