当我将 ASP.NET LinkButton 与 OnClientClick 属性一起使用时,我遇到了一些奇怪的行为。
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,我的链接按钮在发生回发时消失(使用 DropDownList)。
In 另一个话题 https://stackoverflow.com/questions/584747/postback-issue-in-asp-net,我找到了将 EnableViewState 设置为 false 的解决方案。
但我正在编写的应用程序是多语言的,因此当 EnableViewState 设置为“false”时,我也会丢失翻译。
if ( !Page.IsPostBack ) {
// translate all form elements
TranslationUI();
}
我宁愿不在 !Page.IsPostBack 方法之外调用此方法,因为 TranslationUI-method() 根据数据库转换表单元素。
我做了一些测试 - 我认为问题是,您需要确保 LinkButton 中的所有嵌套标签都是服务器端控件(即添加runat="server"
或者更改相关的.net控件,例如更改img
tag to asp:Image
)。当 LinkButton 中存在非服务器端标记时,它如何设置其 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?');";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)