asp.net linkbutton onclientclick and postback

asp.net, c#, javascript

Solution

I did some testing - I think the problem is, you need to ensure all nested tags within the LinkButton are serverside controls (I.e. either add `runat="server"` or change to related .net control, such as change the `img` tag to `asp:Image`). When there is non server-side markup within the LinkButton, there must be an issue with how it sets up its ViewState or something...

Anyway, the following works fine:

<asp:DropDownList ID="test" runat="server" AutoPostBack="true">
    <asp:ListItem>test1</asp:ListItem>
    <asp:ListItem>test2</asp:ListItem>
    <asp:ListItem>test3</asp:ListItem>
</asp:DropDownList>

<asp:LinkButton CssClass="button" ID="btnDeleteGroup" runat="server">
    <asp:Image runat="server" ID="imgDeleteGroup" width="16" height="16" ImageUrl="cross.png" />
    <asp:Literal ID="lblDeleteGroup" runat="server" Text="Delete" />
</asp:LinkButton>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    btnDeleteGroup.OnClientClick = "return confirm('delete?');";
}

Problem

I'm experiencing some weird behaviour when using a ASP.NET LinkButton with a OnClientClick-property. ASPX ``` <asp:DropDownList ID="test" runat="server" AutoPostBack="true"> <asp:ListItem>test1</asp:ListItem> <asp:ListItem>test2</asp:ListItem> <asp:ListItem>test3</asp:ListItem> </asp:DropDownList> <asp:LinkButton CssClass="button" ID="btnDeleteGroup" runat="server"> <img src="cross.png" alt="delete-group" width="16" height="16" /> <span><asp:Literal ID="lblDeleteGroup" runat="server" Text="Delete" /></span> </asp:LinkButton> ``` Code-behind ``` protected void Page_Load(object sender, EventArgs e) { btnDeleteGroup.OnClientClick = "return confirmAction('delete?');"; } ``` Without the OnClientClick, everything is fine. With the OnClientClick, my LinkButton dissappears when a postback occurs (using the DropDownList). In another topic, I've found a solution to set EnableViewState to false. But the application I'm writing is multilingual so with EnableViewState set to "false", I'm also losing my translation. ``` if ( !Page.IsPostBack ) { // translate all form elements TranslationUI(); } ``` I rather not call this method outside the !Page.IsPostBack method because the TranslationUI-method() translates the form elements based on a database.

Original source