Mark and Ken have nailed this, but a small explanation might help clear up
any confusion.
There are effectively two different sets of controls in ASP.NET, organized
into distinct namespaces;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
All of the WebControls are represented in the HTML by tags that begin with
"<asp:";
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
By contrast, HtmlControls are represented by standard HTML tags;
<input type="text" id="applicantID" runat="server" NAME="applicantID">
Note the extra stuff; runat="server" and a unique id are required in order
to access this control from your codebehind source; just like a WebControl.
You're using a WebControl, which protects you from many of the proprietary
inconveniences of raw HTML. Control properties like colors and border
styles are examples of this. Rather than using string constructions to set
these values, you use other .NET classes to control these settings.
Colors are represented through System.Drawing.Color, which has many
predefined colors for convenience. To create special colors, either use
System.Drawing.Color.FromArgb (int red, int green, int blue), or use
ColorTranslater.FromHtml ("#ffeedd") to convert Html hex strings.
Border styles are represented through an enum; in your case you'd want;
applicantID.BorderStyle = BorderStyle.None;
Learning the ASP.NET control object model will save you a lot in the long
run. However if you want to take a shortcut, you can use HtmlControls
instead. Create a standard HTML tag like;
<input type="text" id="applicantID" runat="server"></input>
And in your codebehind class;
protected System.Web.UI.HtmlControls.HtmlInputText applicantID;
This lets you access the standard HTML tag properties and set them all as
strings, just like you're used to. Since HTML text controls don't have
color or border properties (i.e. you have to set the style or class), you
would use;
applicantID.Style.Add ( "cssstyle_you_want", "value_you_want" );
This makes for less readable code, and lower portability between WebForms
and WinForms. The code is also much more prone to typograpic errors. For
practical purposes, you should consider this approach as a hack.
Hope this explains a bit of the "why".
/// M
Ken Cox said:
Here's a way to handle the colour issue:
applicantID.BackColor = ColorTranslator.FromHtml("#66ccff")
However, since you are using a style try:
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
applicantID.Attributes.Add("style", _
"border-style:none;background-color:#F6F6F6;")
End Sub
You could also create a CSS class that uses those properties and assign the
class name as the value of CssClass.
Does this help?
Ken
Microsoft MVP [ASP.NET]
tshad said:
That doesn't work either. I also can't use this:
applicantID.BorderStyle = "none"
I get the message:
Exception Details: System.FormatException: Input string was not in a
correct format.
I can do this and it works fine:
<asp:textbox id="applicantID" Style=" border-style:none;
background-color:#F6F6F6;" TextMode="SingleLine" Columns="32"
runat="server"
The problem is I need to do this at runtime. I have a page that is going
to be for input in one case and for display for another. When it is
display I just want to be able to get rid of the borders on the textbox
and set the background to be the same as the table (which the above works
if I set it directly - the problem is that Style is not able to be set at
runtime).
This is really driving me crazy.
Thanks,
Tom.