ASP.NET HtmlTextArea attributes and newline
I'd like to change some of the attributes of an HtmlTextArea in a SharePoint web part that I'm developing in C#. The HtmlTextArea is being used as a customized display for some Sql Server 2005 data that I'm pulling in and I'd like to change the font, color, etc., and make it read-only. I see that there are a few methods, such as HtmlTextArea.Attributes.Add, HtmlTextArea.Attributes.AddAttributes, and HtmlTextArea.Attributes.CssStyle, but I'm not sure if these are the proper ones to use, nor how to use them. I know that with an ASP.NET TextArea control I can simply use inline CSS, so I'm trying to figure out a way to set that inline CSS from within C#.
Also, I'd like to find out a way to add a newline in between controls, just to aid in placement. I have laid out all of my controls in CreateChildControls, but I don't see how I can get control over their placement. For instance, I have something like:
protected override void CreateChildControls() { customers = new DropDownList(); customers.ID = "customers"; 开发者_JAVA百科 Controls.Add(customers); machines = new DropDownList(); machines.ID = "machines"; Controls.Add(machines); specsOutput = new HtmlTextArea(); specsOutput.ID = "specsOutput"; Controls.Add(specsOutput); }
I would like for the HtmlTextArea to be displayed below the ddls. Thanks for everyone's help.
To add inline css, use Attributes.Add("style", "color: white; background-color: black");
etc. etc.
You can add LiteralControls
to aid in layout of your custom control.
customers = new DropDownList();
customers.ID = "customers";
Controls.Add(customers);
Controls.Add(new LiteralControl("<br />"));
To manage how the control renders, you can override the Render
event, like so:
protected override void Render(HtmlTextWriter writer)
{
customers.RenderControl(writer);
writer.Write("<br />");
machines.RenderControl(writer);
writer.Write("<br />");
specsOutput.RenderControl(writer);
}
As womp mentions, it's possible to add in-line styles by using the Attributes.Add
method on the controls.
If you want to move away from this rather awkward way of building your web part UI you can load an ASCX and your web part designing experience will be like any user control.. much better.
example
精彩评论