Confirm before delete

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

How do i confirm before delete from the datagrid?
I did like this. But getting error... "Object Reference not set to an
instance of object"

<asp:datagrid id="dgbrand" Runat="server" Font-Size="12px" Font-Name="arial"
BorderColor="SteelBlue" OnEditCommand="editMode" OnUpdateCommand="doUpdate"
OnCancelCommand="cancelEdit" DataKeyField="brand_code"
AutoGenerateColumns="False" Width="400px" Font-Names="arial"
OnDeleteCommand="doDelete"> <AlternatingItemStyle
BackColor="#CCCCCC"></AlternatingItemStyle> <HeaderStyle Font-Bold="True"
BackColor="Salmon"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="CAN" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn DataField="brand_code" ReadOnly="True" HeaderText="Brand
Code">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="brand_name"
HeaderText="Description"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="cmddel" Runat="server" Font-Underline="True"
ForeColor="#0000ff" CommandName="Delete">Del</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn></Columns> </asp:datagrid>

Private Sub dgbrand_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgbrand.ItemDataBound
Dim mybut As LinkButton
mybut = e.Item.FindControl("cmddel")
mybut.Attributes.Add("onclick", "return confirm('Are you sure to
delete?')")
End Sub


And, i placed the controls manually in the table tag.
When i rebuild the project, automatically some unnecessary table is adding
in VS.NET that changes my actual alignment. How can i stop this?

Thanx in advance
 
The problem is in the ItemBound() EventHandler. it runs on every item in the
DataGrid, including Header, Item, AlternateItem, Footer, while the "Delete"
button does not present on Header and Footer, hense you get "Object
Reference not set" when the e.Item is Header/Footer.

You can either

System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgbrand.ItemDataBound
Dim mybut As LinkButton
If e.Item.ItemType=ListItemType.Item Or
e.Item.ItemType=ListItemType.AlternateItem Then
mybut = e.Item.FindControl("cmddel")
mybut.Attributes.Add("onclick", "return confirm('Are you sure
to delete?')")
End If
End Sub

Or simply:

System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgbrand.ItemDataBound
Dim mybut As LinkButton
mybut = e.Item.FindControl("cmddel") '''mybut could be Nothing
id the e.Item is Header/Footer or other row that does not have the "Delete"
button
If Not (mybut Is Nothing) Then
mybut.Attributes.Add("onclick", "return confirm('Are you sure
to delete?')")
End If
End Sub
 
Hello,

Thanx 4 the reply. I got it now. But, i have another problem...
I have a table with fields sizeCode,sizeRange,s1,s2,..s12,r1,r2,...r12
I designed a form to perform Addition, Edit and Delete
Addition is performed thru ordinary form(jus by placing all the reqd
controls and do insert on button click). When i click Edit link on datagrid,
its placing textboxs to all editable columns. As the columns are many, the
datagrid is expanding to long width coz the textbox is placed with size 20 on
each columns. This doesnt look easy to edit. I want to do to retrieve all the
column values on to my form when i click edit and do manually like addition.
How can i retrieve all the values and corr datakey(sizeCode)?
 
Hi,

I did as u said. But it is asking confirmation for alternating rows only
not all rows.
How come its like that? May be the condition is ListItem.AlternatingItem ???
How can i change this to do 4 all?
 
=?Utf-8?B?UmFqYW5p?= said:
Hi,

I did as u said. But it is asking confirmation for alternating rows only
not all rows.
How come its like that? May be the condition is ListItem.AlternatingItem ???
How can i change this to do 4 all?




"Norman Yuan" wrote:
<FONT color=blue>
> The problem is in the ItemBound() EventHandler. it runs on every item in the
> DataGrid, including Header, Item, AlternateItem, Footer, while the "Delete"
> button does not present on Header and Footer, hense you get "Object
> Reference not set" when the e.Item is Header/Footer.
>
> You can either
>
> System.Web.UI.WebControls.DataGridItemEventArgs) Handles
> dgbrand.ItemDataBound
> Dim mybut As LinkButton
> If e.Item.ItemType=ListItemType.Item Or
> e.Item.ItemType=ListItemType.AlternateItem Then
> mybut = e.Item.FindControl("cmddel")
> mybut.Attributes.Add("onclick", "return confirm('Are you sure
> to delete?')")
> End If
> End Sub
>
> Or simply:
>
> System.Web.UI.WebControls.DataGridItemEventArgs) Handles
> dgbrand.ItemDataBound
> Dim mybut As LinkButton
> mybut = e.Item.FindControl("cmddel") '''mybut could be Nothing
> id the e.Item is Header/Footer or other row that does not have the "Delete"
> button
> If Not (mybut Is Nothing) Then
> mybut.Attributes.Add("onclick", "return confirm('Are you sure
> to delete?')")
> End If
> End Sub
>
> "Rajani" wrote in message
> news:[email protected]...<FONT color=green>
> > Hello,
> >
> > How do i confirm before delete from the datagrid?
> > I did like this. But getting error... "Object Reference not set to an
> > instance of object"
> >
> >
> Font-Name="arial"
> > BorderColor="SteelBlue" OnEditCommand="editMode"

> OnUpdateCommand="doUpdate"<FONT color=green>
> > OnCancelCommand="cancelEdit" DataKeyField="brand_code"
> > AutoGenerateColumns="False" Width="400px" Font-Names="arial"


> > OnDeleteCommand="doDelete">

> > BackColor="#CCCCCC"> > > BackColor="Salmon">
> >


> > > > CancelText="CAN" EditText="Edit">


> >
 
Back
Top