Cancel does Delete instead!??

C

cj

I have created a datagrid with both EditCommandColumn and ButtonColumn
(setup as Delete). Even when I change the Delete command to a different name
"Remove", the "Cancel" button always ends up doing the 'Delete/Remove'
subroutine :(

What is happening with this? I searched on the net and only found one
similar occurance, but no answer :( Surely it is a known problem..?

Below is some of my code.

Any help out there (I've spent hours trying to nut this out...)?

Thanks

cj

<asp:datagrid
id="dgSoldItems"
runat="server"
AutoGenerateColumns="False"
EnableViewState="False"
ForeColor="Black"
BackColor="White"
CellPadding="3"
CellSpacing="1"
Font-Size="X-Small"
GridLines="None"
DataKeyField="SaleItemID"
OnEditCommand="dgSoldItems_Edit"
OnCancelCommand="dgSoldItems_Cancel"
OnUpdateCommand="dgSoldItems_Update"
OnDeleteCommand="dgSoldItems_Delete"<ItemStyle
backcolor="#DEDFDE"></ItemStyle>
<HeaderStyle font-bold="True"
forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
EditText="Edit" CancelText="Cancel" UpdateText="Update"
ItemStyle-Wrap="false" ButtonType="PushButton"/>
<asp:ButtonColumn Text="Remove Item"
CommandName="Delete" ButtonType="PushButton"/>
<asp:BoundColumn DataField="ItemID"
HeaderText="Item ID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Price"
HeaderText="Price" DataFormatString="{0:c}"></asp:BoundColumn>
<asp:BoundColumn
DataField="ProductName" HeaderText="Product Name"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="ModelNumber" HeaderText="Model Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SerialNumber" HeaderText="Serial Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="AssetNumber" HeaderText="Asset Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="SaleID"
HeaderText="SaleID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SaleItemID" HeaderText="SaleItemID"
ReadOnly="True"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Sub dgSoldItems_Edit(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = CInt(E.Item.ItemIndex)
BinddgSoldItems()
End Sub

Sub dgSoldItems_Cancel(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = -1
BinddgSoldItems()
End Sub

Sub dgSoldItems_Update(Sender As Object, E As DataGridCommandEventArgs)
End Sub

Sub dgSoldItems_Delete(Sender As Object, E As DataGridCommandEventArgs)

Dim DelItemCommand As SqlCommand
Dim DeleteCmdText As String = "DELETE from Sold_Items where
SaleItemID = @SaleItemID"

DelItemCommand = New SqlCommand(DeleteCmdText, MyConnection)

Dim dbParam_SaleItemID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_SaleItemID.ParameterName = "@SaleItemID"
dbParam_SaleItemID.Value =
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex))
dbParam_SaleItemID.DbType = System.Data.DbType.Int32
DelItemCommand.Parameters.Add(dbParam_SaleItemID)

DelItemCommand.Connection.Open()

Try
DelItemCommand.ExecuteNonQuery()
lblmsg.Text = "<b>Record [SaleItemID=" &
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex)) & "] Deleted</b>"
Catch Exc As SQLException
lblmsg.Text = "ERROR: Could not delete record"
End Try

DelItemCommand.Connection.Close()

BinddgSoldItems()
End Sub
 
C

cj

Actually, my problem looks similar/same as Bruce. E. Bonsall ("Dynamic
EditCommandColumn events") who posted earlier today.. but still no answer...

Thanks

cj


cj said:
I have created a datagrid with both EditCommandColumn and ButtonColumn
(setup as Delete). Even when I change the Delete command to a different
name "Remove", the "Cancel" button always ends up doing the 'Delete/Remove'
subroutine :(

What is happening with this? I searched on the net and only found one
similar occurance, but no answer :( Surely it is a known problem..?

Below is some of my code.

Any help out there (I've spent hours trying to nut this out...)?

Thanks

cj

<asp:datagrid
id="dgSoldItems"
runat="server"
AutoGenerateColumns="False"
EnableViewState="False"
ForeColor="Black"
BackColor="White"
CellPadding="3"
CellSpacing="1"
Font-Size="X-Small"
GridLines="None"
DataKeyField="SaleItemID"
OnEditCommand="dgSoldItems_Edit"
OnCancelCommand="dgSoldItems_Cancel"
OnUpdateCommand="dgSoldItems_Update"
OnDeleteCommand="dgSoldItems_Delete"<ItemStyle
backcolor="#DEDFDE"></ItemStyle>
<HeaderStyle font-bold="True"
forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
EditText="Edit" CancelText="Cancel" UpdateText="Update"
ItemStyle-Wrap="false" ButtonType="PushButton"/>
<asp:ButtonColumn Text="Remove
Item" CommandName="Delete" ButtonType="PushButton"/>
<asp:BoundColumn DataField="ItemID"
HeaderText="Item ID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Price"
HeaderText="Price" DataFormatString="{0:c}"></asp:BoundColumn>
<asp:BoundColumn
DataField="ProductName" HeaderText="Product Name"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="ModelNumber" HeaderText="Model Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SerialNumber" HeaderText="Serial Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="AssetNumber" HeaderText="Asset Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="SaleID"
HeaderText="SaleID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SaleItemID" HeaderText="SaleItemID"
ReadOnly="True"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Sub dgSoldItems_Edit(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = CInt(E.Item.ItemIndex)
BinddgSoldItems()
End Sub

Sub dgSoldItems_Cancel(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = -1
BinddgSoldItems()
End Sub

Sub dgSoldItems_Update(Sender As Object, E As DataGridCommandEventArgs)
End Sub

Sub dgSoldItems_Delete(Sender As Object, E As DataGridCommandEventArgs)

Dim DelItemCommand As SqlCommand
Dim DeleteCmdText As String = "DELETE from Sold_Items where
SaleItemID = @SaleItemID"

DelItemCommand = New SqlCommand(DeleteCmdText, MyConnection)

Dim dbParam_SaleItemID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_SaleItemID.ParameterName = "@SaleItemID"
dbParam_SaleItemID.Value =
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex))
dbParam_SaleItemID.DbType = System.Data.DbType.Int32
DelItemCommand.Parameters.Add(dbParam_SaleItemID)

DelItemCommand.Connection.Open()

Try
DelItemCommand.ExecuteNonQuery()
lblmsg.Text = "<b>Record [SaleItemID=" &
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex)) & "] Deleted</b>"
Catch Exc As SQLException
lblmsg.Text = "ERROR: Could not delete record"
End Try

DelItemCommand.Connection.Close()

BinddgSoldItems()
End Sub
 
E

Elton W

Hi CJ,

Change the datagrid's EnableViewState from false to true.
Although disabled viewstate can improve performance of the
datagrid, it also causes many functionalities not work
properly.

HTH

Elton Wang
(e-mail address removed)

-----Original Message-----
Actually, my problem looks similar/same as Bruce. E. Bonsall ("Dynamic
EditCommandColumn events") who posted earlier today.. but still no answer...

Thanks

cj


I have created a datagrid with both EditCommandColumn and ButtonColumn
(setup as Delete). Even when I change the Delete command to a different
name "Remove", the "Cancel" button always ends up doing the 'Delete/Remove'
subroutine :(

What is happening with this? I searched on the net and only found one
similar occurance, but no answer :( Surely it is a known problem..?

Below is some of my code.

Any help out there (I've spent hours trying to nut this out...)?

Thanks

cj

<asp:datagrid
id="dgSoldItems"
runat="server"
AutoGenerateColumns="False"
EnableViewState="False"
ForeColor="Black"
BackColor="White"
CellPadding="3"
CellSpacing="1"
Font-Size="X-Small"
GridLines="None"
DataKeyField="SaleItemID"
OnEditCommand="dgSoldItems_Edit"
OnCancelCommand="dgSoldItems_Cancel"
OnUpdateCommand="dgSoldItems_Update"
OnDeleteCommand="dgSoldItems_Delete"
<ItemStyle
backcolor="#DEDFDE"></ItemStyle>
<HeaderStyle font- bold="True"
forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
EditText="Edit" CancelText="Cancel" UpdateText="Update"
ItemStyle-Wrap="false" ButtonType="PushButton"/>
<asp:ButtonColumn Text="Remove
Item" CommandName="Delete" ButtonType="PushButton"/>
<asp:BoundColumn DataField="ItemID"
HeaderText="Item ID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Price"
HeaderText="Price"
DataFormatString="{0:c}"> said:
<asp:BoundColumn
DataField="ProductName" HeaderText="Product Name"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="ModelNumber" HeaderText="Model Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SerialNumber" HeaderText="Serial Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="AssetNumber" HeaderText="Asset Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="SaleID"
HeaderText="SaleID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SaleItemID" HeaderText="SaleItemID"
ReadOnly="True"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Sub dgSoldItems_Edit(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = CInt(E.Item.ItemIndex)
BinddgSoldItems()
End Sub

Sub dgSoldItems_Cancel(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = -1
BinddgSoldItems()
End Sub

Sub dgSoldItems_Update(Sender As Object, E As DataGridCommandEventArgs)
End Sub

Sub dgSoldItems_Delete(Sender As Object, E As DataGridCommandEventArgs)

Dim DelItemCommand As SqlCommand
Dim DeleteCmdText As String = "DELETE from Sold_Items where
SaleItemID = @SaleItemID"

DelItemCommand = New SqlCommand (DeleteCmdText, MyConnection)

Dim dbParam_SaleItemID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_SaleItemID.ParameterName = "@SaleItemID"
dbParam_SaleItemID.Value =
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex))
dbParam_SaleItemID.DbType = System.Data.DbType.Int32
DelItemCommand.Parameters.Add(dbParam_SaleItemID)

DelItemCommand.Connection.Open()

Try
DelItemCommand.ExecuteNonQuery()
lblmsg.Text = "<b>Record [SaleItemID=" &
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex)) & "]
Deleted said:
Catch Exc As SQLException
lblmsg.Text = "ERROR: Could not delete record"
End Try

DelItemCommand.Connection.Close()

BinddgSoldItems()
End Sub


.
 
C

cj

When I did that, none of the buttons worked at all (neither Edit or Remove).

cj

Elton W said:
Hi CJ,

Change the datagrid's EnableViewState from false to true.
Although disabled viewstate can improve performance of the
datagrid, it also causes many functionalities not work
properly.

HTH

Elton Wang
(e-mail address removed)

-----Original Message-----
Actually, my problem looks similar/same as Bruce. E. Bonsall ("Dynamic
EditCommandColumn events") who posted earlier today.. but still no answer...

Thanks

cj


I have created a datagrid with both EditCommandColumn and ButtonColumn
(setup as Delete). Even when I change the Delete command to a different
name "Remove", the "Cancel" button always ends up doing the 'Delete/Remove'
subroutine :(

What is happening with this? I searched on the net and only found one
similar occurance, but no answer :( Surely it is a known problem..?

Below is some of my code.

Any help out there (I've spent hours trying to nut this out...)?

Thanks

cj

<asp:datagrid
id="dgSoldItems"
runat="server"
AutoGenerateColumns="False"
EnableViewState="False"
ForeColor="Black"
BackColor="White"
CellPadding="3"
CellSpacing="1"
Font-Size="X-Small"
GridLines="None"
DataKeyField="SaleItemID"
OnEditCommand="dgSoldItems_Edit"
OnCancelCommand="dgSoldItems_Cancel"
OnUpdateCommand="dgSoldItems_Update"
OnDeleteCommand="dgSoldItems_Delete"

<ItemStyle
backcolor="#DEDFDE"></ItemStyle>
<HeaderStyle font- bold="True"
forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
EditText="Edit" CancelText="Cancel" UpdateText="Update"
ItemStyle-Wrap="false" ButtonType="PushButton"/>
<asp:ButtonColumn Text="Remove
Item" CommandName="Delete" ButtonType="PushButton"/>
<asp:BoundColumn DataField="ItemID"
HeaderText="Item ID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Price"
HeaderText="Price"
DataFormatString="{0:c}"> said:
<asp:BoundColumn
DataField="ProductName" HeaderText="Product Name"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="ModelNumber" HeaderText="Model Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SerialNumber" HeaderText="Serial Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="AssetNumber" HeaderText="Asset Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="SaleID"
HeaderText="SaleID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SaleItemID" HeaderText="SaleItemID"
ReadOnly="True"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Sub dgSoldItems_Edit(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = CInt(E.Item.ItemIndex)
BinddgSoldItems()
End Sub

Sub dgSoldItems_Cancel(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = -1
BinddgSoldItems()
End Sub

Sub dgSoldItems_Update(Sender As Object, E As DataGridCommandEventArgs)
End Sub

Sub dgSoldItems_Delete(Sender As Object, E As DataGridCommandEventArgs)

Dim DelItemCommand As SqlCommand
Dim DeleteCmdText As String = "DELETE from Sold_Items where
SaleItemID = @SaleItemID"

DelItemCommand = New SqlCommand (DeleteCmdText, MyConnection)

Dim dbParam_SaleItemID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_SaleItemID.ParameterName = "@SaleItemID"
dbParam_SaleItemID.Value =
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex))
dbParam_SaleItemID.DbType = System.Data.DbType.Int32
DelItemCommand.Parameters.Add(dbParam_SaleItemID)

DelItemCommand.Connection.Open()

Try
DelItemCommand.ExecuteNonQuery()
lblmsg.Text = "<b>Record [SaleItemID=" &
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex)) & "]
Deleted said:
Catch Exc As SQLException
lblmsg.Text = "ERROR: Could not delete record"
End Try

DelItemCommand.Connection.Close()

BinddgSoldItems()
End Sub


.
 
E

Elton W

The event handler should look like

Private Sub DataGrid1_EditCommand(ByVal source As Object,
ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles dgSoldItems.EditCommand
' Code

End Sub

Your code misses something like

Handles dgSoldItems.EditCommand

Without these key works, it's just normal method rather
than event handler.


HTH

Elton Wang

-----Original Message-----
When I did that, none of the buttons worked at all (neither Edit or Remove).

cj

Hi CJ,

Change the datagrid's EnableViewState from false to true.
Although disabled viewstate can improve performance of the
datagrid, it also causes many functionalities not work
properly.

HTH

Elton Wang
(e-mail address removed)

-----Original Message-----
Actually, my problem looks similar/same as Bruce. E. Bonsall ("Dynamic
EditCommandColumn events") who posted earlier today..
but
still no answer...
Thanks

cj


03.brisbane.pipenetworks.com.au...
I have created a datagrid with both EditCommandColumn and ButtonColumn
(setup as Delete). Even when I change the Delete
command
to a different
name "Remove", the "Cancel" button always ends up doing the 'Delete/Remove'
subroutine :(

What is happening with this? I searched on the net and only found one
similar occurance, but no answer :( Surely it is a known problem..?

Below is some of my code.

Any help out there (I've spent hours trying to nut
this
out...)?
Thanks

cj

<asp:datagrid
id="dgSoldItems"
runat="server"
AutoGenerateColumns="False"
OnEditCommand="dgSoldItems_Edit"
OnCancelCommand="dgSoldItems_Cancel"
OnUpdateCommand="dgSoldItems_Update"
OnDeleteCommand="dgSoldItems_Delete"

<ItemStyle
backcolor="#DEDFDE"></ItemStyle>
<HeaderStyle font- bold="True"
forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:EditCommandColumn
EditText="Edit" CancelText="Cancel" UpdateText="Update"
ItemStyle-Wrap="false" ButtonType="PushButton"/>
<asp:ButtonColumn Text="Remove
Item" CommandName="Delete" ButtonType="PushButton"/>
<asp:BoundColumn
DataField="ItemID"
HeaderText="Item ID"
<asp:BoundColumn
DataField="Price"
HeaderText="Price"
DataFormatString="{0:c}"> said:
<asp:BoundColumn
DataField="ProductName" HeaderText="Product Name"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="ModelNumber" HeaderText="Model Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SerialNumber" HeaderText="Serial Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="AssetNumber" HeaderText="Asset Number"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SaleID"
HeaderText="SaleID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn
DataField="SaleItemID" HeaderText="SaleItemID"
ReadOnly="True"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Sub dgSoldItems_Edit(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = CInt(E.Item.ItemIndex)
BinddgSoldItems()
End Sub

Sub dgSoldItems_Cancel(Sender As Object, E As DataGridCommandEventArgs)
dgSoldItems.EditItemIndex = -1
BinddgSoldItems()
End Sub

Sub dgSoldItems_Update(Sender As Object, E As DataGridCommandEventArgs)
End Sub

Sub dgSoldItems_Delete(Sender As Object, E As DataGridCommandEventArgs)

Dim DelItemCommand As SqlCommand
Dim DeleteCmdText As String = "DELETE from Sold_Items where
SaleItemID = @SaleItemID"

DelItemCommand = New SqlCommand (DeleteCmdText, MyConnection)

Dim dbParam_SaleItemID As
System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_SaleItemID.ParameterName = "@SaleItemID"
dbParam_SaleItemID.Value =
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex))
dbParam_SaleItemID.DbType = System.Data.DbType.Int32
DelItemCommand.Parameters.Add (dbParam_SaleItemID)

DelItemCommand.Connection.Open()

Try
DelItemCommand.ExecuteNonQuery()
lblmsg.Text = "<b>Record [SaleItemID=" &
dgSoldItems.DataKeys(CInt(E.Item.ItemIndex)) & "]
Deleted said:
Catch Exc As SQLException
lblmsg.Text = "ERROR: Could not delete record"
End Try

DelItemCommand.Connection.Close()

BinddgSoldItems()
End Sub





.


.
 
E

Elton W

Any button in a datagrid, including edit, delete, and so
on, will fire datagrid_ ItemCommand event. So you can
build the event in codebehind like follows:

Private Sub dgSoldItems_ItemCommand(ByVal source As
Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles dgSoldItems.ItemCommand
Dim command_Name As String = e.CommandName

End Sub

Then you can set a breakpoint in the event to trace if the
program gets correct button command name. Edit button's
command name is Edit.

HTH

Elton
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top