Overriding ImageButton Render in a custom control

Discussion in 'ASP .Net Web Controls' started by Peter Colson, Nov 18, 2003.

  1. Peter Colson

    Peter Colson Guest

    I've successfully made a custom control based on an image
    button and prevented it from doing postbacks by
    overriding Render and providing a doClick script function
    (built in OnPreRender) that returns false, as follows:

    protected override void Render(HtmlTextWriter writer)
    {
    writer.WriteBeginTag("input");
    writer.WriteAttribute("type", "image");
    writer.WriteAttribute("name", UniqueID);

    if (ID != null)
    writer.WriteAttribute("id", ClientID);

    writer.WriteAttribute("src", ImageUrl);

    // Override click event and prevent postback.
    writer.WriteAttribute("OnClick", "javascript:return
    doClick()");

    writer.Write(HtmlTextWriter.TagRightChar);

    } // Render

    Problem is: this has completely taken over the rendering
    of the ImageButton rather than allowing the base class to
    render itself.

    I thought something like:

    base.Render(writer);
    Attributes.Add("OnClick", "javascript...etc");

    may have worked, but to no avail.

    Any thoughts?
     
    Peter Colson, Nov 18, 2003
    #1
    1. Advertising

  2. Peter Colson

    Guest Guest

    Try:
    Attributes.Add("OnClick", "return false;");

    you need to return false.

    >-----Original Message-----
    >I've successfully made a custom control based on an image
    >button and prevented it from doing postbacks by
    >overriding Render and providing a doClick script function
    >(built in OnPreRender) that returns false, as follows:
    >
    >protected override void Render(HtmlTextWriter writer)
    >{
    > writer.WriteBeginTag("input");
    > writer.WriteAttribute("type", "image");
    > writer.WriteAttribute("name", UniqueID);
    >
    > if (ID != null)
    > writer.WriteAttribute("id", ClientID);
    >
    > writer.WriteAttribute("src", ImageUrl);
    >
    > // Override click event and prevent postback.
    > writer.WriteAttribute("OnClick", "javascript:return
    >doClick()");
    >
    > writer.Write(HtmlTextWriter.TagRightChar);
    >
    >} // Render
    >
    >Problem is: this has completely taken over the rendering
    >of the ImageButton rather than allowing the base class to
    >render itself.
    >
    >I thought something like:
    >
    > base.Render(writer);
    > Attributes.Add("OnClick", "javascript...etc");
    >
    >may have worked, but to no avail.
    >
    >Any thoughts?
    >
    >
    >.
    >
     
    Guest, Nov 18, 2003
    #2
    1. Advertising

  3. Peter Colson

    Peter Colson Guest

    I've tried that as a test and it doesn't fix the problem.

    The "javascript:return doClick()" actually works (i.e.,
    stops the postback) when I do:

    writer.WriteAttribute("OnClick", "javascript:return
    doClick()");

    in the first variation. The problem is that by doing it
    that way I am taking over the rendering of the whole
    control from the base class.

    I've already seen how this affects rendering since
    referencing a CssClass has no effect with my simplified
    control rendering, but works using "base.Render(writer)".

    >-----Original Message-----
    >Try:
    > Attributes.Add("OnClick", "return false;");
    >
    >you need to return false.
    >
    >>-----Original Message-----
    >>I've successfully made a custom control based on an

    image
    >>button and prevented it from doing postbacks by
    >>overriding Render and providing a doClick script

    function
    >>(built in OnPreRender) that returns false, as follows:
    >>
    >>protected override void Render(HtmlTextWriter writer)
    >>{
    >> writer.WriteBeginTag("input");
    >> writer.WriteAttribute("type", "image");
    >> writer.WriteAttribute("name", UniqueID);
    >>
    >> if (ID != null)
    >> writer.WriteAttribute("id", ClientID);
    >>
    >> writer.WriteAttribute("src", ImageUrl);
    >>
    >> // Override click event and prevent postback.
    >> writer.WriteAttribute("OnClick", "javascript:return
    >>doClick()");
    >>
    >> writer.Write(HtmlTextWriter.TagRightChar);
    >>
    >>} // Render
    >>
    >>Problem is: this has completely taken over the

    rendering
    >>of the ImageButton rather than allowing the base class

    to
    >>render itself.
    >>
    >>I thought something like:
    >>
    >> base.Render(writer);
    >> Attributes.Add("OnClick", "javascript...etc");
    >>
    >>may have worked, but to no avail.
    >>
    >>Any thoughts?
    >>
    >>
    >>.
    >>

    >.
    >
     
    Peter Colson, Nov 18, 2003
    #3
  4. Peter Colson

    Peter Colson Guest

    However, doing:

    Attributes.Add("OnClick", "return doClick()");

    before

    base.Render(writer);

    fixes the problem!

    It was worthwhile going back and having a look at
    Attributes.Add after all.

    >-----Original Message-----
    >I've tried that as a test and it doesn't fix the problem.
    >
    >The "javascript:return doClick()" actually works (i.e.,
    >stops the postback) when I do:
    >
    > writer.WriteAttribute("OnClick", "javascript:return
    >doClick()");
    >
    >in the first variation. The problem is that by doing it
    >that way I am taking over the rendering of the whole
    >control from the base class.
    >
    >I've already seen how this affects rendering since
    >referencing a CssClass has no effect with my simplified
    >control rendering, but works using "base.Render(writer)".
    >
    >>-----Original Message-----
    >>Try:
    >> Attributes.Add("OnClick", "return false;");
    >>
    >>you need to return false.
    >>
    >>>-----Original Message-----
    >>>I've successfully made a custom control based on an

    >image
    >>>button and prevented it from doing postbacks by
    >>>overriding Render and providing a doClick script

    >function
    >>>(built in OnPreRender) that returns false, as follows:
    >>>
    >>>protected override void Render(HtmlTextWriter writer)
    >>>{
    >>> writer.WriteBeginTag("input");
    >>> writer.WriteAttribute("type", "image");
    >>> writer.WriteAttribute("name", UniqueID);
    >>>
    >>> if (ID != null)
    >>> writer.WriteAttribute("id", ClientID);
    >>>
    >>> writer.WriteAttribute("src", ImageUrl);
    >>>
    >>> // Override click event and prevent postback.
    >>> writer.WriteAttribute

    ("OnClick", "javascript:return
    >>>doClick()");
    >>>
    >>> writer.Write(HtmlTextWriter.TagRightChar);
    >>>
    >>>} // Render
    >>>
    >>>Problem is: this has completely taken over the

    >rendering
    >>>of the ImageButton rather than allowing the base class

    >to
    >>>render itself.
    >>>
    >>>I thought something like:
    >>>
    >>> base.Render(writer);
    >>> Attributes.Add("OnClick", "javascript...etc");
    >>>
    >>>may have worked, but to no avail.
    >>>
    >>>Any thoughts?
    >>>
    >>>
    >>>.
    >>>

    >>.
    >>

    >.
    >
     
    Peter Colson, Nov 18, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    749
  2. Yama

    Overriding Render Event

    Yama, Oct 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    472
  3. MichiMichi
    Replies:
    0
    Views:
    462
    MichiMichi
    May 29, 2007
  4. Replies:
    1
    Views:
    408
    Teemu Keiski
    Jul 2, 2003
  5. Stephen Miller
    Replies:
    12
    Views:
    837
    Stephen Miller
    Jan 20, 2004
Loading...

Share This Page