1
public class CustCtl : WebControl
{
    protected override System.Web.UI.HtmlTextWriterTag TagKey
    {
        get
        {
            return HtmlTextWriterTag.Div;
        }
    }       
}

With this bare bones control, it would render the root element as a Div tag. But how can I add attributes to that root HTML element that this control will render ... such as a style or id.

Thanks! =D

6 Answers 6

4

You would be able to do something like this within the OnPreRender event

public class CustCtl : WebControl
{
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        WebControl parent = Parent as WebControl;
        if (parent != null)
        {
            parent.Attributes.Add("key", "value");
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank for help. Seems a bit easier to do it my way because the intent and meaning is obviously apparent and there is much less work to be done.
I thought you were trying to add an attribute to the controls parent? I'm going mad because everyone else answered differently, too
Aah I see. Well interesting how the term "Parent Element" was interpretted as "Parent Control". But apparently I am in the minority on that one. I think HTML when I hear the word element. I rephrased the question to include the word HTML for clarity =D
0

If you have your HtmlTextWriter, you can first add some attributes, before rendering a tag. For example:

public void writeDivWithStyle(HtmlTextWriter writer, string style)
{
    writer.AddAttribute(HtmlTextWriterAttribute.Style, style);
    writer.RenderBeginTag(HtmlTextWriterTag.Div);

    // more code here

    writer.RenderEndTag(); // close the DIV
}

1 Comment

"But how can I add attributes to that parent element such as a style or id." am I the only one reading this part?
0

I found this method while looking at the API.

This worked just fine for me and seemed the most appropriate place to put it. Just simply override.

public override void RenderBeginTag(HtmlTextWriter writer)
{
    writer.AddAttribute(HtmlTextWriterAttribute.Class, "[^_^]");
    base.RenderBeginTag(writer);
}

2 Comments

This method is overriden within the Custom Control. Therefore, when it's RenderBeginTag() is called, I insert attributes as I see fit. What is unclear about the question or my answer?
sorry, I deleted my comment once I saw that you were the asker. See the comment on my answer...
0

You should be able to use:

CONTROL.Attribute.Add("..") //not the correct syntax

see this link

EDIT: sample usage

Control control = this.FindControl("body");
HtmlControl divControl = new HtmlGenericControl("div");
divControl.Attributes.Add("id","myid");
divControl.Attributes.Add("class","myclass");
control.Controls.Add(divControl);

3 Comments

Do you want to make me think that passing a single string argument to the Add method, which requires 2 string arguments by the way, points to the parent control like a UNIX path?
Wasn't me, but given what the asker is commenting this is totally valid, aside from the missing params. I read the question differently
@djechelon: lol, no don't think that..... I edited my answer to show the proper syntax in the edit.
0

[Edit, after all comments] Simply, Attributes.Add("key", "value"); in OnPreRender method works

3 Comments

The phrasing may be somewhat ambiguous but I disagree as to why. "With this bare bones control, it would render the parent element as a Div tag." The key term is Element in this sentence. I am asking in reference to the parent html element of the control. I did not specify a parent Control in the sentence.
Sorry for misunderstanding, then option 2 goes for you! ;) I would have said "root element" because just when I read "parent" I think about parent control
haha understandable. I updated the question heading and body to reflect the more clarified version.
0

I think that the most appropriate method or event to override is :

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
    writer.AddAttribute("Key", "Value");
    base.AddAttributesToRender(writer);
}

Check the msdn.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.