1

I have created a custom page layout with a div tag in it. I am trying find this div from a visual webpart code behind,but the control is always null.

Page Layout

<div id="abc" runat="server">....</div>

Visual WebPart code

Control ct = findDivControl(Page,"ctl00_PlaceHolderMain_abc");

Recursive method

protected HtmlGenericControl findDivControl(Control c, string id)
        {
            HtmlGenericControl foundControl = (HtmlGenericControl) c.FindControl(id);
            if (foundControl != null)
                return foundControl;

            foreach (Control ctx in c.Controls)
            {
                foundControl = (HtmlGenericControl) findDivControl(ctx, id);
                if (foundControl != null)
                    return foundControl;

            }

        return null;
    }
3

Why are you using the client side ID. Use the server side ID "abc". It will work.

Regards.

  • Yep, using server side ID resolved the issue. Silly mistake. Thanks for the help. – Thomas Mathew May 24 '15 at 10:27
0

Here is a workaround you can use.

In the visual webpart use a hidden input control to save the value you want to display in the div.

After populating, use JavaScript to read value from the input hidden control and populate the div element.

Here we are using a hidden input to communicate between the div control and server side code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.