Search code examples
c#asp.netlinkbutton

LinkButton in custom Control not rendering ID (or really working at all)


I am guessing this could be related to this but unfortunately the workaround doesn't seem to work in this case.

After struggling with the larger implementation not working, I boiled it down to the simplest case. This does not work.

   public class MyButton : Control
    {
        public MyButton()
            : base()
        {
            LinkButton but = new LinkButton();
            but.CommandName = "test";
            but.CommandArgument = "test2";
            but.Text = "Click Here";
            Controls.Add(but);
        }
    }

What renders is:

<a href="javascript:__doPostBack('ctl00$ctl11$ctl07','')">Click Here</a>

There are two major problems here. First, there is no ID. It should have tag id='ctl00$ctl11$ctl07'. So even though it will post, the events never get captured.

Second, it's ignoring the CommandName and CommandArgument, it should be rendering __doPostBackWithOptions anyway.

Am I being immensely stupid and just overlooking something obvious or is this a huge bug in ASP.NET?

I've done this lots of times before where there were many other controls rendered inside a Control or WebControl and never had any problems, so it must have something to do with the simplicity rather than the complexity, I guess.

If anyone can help me solve this it would be much appreciated.


Solution

  • That's because you should implement INamingContainer Interface