4

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.

1
  • Wow that is very odd.. I can reproduce this as well.. In the resulting markup after the page posts back, the corresponding <a> tag for the link button does not contain any nested tags any longer. This is why it looks like it disappears.. Commented Feb 3, 2010 at 16:27

1 Answer 1

4

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?');";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.