Ryan
Ryan

Reputation: 3492

ASP.NET Button not rendering ID - Sharepoint Web Part

I have a sharepoint web part where I am programatically adding a button to the page. When the button renders, it has no name or ID. I am doing the following in the code behind of the web part:

public class ChartControl : WebPart
{
    protected Button BubbleChartApplyButton;

    protected override void CreateChildControls()
    {
        BubbleChartApplyButton = new Button {Text = "Apply This"};
    }

    protected override void RenderWebPart(HtmlTextWriter output)
    {
        BubbleChartApplyButton.RenderControl(output);
    }
}

and this is what gets rendered:

    <div WebPartID="4844c2e5-2dd5-437b-a879-90c2e1e21f69" HasPers="false" id="WebPartWPQ2" width="100%" class="ms-WPBody ms-wpContentDivSpace" allowDelete="false" style="" >
         <input type="submit" value="Apply This" />

...more output here...
</div>

Notice how the input tag gets rendered with no name and no id... I would expect to see something like ID="ctl00$m$g_2d506746_d91e_4508_8cc4_649e463e1537$ctl13" or something.

Any ideas as to why the button control is not rendering with the name and IDs that .NET normally generates?

Thanks,

Ryan

Upvotes: 1

Views: 381

Answers (2)

Ryan
Ryan

Reputation: 3492

Ugh, it was a simple thing I overlooked. So I am creating the control and rendering it to the output so it shows up when the page is rendered. BUT, in order for it to be dialed into the control tree so that ASP.NET assigns IDs and I can wire it up to an event handler, I MUST specifically add it to the control tree.

So I also needed to do this:

protected override void CreateChildControls()
{
    BubbleChartApplyButton = new Button {Text = "Apply This"};

    Controls.Add(BubbleChartApplyButton) //<== ADD TO THE CONTROL TREE BEAVIS!
}

Upvotes: 1

Jupaol
Jupaol

Reputation: 21365

Have you tried something like:

BubbleChartApplyButton = new Button {Text = "Apply This", ID = "myButtonID"};

Upvotes: 0

Related Questions