asp.net的LinkButton的OnClientClick和回传 [英] asp.net linkbutton onclientclick and postback
问题描述
我使用的是ASP.NET的LinkButton用的OnClientClick属性时遇到一些奇怪的行为。
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-背后
protected void Page_Load(object sender, EventArgs e)
{
btnDeleteGroup.OnClientClick = "return confirmAction('delete?');";
}
如果没有的OnClientClick,一切都很好。
随着的OnClientClick,当(使用的DropDownList)发生回传我的LinkButton dissappears
Without the OnClientClick, everything is fine. With the OnClientClick, my LinkButton dissappears when a postback occurs (using the DropDownList).
在另一个话题,我已经找到了解决的EnableViewState设置为false 。
但我写的应用程序是多语种所以用的EnableViewState设置为假,我也失去了我的翻译。
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();
}
我宁愿不调用这个方法之外!Page.IsPostBack方法,因为基于数据库的TranslationUI法()转换的表单元素。
I rather not call this method outside the !Page.IsPostBack method because the TranslationUI-method() translates the form elements based on a database.
推荐答案
我做了一些测试 - 我认为这个问题是,你需要确保的LinkButton内的所有嵌套的标签是服务器端控件(即无论是添加 =服务器
或 IMG
标签更改相关.NET控件,如切换到 ASP:图像
)。当在LinkButton的内非服务器端的标记,必须有与它如何设置其的ViewState或东西的问题...
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:
protected void Page_Load(object sender, EventArgs e)
{
btnDeleteGroup.OnClientClick = "return confirm('delete?');";
}
这篇关于asp.net的LinkButton的OnClientClick和回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!