Results 1 to 8 of 8

Thread: UserControls

  1. #1
    Join Date
    May 2005
    Posts
    2

    Default UserControls

    I'm rather new to the Spring framework, so please excuse if this is a 'stupid' Q:

    I've done a page that extends Spring.web.UI.Page, I've also made my control extend Spring.web.UI.UserControl.

    Now what I don't understand is:
    1) how do I define the user control on the XML?
    2) how will my objects (in XML) look to represent a form using a user control?

    I've tried quite a few ways but if I define it as an object with type xyz.ascx it tells me the type can not be found and when I define it as a object with type the fully qualified classname, it goes into a end-less loop killing my site.

    If anyone has an example it will also help greatly.

    Thanks

  2. #2
    Join Date
    Mar 2005
    Posts
    78

    Default

    When we migrated our controls, I don't remember changing the XML reference. It's still just

    Code:
    <%@ Register TagPrefix="wne" TagName="header" Src="header.ascx" %>
    <%@ Register TagPrefix="wne" TagName="footer" Src="footer.ascx" %>
    Ditto for the inline references.

    Code:
    <wne&#58;header id=header runat="server"></wne&#58;header>
    We only use two user controls, and they are very simple.

    -Ted.

  3. #3
    Join Date
    Sep 2004
    Location
    Belgrade, Serbia
    Posts
    613

    Default

    Hi,

    You cannot define user controls in the config file directly, and I can't think of a case where that would be necessary. What you can do is define control in the context of the page, by specifying unique ID of the control as a value of the name attribute. This usually works best when page and its controls are defined in the child application context.

    However, keep in mind that this feature injects dependencies into controls after Init events for the page and controls fire, but before Load -- this means that you cannot use injected dependencies within OnInit method (or Init event handler) of your control.

    - Aleks

  4. #4
    Join Date
    May 2005
    Posts
    13

    Default Example of dependency injection for a user control

    Hi,

    Any chance someone could post an example of the xml for setting a property of a user control. I'm using the master pages stuff so my controls are inside a contentplaceholder. I've had a go with no sucess and couldn't find anything in the help for the spring.web. The controls are derived from Spring.Web.UI.UserControl. My page aspx file is shown below



    Code:
    <%@ Page language="c#" Codebehind="ArtistDetail.aspx.cs" AutoEventWireup="false" Inherits="HardBopRecords.WebApp.ArtistDetail" %>
    <%@ Register TagPrefix="uc1" TagName="ArtistCatalogSummary" Src="~/Controls/ArtistCatalogSummary.ascx" %>
    <%@ Register TagPrefix="uc1" TagName="ArtistCollaborationSummary" Src="~/Controls/ArtistCollaborationSummary.ascx" %>
    <%@ Register TagPrefix="spring" Namespace="Spring.Web.UI.Controls" Assembly="Spring.Web" %>
    <HTML>
    	<body>
    		<spring&#58;content id="_mainContent" runat="server" contentPlaceholderId="_contentPlaceholder">
    			<TABLE class="PageTable" id="ArtistDetailPageTable" cellspacing="30" border="0">
    				<tr>
    					<td valign="top" class="ArtistDetailName"><%#Artist.Name%></td>
    				</tr>
    				<tr>
    					<td rowspan="2" class="ArtistDetailDescription"><%#Artist.Description.Substring&#40;0, 1&#41;%>[img]images/artist_small_<%#Artist.ImageFileName%>.gif[/img]</img><%#Artist.Description.Substring&#40;1&#41;%></td>
    					<td valign="top">
    						<uc1&#58;ArtistCatalogSummary id="_artistCatalogSummary" Artist="<%#Artist%>" runat="server"></uc1&#58;ArtistCatalogSummary>
    						<uc1&#58;ArtistCollaborationSummary id="_artistCollaborationSummary" Artist="<%#Artist%>" runat="server"></uc1&#58;ArtistCollaborationSummary>
    					</td>
    				</tr>
    				<tr>
    					<td valign="top">
    					</td>
    				</tr>
    			</TABLE>
    		</spring&#58;content>
    	</body>
    </HTML>
    Any help much appreciated, probably me just being a bit thick.

  5. #5
    Join Date
    May 2005
    Posts
    13

    Default Figured it out

    Checked the Spring source and figured it out.

    Used the id of the control in my page as the name attribute.

    Code:
    <object name="_artistCatalogSummary" type="HardBopRecords.WebApp.Controls.ArtistCatalogSummary, HardBopRecords.WebApp" singleton="false">
    				<property name="Controller">
    					<ref object="ArtistController" />
    				</property>	
    			</object>
    If I used this control on various pages, each with a different Id I'd have to put multiple entries into the config xml. Is there not a way to define the object once for all instances, regardless of id?

  6. #6
    Join Date
    Sep 2004
    Location
    Belgrade, Serbia
    Posts
    613

    Default

    Sorry I missed original message...

    Yes, there is a way -- you can use full type name of the control (HardBopRecords.WebApp.Controls.ArtistCatalogSumma ry in your case) to inject properties into all instances of the control.

    To inject values into specific instance of the control you need to use control's unique ID as object definition id/name. That is not quite the same as control's id, because it also contains ids of any naming containers that conrtol is nested within. Unique ID is a string that concatenates all naming containers' ids and control id, using colon as a separator.

    As a sidenote, if you want to use different settings for the same control on different pages, you don't have to use different ids for the control. You can simply use hierarchical contexts and use separate context definition for each page or component. Each page/component context will be able to access all of the common definitions from root context, while at the same time hiding its own definitions from other page/component contexts.

    You can define page/component context simply by creating Web.config in the same directory where your page is. Of course, this requires that you put different components (set of one or more related pages) into separate directories, but you should probably do that anyway to make application layout cleaner.

    Hope this helps,

    Aleks

  7. #7
    Join Date
    May 2005
    Posts
    13

    Default Thanks

    Excellent, thanks very much...it's all starting to make sense now.

  8. #8
    Join Date
    May 2005
    Posts
    13

    Default Pages and controls in sub directories

    Hi

    I tried, as recommended, placing some pages and controls, in this case admin pages, in a sub folder of my main site. I added a web.config file with the object definitions for the pages and controls. Now I have encoutered a problem. If I start the application by visiting a page in the root directory, I can't then access a page in the sub directory without getting an error that the object (the page) cannot be found in the context. The reverse is true if I start in with a page in the sub folder and then navigate to a page in the root.

    Any ideas what I'm doing wrong, or does anyone have a simple example of this feature in action that I can learn from?

    Cheers :O)

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2006, 02:44 PM
  2. Replies: 1
    Last Post: 03-23-2006, 02:08 AM
  3. hierarchical contexts
    By rich in forum Web
    Replies: 7
    Last Post: 09-20-2005, 12:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •