Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

confirmation message before deleting in datagrid

Status
Not open for further replies.

meenakshidhar

Programmer
Oct 19, 2001
77
MY
Hi,
I want to display the confirmation message before deleting any record from datagrid...

HTML Code--
__________________________________________________________

<ASP:DATAGRID id="DataGrid1"
runat="server"
CellPadding="3"
BorderWidth="1px"
AutoGenerateColumns="False"
OnDeleteCommand="DataGrid1_Delete"
OnEditCommand="DataGrid1_Edit"
OnItemDataBound="DataGrid1_ItemDataBound"
OnSortCommand="SortCommand_Click"
AllowSorting="True"
PageSize="10"
OnPageIndexChanged="PageIndexChanged_OnClick"
AllowPaging="True">

<Columns>

<asp:BoundColumn visible="false" DataField="LOCATION_ID">
</asp:BoundColumn>

<asp:BoundColumn SortExpression="COMPANY_NAME" DataField="COMPANY_NAME" HeaderText="Company Name">
</asp:BoundColumn>

</Columns>

<Columns>

<asp:EditCommandColumn EditText="Edit Info" ButtonType="PushButton" UpdateText="Update" CancelText="Cancel">
</asp:EditCommandColumn>

</Columns>

<Columns>

<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="delete"></asp:ButtonColumn>

</Columns>
</ASP:DATAGRID>
_________________________________________________________



ASP Code--
_________________________________________________________

Sub DataGrid1_Delete(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this item?');")
End If
Dim intLocID As Integer = e.Item.Cells(0).Text.Trim

'the above code is not wrking properly..i.e. after seleceting delete button twice then only this confirmation box appears...and next issue is that after clicking 'Ok', only then the below code shld be executed...if User selects 'cancel' then below code shld not be executed...

Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter
Dim DS As DataSet

MyConnection = New SqlConnection("Data Source=INFO3783;" _
& "Initial Catalog=GTD_Test;User Id=gtd;Password=gtd;" _
& "Connect Timeout=15;Network Library=dbmssocn;")
MyCommand = New SqlDataAdapter(Delete from GTD_M_ORG where LOCATION_ID=intLocID", MyConnection)
DS = New DataSet()
MyCommand.Fill(DS)

End Sub
__________________________________________________________

Urgent Help needed..

Regards,
Meenakshi Dhar



 
The reason is that you are adding the confirmation button when a record is being deleted and therefore it won't appear until you click the button for the second time (as you have noticed).

Try adding the "onclick" attribute in the code for your delete button. e.g.

Code:
MyDeleteButton.Attributes("onclick") = _
  "return confirm('Are you sure you wish to _ 
                     delete this record?');"

There is a good example of datagrids (including adding a confirm message - this is on page 2) by Karl Moore at:



----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
but it checks only for Button control, not a PushButton..i m using PushButton...
 
OK in that case place your "onclick" code in the ItemCreated event of the datagrid...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
can u plss tell me how to do that..i m new to asp.net...

wht i m changing is i put OnItemCreated in Datagrid--

<ASP:DATAGRID id="DataGrid1"
runat="server"
CellPadding="3"
BorderWidth="1px"
AutoGenerateColumns="False"
OnDeleteCommand="DataGrid1_Delete"
OnEditCommand="DataGrid1_Edit"
OnItemDataBound="DataGrid1_ItemDataBound"
OnItemCreated="DataGrid1_ItemCreated"
OnSortCommand="SortCommand_Click"
AllowSorting="True"
PageSize="10"
OnPageIndexChanged="PageIndexChanged_OnClick"
AllowPaging="True">

after that i add this procedure

Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or _ e.Item.ItemType = ListItemType.AlternatingItem Then

If e.Item.Cells(0).Controls.Count > 0 Then
If TypeOf (e.Item.Cells(0).Controls(0)) Is Button Then
Dim btnDelete As Button = CType(e.Item.Cells(0). _
Controls(0), Button)
btnDelete.Attributes("onclick") = _
"return confirm('Are you sure you wish to delete this record?');"
End If
End If
End If

End Sub


but it's not wrking...

Regards,
Meenakshi Dhar
 
well, thx a lot ca8msm..i got the solution...

these below mentioned lines which i included in DataGrid1_delete, should be included in ItemDataBound..

_______________________________________________________

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this item?');")
End If
_______________________________________________________


and its is perfectly running fine now...

Regards,
Meenakshi Dhar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top