How can you dynamically name a cfform or cfgrid?

Lee R Liddick Jr
Lee R Liddick Jr used Ask the Experts™
on
I have a cfgrid inside a loop so that a different grid is created for each iteration of the loop. The problem is, to show all the grids, they each have to have a different name. How can I dynamically name each grid so that this works?

I tried setting up the FormName in a CFset, but CFgrid does not like the #FormName# in the name.
<cfset GrdHeight = (qryLOBContact.recordcount * 20) + 20 >
<cfset FormName = "#LOB_ID#ContactList"><cfdump var="#FormName#">
 
<cfform format="flash" action="?Act=upd" name="ContactList" skin="haloBlue" accessible="true" height="#FrmHeight#" width="935">
<cfgrid name="#FormName" format="flash" query="qryLOBContact" height="#GrdHeight#" colheaderbold="true" colheadertextcolor="##067ab4" >

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Commented:
I'm assuming your are only creating _one_ form, but multiple cfgrids.  I haven't tried it, but your code should work, or maybe a slight variation:

<cfloop from="1" to="#howeverManyGrids#" index="counter">
    <cfgrid name="ContactList#counter#" ....>
</cfloop>

OR using a query

<cfloop query="yourQuery" >
    <cfgrid name="ContactList#currentRow#" ....>
</cfloop>

hope your error is not because you are missing the closing # in <cfgrid name="#FormName"...

'cos as _agx_ said, it should work just fine

Azadi
Lee R Liddick JrReporting Analyst

Author

Commented:
The problem is, ANY time I put # in the cfgrid name, it fails. When I just name it, it works for the first grid and fails for the rest.  I've tried dynamically naming the forms and the grids, because I don't care which one truthfully, as long as the out put works.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Lee R Liddick JrReporting Analyst

Author

Commented:
When I use the code in the post this is what happens:  The queries and data from the loop show up, but not the grids.
Most Valuable Expert 2015

Commented:
It has to be somethin else in your code, because this works just fine:

<cfset howeverManyGrids = 5>
<cfform format="flash" action="?Act=upd" name="ContactList" accessible="true" width="935">
	<cfloop from="1" to="#howeverManyGrids#" index="counter">
		<cfoutput></cfoutput>
    	<cfgrid name="ContactList#counter#" format="flash">
			<cfgridcolumn name="firstName" header="First Name">		
			<cfgridcolumn name="lastName" header="LastName">
		</cfgrid>
	</cfloop>
</cfform>

Open in new window

Most Valuable Expert 2015

Commented:
>  <cfoutput></cfoutput>

Minus that line of course (not needed)
yes, i can also assure you that a cf variable as name of cfgrid works just fine.

does your #LOB_ID# evaluate to a number?
irc, cf does not like variable names that start with a number.

change your code to this:
<cfset FormName = "ContactList" & LOB_ID><cfdump var="#FormName#">

Azadi
Lee R Liddick JrReporting Analyst

Author

Commented:
Okay I tried that and your example does work...let me look at my other code and figure out what I have that is conflicting.  I will post back with questions or award points from there.  Thank you!  Great start here.
yep, confirmed: make sure your dynamic names DO NOT start with a number. only letter or _ (underscore).

Azadi
Most Valuable Expert 2015

Commented:
> DO NOT start with a number. only letter or _ (underscore).

Yes, non standard field names can create those subtle problems that are hard to spot.  Even though you can do it in some places .. my advice is don't ;-)
Lee R Liddick JrReporting Analyst

Author

Commented:
Okay...after fussing with this I got it.  Thanks to you both!  I will get the points award and close the question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial