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
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top