Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Variables inside a DataList ItemTemplate Loaded from a File.


Message #1 by "Anthony Bouch" <tony@a...> on Tue, 29 Oct 2002 00:20:37
Hi All.

I'm loading a couple of different Itemtemplate files (default.ascx, 
test.ascx etc) for a DataList control on my main Webform page. The user 
can choose which to load.

What I'd like to know is how can I access, or set, the values of variables 
I would like to use in my template. I have a calculated variable that is 
not part of the datasource. At the moment this variable is in the Webform 
page i.e. string myVar = "Calculated Value".

The main Webform page is called QuickSearch.aspx.

The ItemTemplate File is: /templates/test.ascx

On Page Load - in QuickSearch.aspx

gridResults.ItemTemplate = Page.LoadTemplate("templates/test.ascx");

Before I was using a template file - the variable 'myVar' was bound to the 
page using the standard <%# myVar %> - within the ItemTeplate section.

But now that I'm loading the template from a file - I get a compile error 
that says: The name 'myVar' does not exist in the class or 
namespace 'ASP.test_ascx'

For fields inside the loaded Itemtemplate test.ascx that use <%# 
DataBinder.Eval(((DataListItem)Container).DataItem, "Fieldname") %> - no
problem.

But if I have <%# myVar %> in the template (which was declared and 
initialised in QuickSearch.aspx) - how do I reference this variable? 

If I include the name of the main page - QuickSearch - as a classname ? 
and the namespace - i.e. QuickSearch.myVar I get a compile error that say 
I need an object reference to the non-static member or field. I don't want 
to make this variable static do I?

Or should I do this the other way round - and declare the variable in the 
Itemtemplate file - and the get a reference to this from the main page?

It all works fine when the Itemtemplate is in the page itself and not 
loaded from a file as an .ascx. file.

Help!

Best regards,

Anthony Bouch

  Return to Index