开发者

Click event isn't fired when using custom button control

开发者 https://www.devze.com 2023-03-31 10:32 出处:网络
I have made custom button control, but I\'m missing something because I can\'t get it work properly. Button works if render method is like this:

I have made custom button control, but I'm missing something because I can't get it work properly.

Button works if render method is like this:

protected override void Render(HtmlTextWriter writer)
{
  base.Render(writer);
}

But when I change code to this:

protected override void Render(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Class, css);
        writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ID);
        writer.RenderBeginTag(HtmlTextWriterTag.Button);
        writer.RenderBeginTag(Html开发者_开发技巧TextWriterTag.Span);
        writer.RenderBeginTag(HtmlTextWriterTag.Span);
        writer.RenderBeginTag(HtmlTextWriterTag.Span);
        writer.Write(base.Text);
        writer.RenderEndTag();
        writer.RenderEndTag();
        writer.RenderEndTag();
        writer.RenderEndTag();
    }

It just loads same page where button was clicked and nothing else happens.

So what I'm not doing what base.Render(write) does? And is there anything else in these custom control buttons what should be done or can I do this better?

Buttons output should be something like:

<button>
  <span>
    <span>
      <span>
        Title
      </span>
    </span>
  </span>
</button>


You should invoke the base method...

base.Render(writer);

Like below ↓

  protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, css);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ID);
            writer.RenderBeginTag(HtmlTextWriterTag.Button);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(base.Text);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();

       base.Render(writer);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消