1

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

2 Answers 2

1

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!
}
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried something like:

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

1 Comment

Yes that does then render an ID... but I am still wondering why an ID is not getting generated automatically. Ultimately I am trying to figure out why my event handler is getting called for this exact button, but first I want to see why the IDs aren't getting rendered automatically in case it's related.

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.