Asp.net linkbutton onclientclick and postback

I am experiencing some strange behavior when using ASP.NET LinkButton with the 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 for

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

Without OnClientClick, everything is fine. With OnClientClick, my LinkButton disappears when postback occurs (using DropDownList).

In another topic , I found a solution to set EnableViewState to false. But the application I'm writing is multilingual, so with EnableViewState set to false, I also lose the translation.

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

I prefer not to call this method outside the method! Page.IsPostBack because TranslationUI-method () converts form elements based on the database.

+4
source share
1 answer

I did some testing - I think the problem is that all the nested tags in LinkButton are server controls (ie add runat="server" or change to the corresponding .net control, for example, change the img tag to asp:Image ). If there is no server-side markup in LinkButton, there should be a problem with how it configures ViewState or something like that.

In any case, the following works just 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?');"; } 
+4
source

Source: https://habr.com/ru/post/1300189/


All Articles