test
test

Reputation: 23

Adding html to CreateChildControls() method

I'm using MOSS 2007 in my webpart within my CreateChildControls() method I have the code below:

     protected override void CreateChildControls()
        {
        base.CreateChildControls();

        Panel myPanel = new Panel();
        myPanel.ID = "SelectionPanel";
        this.Controls.Add(myPanel);

        this.myGridView = new GridView();
        createMyGridView(ref myGridView);
        myPanel.Controls.Add(myGridView);

        this._btnUpdate = new Button();
        this._btnUpdate.Text = "Update";
        myPanel.Controls.Add(_btnUpdate);
        this._btnUpdate.Click += new EventHandler(_btnUpdate_Click);
}

My question is how do insert html so I can wrap a div around these controls, without using the RenderWebPart() method.

I am trying to acheive this because i dont want to use a panel whose id will be an autogenerated client id.

Many Thanks,

Upvotes: 2

Views: 5676

Answers (3)

user1217169
user1217169

Reputation: 439

I'm working with something similar, and I wanted to put the new SP 2013 " + add new announcement" in my custom webpart.

I tried this, and it worked, but I dont know if it's a good way to do it. Basically you can put whatever html in a litteralcontrol I found out...

string contents = "<div class=\"ms-comm-heroLinkContainer wp-custom-addnew\"><a id=\"forum0-NewPostLink\" class=\"ms-textXLarge ms-heroCommandLink\" title=\"add new announcement\" href=\"" + list.DefaultNewFormUrl + "\"><span class=\"ms-list-addnew-imgSpan20\"><img class=\"ms-list-addnew-img20\" src=\"/_layouts/15/images/company/SPSPrite.png\"></span><span>New Announcement</span></a><div class=\"ms-clear\"></div></div>";

LiteralControl link = new LiteralControl(contents);

this.Controls.Add(link);

Please tell me if there is a better way of posting "raw custom" html.

Upvotes: 1

kristian
kristian

Reputation: 23039

I'm not sure if there's some reason this wouldn't work for a webpart, but this seems to do the trick for a web user control:

protected override void CreateChildControls()
{
base.CreateChildControls();

var container = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
container.Attributes.Add("id","SelectionPanel");

TextBox tb = new TextBox();
container.Controls.Add(tb);

this.Controls.Add(container);

}

Upvotes: 2

Rubens Farias
Rubens Farias

Reputation: 57996

Panel render as <div>. Try this:

protected override void CreateChildControls()
{
    base.CreateChildControls();

    Panel around = new Panel();

    Panel myPanel = new Panel();
    myPanel.ID = "SelectionPanel";
    around.Controls.Add(myPanel);

    // ... other controls ... \\

    this.Controls.Add(around);
}

Upvotes: 1

Related Questions