views:

158

answers:

3

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";
        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.

+2  A: 

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 />"));
womp
Perfect! Thanks very much.
Geo Ego
+1  A: 

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.

CAbbott
I had actually initially rendered the control this way; I just found that with a simpler layout like I have (two ddls and an HtmlTextArea) and fairly simple rendering, I liked the straight-ahead method of overriding CreateChildControls and letting it all render right in a line.
Geo Ego
That's cool. Just keep in mind that when you're doing it that way you're creating a lot of objects just to render and that they need to be created in the order that you want them rendered.
CAbbott
A: 

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

ArjanP
Thanks. I've looked at this way at doing of things, and it's probably the route I'll take in the future, but this web part is almost wrapped up so I'm going to finish it using this methodology.
Geo Ego