最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - asp.net linkbutton onclientclick and postback - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited May 23, 2017 at 12:11 CommunityBot 11 silver badge asked Feb 3, 2010 at 16:13 thomasvdbthomasvdb 7491 gold badge12 silver badges32 bronze badges 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.. – KP. Commented Feb 3, 2010 at 16:27
Add a ment  | 

1 Answer 1

Reset to default 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 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?');";
}
发布评论

评论列表(0)

  1. 暂无评论