Overriding ImageButton Render in a custom control

P

Peter Colson

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?
 
P

Peter Colson

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)".
 
P

Peter Colson

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top