【问题标题】:asp.net linkbutton onclientclick and postbackasp.net 链接按钮 onclientclick 和回发
【发布时间】:2010-02-03 16:13:13
【问题描述】:

在使用带有 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() 基于数据库转换表单元素。

【问题讨论】:

  • 哇,这很奇怪..我也可以重现这个..在页面回发后的结果标记中,链接按钮的相应&lt;a&gt;标签不包含任何嵌套标签更长。这就是它看起来消失的原因..

标签: c# asp.net javascript


【解决方案1】:

我做了一些测试 - 我认为问题是,您需要确保 LinkBut​​ton 中的所有嵌套标签都是服务器端控件(即添加 runat="server" 或更改为相关的 .net 控件,例如更改 img 标签到asp:Image)。当 LinkBut​​ton 中有非服务器端标记时,它如何设置其 ViewState 或其他东西肯定存在问题......

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

<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>

后面的代码:

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

【讨论】:

    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2018-09-30
    • 2015-07-08
    相关资源
    最近更新 更多