开发者

LinkButton's OnClick does not fire in IE8

The following works just fine in Chrome.

<asp:LinkButton runat="server" ID="lbEdit" OnClick="lbEd开发者_运维问答it_Click">
    <button type="button" class="edit">
         Edit
    </button>
</asp:LinkButton>

And here is the CSS for button and its subclass.

button
{    
    padding: 3px;
}
button.edit 
{
    background:#3f6096;
    border:none;
    font:10px verdana;
    color:#fff;
    width:71px;
    margin-top:3px;
}

As you can see, nothing special; just colors and beautiful things.

I click on the blue Edit button and it fires the OnClick postback just fine.... in Chrome!

But if I do the same in IE8, it just does nothing; doesn't even detect a click.

To help pinpoint the problem, I removed the tag and kept just the word "Edit", and it works just fine in IE8 as a simple underlined link; the postback fires.

So, Why can't IE8 accept anything within LinkButton?


Why use a LinkButton if you need to handle a postback and look like a button at the same time? Why not make it just a Button? It seems to me that the only reason to use one over the other is appearance. Functionally, they're both supposed to trigger a postback.


What you are doing isn't something you are supposed to do, but I would say that IE is probably the one behaving closest to 'correctly' here.

You should not nest button elements inside other elements that provide links. You can simply remove the button from inside the LinkButton, attach the OnClick to it instead, make the styling work, and it'll be good.

The reason I say I think IE is behaving closer to 'correctly' is because it's doing what you have defined on the innermost tag; which is nothing. (you have not attached any actions to the button element, in actuality). But really, it's not something you should do in HTML anyway, as I don't think the behavior is defined in the first place.


What are you trying to achieve here?

You have a server-side link button, with a nested HTML button?

A LinkButton is useful when you want a hyperlink to be able to perform a postback.

Did you have a look at the rendered HTML? I don't know if having a button inside an anchor tag is even valid HTML.

If you want the postback to be handled by a particular server-side event, just use an ASP:Button.

If you dont, just use a regular HTML button.


You can use CssClass:

<asp:TemplateField HeaderText="Opciones">
    <ItemTemplate>
    <asp:LinkButton runat="server" Text="Hola!" CommandName="Deshabilitar" 
        CssClass="btn btn-primary"
        CommandArgument='<%# Item.Id %>' />
    </ItemTemplate>
</asp:TemplateField>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜