asp.net linkbutton onclientclick和postback

时间:2010-02-03 16:13:13

标签: c# asp.net javascript

使用带有OnClientClick属性的ASP.NET LinkBut​​ton时,我遇到了一些奇怪的行为。

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>

代码隐藏

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

没有OnClientClick,一切都很好。 使用OnClientClick时,我的LinkBut​​ton会在发生回发时消失(使用DropDownList)。

another topic中,我找到了将EnableViewState设置为false的解决方案。 但是我正在编写的应用程序是多语言的,所以当EnableViewState设置为“false”时,我也失去了我的翻译。

if ( !Page.IsPostBack ) {
    // translate all form elements
    TranslationUI();
}

我宁愿不在!Page.IsPostBack方法之外调用此方法,因为TranslationUI-method()会根据数据库转换表单元素。

1 个答案:

答案 0 :(得分:4)

我做了一些测试 - 我认为问题是,你需要确保LinkBut​​ton中的所有嵌套标签都是服务器端控件(即添加runat="server"或更改为相关的.net控件,例如更改{{ 1}}标记为img)。如果LinkBut​​ton中没有非服务器端标记,则必须存在设置其ViewState或其他内容的问题......

无论如何,以下工作正常:

asp:Image

代码背后:

<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>
相关问题