 |
| ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.1 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
|
|
|
|

June 23rd, 2004, 06:26 AM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
OnItemCreated
I have a DataList and I want to add some Javascript to a button in the Datalist. I am guessing that I will use the "OnItemCreated" event to find my control and then load the java script to the button.
The problem is that I can not find the control when I search for it in the event. I am guessing that internally it is assigning a unique ID to each of the buttons that the Datalist is creating and this is why I can not find the control with a "FindControl" method.
So how do I find the control? Here is the code I created...
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Control ctrl = e.Item.FindControl("cmdPPIPEditDelete");
if (ctrl != null)
{
((Button)ctrl).Attributes["onclick"] = "return RecordDeleteConfirmation();";
}
}
|
|

June 23rd, 2004, 06:34 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Each button does indeed get a unique ID, but this is for the browser / output only. Internally, the FindControl accepts the ID of the control as it would appear in your item template. So, you should be able to use one control ID for all your buttons.
So, I'd say you code should work. How does the rest of the grid look like? (The markup for the button). And did you hook up the ItemCreated to the grid? Are you sure this method is firing?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Crime In The City (Sixty to Zero Part I) by Neil Young (Track 2 from the album: Freedom) What's This?
|
|

June 23rd, 2004, 07:26 AM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, the event is firing and it is a DataList and not a DataGrid, not that this makes any difference.
I can see it in the debugger and it does find the "ListItemType.Item" and it does fall into the code where it is invoking the FindControl method, but it never finds the control. I am also fairly confident that FindControl will not find the control with the name I gave it bacause that name is altered when the control is rendered. I did figure out how to do a workaround with the following code which did work, but I am guessing that there is a better way to do this.
foreach (Control ctl in e.Item.Controls)
{
if (ctl != null)
{
if (ctl.ToString() == "System.Web.UI.WebControls.Button")
{
if ( ((Button) ctl).Text == "Delete")
{
((Button)ctl).Attributes["onclick"] = "return
RecordDeleteConfirmation();";
}
}
}
}
|
|

June 23rd, 2004, 08:03 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Like I said in my previous post, the name *is* altered, but at a higher level. On a page, each control needs a unique ID, so that's why the name of the container control is appended to the name of the control to guarantee that unique name.
However, within the scope of e.Item, the name is already unique. I use code like this to append JavaScript to a button:
Button myDeleteButton = (Button)e.Item.FindControl("btnDelete");
The button is called btnDelete, and is located within an ItemTemplate, like this:
<ItemTemplate>
<asp:Button
ID="btnDelete"
CommandName="Delete"
Runat="server"
Text="Delete...">
</asp:Button>
</ItemTemplate>
So, the only reason I can think of why your code wouldn't work, is a different name for the control inside the template, or a different location of the button within the DataList.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

June 24th, 2004, 07:38 AM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, thanks, I have it working now!
|
|

June 24th, 2004, 07:54 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Do you want to share your solution / problem??
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Ondankbare plant by Hans Teeuwen & Pieter Bouwman (Track 6 from the album: De mannen van de radio - improvisaties 2) What's This?
|
|
 |