Wednesday, March 28, 2012

Web Control location and help?

Hi there,
Can anybody help me?
I am little confused with the correct way of creating a web control. I have done one but should I put it in it's own folder?
At the moment it is residing in the root of my web directory with all the other .aspx forms and shares its CSS style sheet with all my other web pages.
I was thinking that the web control is a control within itself so it shouldn't have to rely on other items from the main web.
I am mainly using the CSS for position items within the control. What is the best way to change the colours on the fly of various aspects of the control use Properties or embed its own CSS or similar?
Maybe i am getting a little lost here, is there such a thing as a compiled web control? Well the .ascx and CSS don't need to be distirubuted?
If anybody can suggest "Best Practices" it would be really helpful
Thanks in advance
IanYou mention .ascx so I am assuming you are creating Web UserControls and not custom Server Controls. For UserControls, it's ok to have them reside in a UserControls folder under the root of your Web App. Since UserControls are generally specific for that app, there's no need to separate it.
However, for custom Server Controls, it's probably better to create a new Project and put your Server Controls there. Your Web App can then reference the dll of that Project. As far as the CSS styles are concerned, your Server Controls can expose a bunch of properties allowing the user to set the CSS classname to whatever they want. Then in your server control, you just set the classname attributes of whatever childcontrols inside it to those properties.
Hi there,
thansk for the reply.. Yes it is user controls i am creating.
One question if my user control has 2 textboxes, and 2 images on there then how do i NAME the class/Id that will be output to HTML so that my main page can specify CSS to control this?
One thing that gets me, and confuses me a bit is why offer 2 ways of changing things i.e.
CSS can control my control for Colours etc
so can a Property of my control.
What is the standard way of doing this?
I presume it would be CSS, no? as with CSS if i wish to change certain colours on my control on certain pages i just edit the CSS
otherwsie I would have to edit each property individually?
Thanks again
ian

Each server control should have a CssClass Property that you set to the name of the class in your css file. So if you have two textboxes and 2 images, you can just expose the CssClass Properties

PublicProperty CssClassTextBox1()AsString

Get

Return TextBox1.CssClass

EndGet

Set(ByVal valueAsString)

TextBox1.CssClass = value

EndSet

EndProperty

PublicProperty CssClassImage1()AsString

Get

Return Image1.CssClass

EndGet

Set(ByVal valueAsString)

Image1.CssClass = value

EndSet

EndProperty

ASP.NET made all those appearance properties such as Font, ForeColor,etc so it makes it easier for people that don't want to use CSS to style their controls. It also provide Design Tme support so you can see your styles right away. There's really no difference since those properties are just rendered as their html style equivalent on the client side. ASP.NET 2.0 also allows Themes and Skins so you can predefine a "skin" for your textbox in a .skin file. Then you can set the SkinID on your textbox, and it will have the same style as you defined in your .skin file.

0 comments:

Post a Comment