Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2006, 07:28 PM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default referencing an object in an edit item template

i have a datalist control in my asp.net application. I would like to put the delete button inside the edititemtemplate and then add somejavascript to confirm the deletion before deleting the record.

currently the delete button is in the itemtemplate and the code works fine. the delete button also works within the edititemtemplate

the problem i have is in trying to reference the delete button once it is in the edititemtemplate. all my attempts so far have resulted in a "Object reference not set to an instance of an object.” error.

is there anyway to reference an object such as a button inside an
edititemtemplate of an asp.net control.



the code i have written which makes the 'confirm delete'
work within the itemtemplate looks like this.

(vb.net)



 sub confirm_delete (obj as object, e as datalistitemeventargs)

' edit button crashes without this if clause

If e.Item.ItemType <> ListItemType.edititem

dim deletebutton as button

 deletebutton = e.item.findcontrol("btndelete")

deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Delete')"

end if

 end sub

 <asp datalist onitemdatabound = "confirm_delete" ...... />

<itemtemplate>

... all the databound stuff


 <asp:Button Text="Delete" CommandName = "delete" runat =server ID = "btndelete" />

</itemtemplate>


 
Old September 24th, 2006, 08:17 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Try:

Dim deleteButton as NEW button

--Stole this from a moderator

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
 
Old September 25th, 2006, 03:51 AM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for looking at the problem.

i worked out the solution

the confirm delete sub looks like this

        If e.Item.ItemType = ListItemType.edititem


        dim deletebutton as new button

        deletebutton = e.item.findcontrol("btndelete")

        deleteButton.Attributes("onclick") = "javascript:return " & _
            "confirm('Delete')"


        end if

the only other thing i had to do was to set the
'causes validation' value of the delete button to 'false'
in order to make it work. the code works whether or not
you declare deletebutton as NEW button or not. is it
just good programming practice?




 
Old September 25th, 2006, 07:15 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

In general yes, its a good idea to declare the control with New because there are some controls that, if you don't declare them as New, the compilier will throw the Object Reference not set exeception.

--Stole this from a moderator

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid Item Template question dparsons ASP.NET 1.0 and 1.1 Professional 3 March 14th, 2007 09:08 AM
edit item in gridview al-hijjawi Visual Studio 2005 0 December 27th, 2006 03:20 PM
Item Template Query rsm42 ASP.NET 1.0 and 1.1 Basics 0 December 22nd, 2006 07:55 AM
edit item template in a datagrid akshay144 ASP.NET 1.0 and 1.1 Professional 0 October 25th, 2006 05:05 AM
Referencing a Item in a Repeater saturdave ASP.NET 1.0 and 1.1 Basics 5 February 20th, 2004 03:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.