September 8, 2010  
  You are here:  Home
Module Border Module Border
My Book

 

Module Border Module Border

QTCommunicationSimplified50.gif


Try Radicomm's Quicktalk, the first enterprise-class Push-To-Talk application for your Motorola/Symbol rugged devices, in your distribution center for free.

Click here to enter the 21st Century.


test
 Main Sections
   
test
 My Other Sites
   
Module Border Module Border
Friend's Books

Pro WF/.NET 3.5


Pro WF/.NET 3.0





 

Module Border Module Border
test
 Top Referrals
   
test
 Feedback
 





Cancel   Send
 
test
 Site Map
   

This site has been visited 86407 times.

  Add NetSplore to my favorites.

    Turning Ideas Into Reality  

 

thinker.jpg

Turning your ideas into reality is not just a catchy phrase, it's what we do. It has become expected that legitimate companies have a web presence. But more than likely, website development is not your expertise. What is a company to do?

Let NetSplore create the first impression your potential customers see.  With your ideas and our web savvy, we can create a website that will make a lasting impression on your customers, or prospective customers.

You can have a sales presence 24 hours a day. A support presence 24 hours a day.  You can have sales leads waiting for you when your staff arrives in the morning. Not cold leads, but interested potential customers that took enough interest to find you!

       

Interested in Language Integrated Query (LINQ)?
 

If you are interested in learning about Microsoft's Language Integrated Query (LINQ) for the .NET platform, please visit our other site http://www.linqdev.com.

 

 
.NET 3.5 Books Now Available
 

Pro LINQ: Language Integrated Query in C# 2008


Click here to purchase.

by Joe Rattz

Visit LINQDev.com

Pro WF: Windows Workflow in .NET 3.5


Click here to purchase.

by Bruce Bukovics

Visit LearnWorkflow.com

 
.NET 3.0 Books Available
 

Pro WF: Windows Workflow in .NET 3.0


Click here to purchase.

by Bruce Bukovics

Pro WPF: Windows Presentation Foundation in .NET 3.0


Click here to purchase.

by Matthew MacDonald

Pro WCF: Practical Microsoft SOA Implementation


Click here to purchase.

by Amit Bahree, Shawn Cicoria, Dennis Mulder, Nishith Pathak, Chris Peiris

 
Module Border Module Border
Random Tips Random Tips

F6 == URL Address
In Internet Explorer and Firefox, F6 activates the URL Address box and selects the current URL. This is a real convenient way to quickly change a URL.

Module Border Module Border
Tip Of The Week
 
Free IE Web Development Toolbar
This useful utility plugs into your browser and provides all sorts of useful things. Just to list a few, onscreen rulers and magnifier, html element outliner, html element tree view, styles, etc. Don't leave home without this one from Microsoft.
Free Microsoft Development Environments - Visual Studio 2008 Beta 2
Microsoft's Express editions of Visual Studio 2008 Beta 2 are free and available here.
Free .NET Controls!!!
Some free WinForm and WebForm controls courtesy of Developer Express. Get them while you can.
Site With Nice Images
This subscription site has some very nice graphic images. While there is a fee for the subscription, thumbnail sized images are free.
Really Cool FREE WinForm Controls
This page has a list of some really awesome free WinForm controls.
MSDN Chats
This is your chance to ask the Microsoft technololgy gurus your questions.
Good Stock Photo Site
Need some professional looking images for that Powerpoint presentation? Try the creatingonline.com site out,
How To Send Text Messages To Cell Numbers
This is a good resource for determining how to send a text message to a cell phone.
JSI Inc.
This is a good site for Windows registry hacks.
Pinoy VII
This site is full of great Paint Shop Pro tutorials. If you use Paint Shop Pro, do youself the favor of checking out these links.
Sysinternals.com
This site has many great free utilities for developers.
Free ContainerGuy DotNetNuke Container
A free DotNetNuke container for ContainerGuy.com registered users.
Free Icons
Literally hundreds of great looking icons for free. Nothing makes a site look better than great looking images.
Free PDF Converter
This totally free application installs as a virtual printer and allows you to create PDF files from virtually any application such as MS Word, MS Excel, etc. There are no restrictions, no watermarks, no spyware, no adware, and no pop-up advertisements. Thank you EXP Systems.
Free DotNetNuke Containers
Free courtesy Nina Meiers. You must login to download them though.
Microsoft Webcasts
There seems to always be a webcast going on here.
 
ASP.NET User Control Positioning With CSS
 
Location: BlogsNetSplore's Blog    
Posted by: Joe Rattz 11/4/2008 11:41:42 AM

- Solving the Problem No One Seems to Care About

I subtitled this post with the tongue-in-cheek phrase "Solving the Problem No One Seems to Care About" because I recently discovered what I would consider to be a flaw in the design of ASP.NET user controls, but I seem to be the only one who cares.

The flaw is that there is no HTML emitted for an ASP.NET user control.  Don't be mislead by my previous statement though.  Certainly the contents of the user control are emitted, but if your user control contains nothing, there will be no HTML emitted by it.  Here is some example ASP.NET markup for a page:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="UserControlA.ascx" tagname="UserControlA" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <uc1:UserControlA ID="UserControlA1" runat="server" />
    </form>
</body>
</html>

Here is the ASP.NET markup for the user control, UserControlA:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControlA.ascx.cs" Inherits="UserControlA" %>

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControlA.ascx.cs" Inherits="UserControlA" %>

Notice that there is no markup for the user control. And here is the emitted HTML for the page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
 Untitled Page
</title></head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="ID__VIEWSTATE" value="/wEPDwUJMjM2Mjk4NzA0ZGT6f3M26FuBr6agyMspUhhwhPM2jw==" />
</div>

       
    </form>
</body>
</html>

Notice that there is no sign of the user control in the HTML.  You can delete the addition of the user control in the page markup and the HTML will be identical.  What is the significance of this and why do I care?  I care because I want to use CSS to position my user control on the page.  Since the point of a user control is to create a reusable component, and I most likely will be reusing the user control on different pages, I most likely will want the user control to be placed in different positions on different pages.  If I want to use CSS to control the positioning, how do I do this?  One thought would be to create a div immediately inside the user control and specify a CSS selector mapped to a style that will position the user control, like this:

<div class="MyUCPositioningClass">
    ...
</div>

The flaw with this approach is that it means the position would be the same on every page I add the user control to, and that is no good.

The next approach developers seem to want to take is wrapping the page markup for the user control in a div, like this:

<form id="form1" runat="server">
    <div class="MyUCPositioningClass">
        <uc1:UserControlA ID="UserControlA1" runat="server" />
    </div>
</form>

This basically works.  It allows you to specify the CSS class outside of the user control contents, which means you can use CSS to control the positioning and it be specific to the page.  However, it creates a coordination effort between the div and the user control if you need to make the user control invisible.  In my case, I had several user controls and depending on the user's action, some controls would be invisible while others were visible, and vice versa.  While I can do this, it is a hassle.

What would be best is if the user control itself emitted HTML of its own...a div representing the user control.  I could then set the user control's class or CssClass to be whatever style I want on each page and therefore each page would have the ability to control the user control's positioning.  But alas, the user control won't do this.

Matthew MacDonald, author of Pro ASP.NET 3.5 in C# 2008, did give me this work-around, which seems very usable.  I thought I would share it with you.  I will start with the same basic code as above, except I will create a div immediately inside of the user control, like this:

<div id="<%# ID %>">
    <h1><%# ID %></h1>
</div>

This allows me to have a div inside the user control, but whose id is set based on the user control's ID.  I have also added a header (h1) containing the user control's ID to make it easy to identify the user control on the page.  For this to work, you need to add a call to DataBind() in your Page_Load() method for the user control like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataBind();
    }
}

The next step is to create a CSS file.  I have created a CSS file named StyleSheet.css and added it to my page's aspx file:

...
<head runat="server">
    <title>Untitled Page</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
...

In my page's markup, I add the user controls.  As an example, I will add two instances to the same page, each with a different ID thereby allowing me to create a different style for each in the CSS file:

<body>
    <form id="form1" runat="server">
        <uc1:UserControlA ID="UserControlA1" runat="server" />
        <uc1:UserControlA ID="UserControlA2" runat="server" />
    </form>
</body>

Notice the ID for the first instance of the user control is UserControlA1 and the second ID is UserControlA2.  Now all I need to do is add separate selectors and styles for each in my CSS, and I have positioning (and any other CSS control I want) controlled at the page (or even instance) level using CSS.  Here is my CSS:

#UserControlA1
{
 background-color: Aqua;
 position: relative;
 left: 50px;
 top: 75px;
 height: 200px;
 width: 350px;
}

#UserControlA2
{
 background-color: Yellow;
 position: relative;
 left: 300px;
 top: 175px;
 height: 150px;
 width: 200px;
}

That's it.  To recap, here are the steps enumerated for your convenience:

  1. Add a div immediately inside the user control and specify its id to be equal to "<%# ID %>".  This will get replaced with the user control's ID when we call DataBind() in the user control's Page_Load() method.
  2. Add a call to DataBind() in your user control's Page_Load method.
  3. Add the markup to your page for each user control giving them a unique ID.
  4. Add a style in the CSS for each user control ID.

Here is a screen shot demonstrating the placement of the two user controls:

UserControlPositioningWithCSS.jpg

And here is the project you can download to play with.

 

Copyright ©2008 Joe Rattz
Permalink |  Trackback

Comments (1)  
Re: ASP.NET User Control Positioning With CSS    By Joe Rattz on 11/14/2008 5:51:16 PM
Correction:

In step #1 in the enumerated list of steps, the value it states to set the div's id to inside the user control is an empty string. This is just the way the HTML I am trying to display gets rendered. The contents of that string should be the way I have it shown farther up in the article, "<%# ID %>".

Unfortunately, trying to correct a post like this requires me to redo all the HTML in the post because the editor doesn't encode/decode this stuff very well, so rather than even try to correct it, I have added this comment.

Thanks.

 
Articles
 
Since posting my article titled "Calling AS/400 (AS400) RPG Programs From ASP.NET", I have gotten a good bit of feedback and questions. This is further evidence of how difficult it is to find good information about interfacing with the AS/400. Due to some of the questions and comments posted about that article, I thought I would do a little more research, and post a follow-up article.   Read More...
Now would probably be a good time to point out that when I first started creating articles for NetSplore, my intent was not only to share information with others, but to share it with myself...in the future! I can't tell you how many times I have had to go scrambling trying to figure out what code did something I needed to do again. So, I thought that if there was something I was having trouble remembering how to accomplish, others may have the same problem, and I should create an article.   Read More...
Have you ever needed to raise a button-click event programmatically? I sure have. Sometimes you end up needing to programmatically simulate that a button was clicked by a user, but how do you accomplish this? ...   Read More...
Have you ever had to wrestle with HTML or CSS rendering issues? Working in a compiled language environment like ASP.NET can be exasperating sometimes. The cycle time of edit, compile, and test can get very fristrating when you are making many small changes trying to determine why something is not rendering the way you would expect.   Read More...
Have you ever wondered how you might use C# multicast delegates in a real world scenario? Have you ever had two controls with interdependence and wanted a technique to decouple them?   Read More...
Have you ever needed to call an RPG program running on an AS/400 from ASP.NET? Here's how I do it.   Read More...
Have you ever wanted to reference an image in a forum post or blog, but had no place to host the image? Here's one.   Read More...
Have you ever needed a 3-state checkbox? Have you ever wanted to provide a gaphic instead of seeing the normal checkbox or radio button? I know I have.   Read More...
Have you ever had a button that caused serious problems if the user clicked it more than once? Here is some code to prevent that.   Read More...
Have you ever wondered what a shadow module is good for? Me too.   Read More...
Have you ever wanted a tooltip that didn't timeout and that you could control the look of the text? This free ASP.NET server control will do it.   Read More...
Have you ever needed a way to highlight ASP.NET DataGrid rows on the client side as the user moused over them? Here's how.   Read More...

 
New Features
 
Check here for the latest NetSplore features.

 
    






Favorite Sites
   
"Dare to be gorgeous and unique. But don't ever be cryptic or otherwise unfathomable. Make it unforgettably great."
    -- RJ Mical - Father of Amiga Computer GUI, Intuition
 
Home|Freebies|Blog|Services|Articles|ASP.NET Depot|DotNetNuke Central|Contact Us
  Copyright 2005 Netsplore Terms Of Use Privacy Statement