How does CommandName property render at run-time RRS feed

  • Question

  • User-1691609705 posted

    Hi everyone!

    I am posting here as it has relevance to a control I am developing.  I have a class that I want to inherit from LinkButton, but I also want to give a developer the option to "convert" this to a Hyperlink by providing a NavigateURL property, which will render in the href attribute at runtime. This works, but the problem with this approach is it create 2 href tags - the first one with the URL and the second with the javascript doPostBack function.

    After much debate, I decided to rather override the render method and write out the html (writer.write([link markup])). this way I am able to control the value of href, either with a URL or with doPostBack(...), as well as some other styling that I couldn't override any other way (rambling...when u set a height or width property of a control, the rendered output reads something like : style="height:20px; display:inline-block" which is undesirable for my purposes)

    Anyway, I have subsequently decided to inherit from WebControl, and cater for the additional properties of Linkbutton/Hyperlink (yes, i know..the looong route!)

    the only 3 properties I cant seem to wrap my head around and cater for are CommandArgument, CausesValidation and CommandName.  I cant see any different output to the HTML in Page-source view, so how do these render? Does an additional script run?

    Hope I am making sense with all of this! [:$]


    Wednesday, June 11, 2008 3:52 PM


  • User-667042492 posted

    hi shalan99,

    there is not an additional script. But Hyperlink control has some attributes on render. one of them is "href" ..

    but if you want to your custom Hyperlink, you can use WebControl object to serve your custom web control.

    and your render class may be like below,

    1    protected override void Render(HtmlTextWriter writer)
    2            {
    3                // Make sure this control is in a server form.
    4                if (Page != null)
    5                {
    6                    Page.VerifyRenderingInServerForm(this);
    7                }
    8                base.Render(writer);
    9            }

    and you can use; protected override void AddAttributesToRender(HtmlTextWriter writer)

    method for customize your custom web control. In this method you can add some attributes to your control. For example;

    writer.AddAttribute(HtmlTextWriterAttribute.Id, "someId");

    i hope this help you..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 14, 2008 10:59 AM