Wrox Programmer Forums
|
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x 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 March 16th, 2008, 11:42 PM
Registered User
 
Join Date: Mar 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Access ascx filename at runtime

Hi All,
I have an aspx page onto which I have dragged my UserControl (ascx). I want to be able to access the ascx filename (eg. test.ascx) during the Page_Load event of the ascx. Is this possible?
Thanks,
Steele.
 
Old March 17th, 2008, 08:57 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I don't know of a way to do that directly. Making the assumption that you follow the names that visual studio provides (given that you are using VS) you could construct the file name from the control class name plus ".ascx" as they should typically match. However, this is not guaranteed.

Can I ask why you want to do this? What goal are looking to achieve? Perhaps there's a better way to meet it.


-Peter
peterlanoie.blog
 
Old March 17th, 2008, 06:10 PM
Registered User
 
Join Date: Mar 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Basically what I am trying to achieve is a unique reference to controls on a page so that I can store a database table of aliases for labels.
This is so a client can tailor labels to their own terminology. In the database I store Page, ControlId and Label so I can get all the pages Labels and apply them to the controls on Page_Load.
Is there a better way to apply this functionality?
 
Old March 17th, 2008, 08:00 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When you say "alias" do you mean the text value of the label? You are storing user customizable label text right?

Here's the thing about user controls: When you have an instance of a user control on your page, it becomes just one of many control instances in the page's control tree. The fact that the control starts life as an ascx page is basically irrelevant. So using the ascx file name will be difficult if not impossible (I don't think there is any way to extract that from any class member of the control class).

You would be much better off to use the control's class name. Even better still would be to tag your user control with a custom attribute instead of relying on its class name.

Despite these ideas, I'm concerned with the solution you propose. Putting page and control Ids into the database introduces very tight coupling between your database layer and your presentation layer. This is typically something you should strive to keep well partitioned. What happens if you change a page's controls? Or the page/control file name? What about if you want to reuse various label values in other places but the page/control names don't apply?

For these reasons you should consider establishing keys (possibly text based) to represent the units of text in the database you will use for the label controls text property. The controls/pages that contain these labels can set the label text in their page_load method.

One trick I have used is to utilize the flexibility of the control attributes collection. You could add an attribute to a control's markup tag to identify the key that holds the text you want to assign to it. In the code behind you can interrogate the control's Attributes collection for that value, use it as needed, then remove it.

Although I haven't tried it so I'm not sure if/how it can be done, you might want to look into a custom resource manager. I believe that you can assign some kind of resource binding to controls such that the resource manager handles getting a resource (i.e. a localized string value) and assigning it to the bound property (i.e. <label>.Text). I'd be surprised if you could not create your own resource manager that does just what you need to do (get a user set value from your DB and return that value) but still relies on the standard resource binding for handling the control assignment task. Just in this case you have a custom resource manager to handle user-preferred instead of culture-preferred values.

-Peter
peterlanoie.blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Redistributable Access Runtime bln BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 0 September 24th, 2007 01:39 PM
use filename in access DB with a filepicker paul20091968 VB Databases Basics 0 April 4th, 2006 06:38 AM
Access 2003 Runtime Clarence Morgan Access 1 December 3rd, 2004 01:23 AM
Creating Access database at runtime janigorse Access 0 September 18th, 2004 03:48 AM
Access RunTime Ben Access 22 February 20th, 2004 10:27 AM





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