Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: FYI, for working with controls embedded in a datalist.


Message #1 by "Oliver, Wells" <WOliver@l...> on Wed, 28 Aug 2002 10:06:41 -0700
I just figured this out, and I'm so happy I had to share. This might be
obvious to some, and it might help others. Guess that's what this list is
for, eh?

I had a label control in a datalist EditItemTemplate that I couldn't figure
out how to manipulate. I built a button control and had an onClick event and
worked for days to figure it out with no luck.

The thing is, to work with controls embedded in a datalist you have to
assign the datalist an OnItemCommand event. So:

<asp:dataList
	id="testRequest"
	CellPadding="0"	CellSpacing="0"
	CssClass="projectDataHeaderOutline"
	DataKeyField="testNumber"
	OnEditCommand="DoItemEdit"
	OnUpdateCommand="DoItemUpdate"
	OnItemCommand="DoItemCommand"
	runat="server">

And then I wrote a sub to handle that event in order to manipulate the
embedded label control in the EditItemTemplate:

	Public Sub DoItemCommand (S as System.Object, E as
DataListCommandEventArgs)
	' Handles buttons within the testRequest datalist
		Dim dateStr as String
		Dim compareValue as Integer
		Dim newDate as DateTime

		Select E.CommandName
			Case "completedDate_Add"
				' Add a day to the date
				dateStr = CType (E.Item.FindControl
("completedDate"), Label).Text
				CType (E.Item.FindControl ("completedDate"),
Label).Text = DateTime.Parse (dateStr).AddDays (1).toShortDateString ()
			Case "completedDate_Add"
				' Subtract a day from the date but ensure
that it's not prior to the start date
				dateStr = CType (E.Item.FindControl
("completedDate"), Label).Text
				newDate = DateTime.Parse(dateStr).AddDays
(-1)
				compareValue = newDate.compareTo
(DateTime.Parse(labDate.Text))
				if (compareValue >= 0) Then
					dueDate.Text 
newDate.toShortDateString ()
				End If
		End Select
	End Sub

So that works! My code involves some other labels but you can see how it
happens.

Hope this helps someone-- spent all day yesterday pulling out hairs over the
issue.

Wells Oliver
Web Application Programmer
Leviton Voice & Data
xxx-xxx-xxxx
http://www.levitonvoicedata.com

Message #2 by "David Adames" <david@p...> on Wed, 28 Aug 2002 15:10:14 -0400
I have a similar but slightly different problem which I have listed here
previously.  I have a panel control used to manage the layout of other
controls.  I have a datalist which has linkbuttons.  Via code, when I add
the datalist to the panel's controls collection, then display the datalist
and click one of its linkbuttons the linkbutton event does not fire.
However, if I do not use the panel then I can fire the linkbutton event.
Any ideas?

Thanks,
David X


-----Original Message-----
From: Oliver, Wells [mailto:WOliver@l...]
Sent: Wednesday, August 28, 2002 1:07 PM
To: ASP+
Subject: [aspx] FYI, for working with controls embedded in a datalist.


I just figured this out, and I'm so happy I had to share. This might be
obvious to some, and it might help others. Guess that's what this list is
for, eh?

I had a label control in a datalist EditItemTemplate that I couldn't figure
out how to manipulate. I built a button control and had an onClick event and
worked for days to figure it out with no luck.

The thing is, to work with controls embedded in a datalist you have to
assign the datalist an OnItemCommand event. So:

<asp:dataList
	id="testRequest"
	CellPadding="0"	CellSpacing="0"
	CssClass="projectDataHeaderOutline"
	DataKeyField="testNumber"
	OnEditCommand="DoItemEdit"
	OnUpdateCommand="DoItemUpdate"
	OnItemCommand="DoItemCommand"
	runat="server">

And then I wrote a sub to handle that event in order to manipulate the
embedded label control in the EditItemTemplate:

	Public Sub DoItemCommand (S as System.Object, E as
DataListCommandEventArgs)
	' Handles buttons within the testRequest datalist
		Dim dateStr as String
		Dim compareValue as Integer
		Dim newDate as DateTime

		Select E.CommandName
			Case "completedDate_Add"
				' Add a day to the date
				dateStr = CType (E.Item.FindControl
("completedDate"), Label).Text
				CType (E.Item.FindControl ("completedDate"),
Label).Text = DateTime.Parse (dateStr).AddDays (1).toShortDateString ()
			Case "completedDate_Add"
				' Subtract a day from the date but ensure
that it's not prior to the start date
				dateStr = CType (E.Item.FindControl
("completedDate"), Label).Text
				newDate = DateTime.Parse(dateStr).AddDays
(-1)
				compareValue = newDate.compareTo
(DateTime.Parse(labDate.Text))
				if (compareValue >= 0) Then
					dueDate.Text 
newDate.toShortDateString ()
				End If
		End Select
	End Sub

So that works! My code involves some other labels but you can see how it
happens.

Hope this helps someone-- spent all day yesterday pulling out hairs over the
issue.

Wells Oliver
Web Application Programmer
Leviton Voice & Data
xxx-xxx-xxxx
http://www.levitonvoicedata.com


---

ASP.NET 1.0 Namespace Reference with C#
http://www.wrox.com/acon11.asp?ISBN=1861007442

ASP.NET 1.0 Namespace Reference with VB.NET
http://www.wrox.com/acon11.asp?ISBN=1861007450

These books are a complete reference to the ASP.NET namespaces
for developers who are already familiar with using ASP.NET.
There is no trivial introductory material or useless .NET
hype and the presentation of the namespaces, in an easy-to use
alphabetical order ensures a user-friendly reference format.
We provide in-depth coverage of all the major ASP.NET classes,
giving you those real-world tips that the documentation doesn't
offer, and demonstrating complex techniques with simple
examples.

---


  Return to Index