Tuesday 15 July 2014

c# - .toggle() a div with asp:button -


I have a basic div that I want to do. Toggle () OnClientClick's asp: button works as a code, but I think the onClick event handler on the server side is due to reset the device. In fact, for the .01ms, the Divis disappears and then again it looks again.

Default.aspx:

  & lt;% @  

Page language = "C #" AutoEventWireUp = "True" codeless = "Default. Aspx.cs "Inheritance =" WebApplication 2. Default "%" & Lt ;! DOCTYPE html & gt; & Lt; Html xmlns = "http://www.w3.org/1999/xhtml" & gt; & Lt; Head runat = "server" & gt; & Lt; Title & gt; Trial & lt; / Title & gt; & Lt; Script src = "// ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> & Lt; Script type = "text / javascript" & gt; Toggle (function) Toggle () {$ ('# Toggle Me'). Toggle (); } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form id = "form1" runat = "server" & gt; & Lt; Div id = "toggleMe" & gt; & Lt; Label & gt; Hello World & lt; / Label & gt; & Lt; / Div & gt; & Lt; ASP: Button ID = "Button 1" Runat = "Server" Text = "Button" OnClick = "Button 1" On ClientClick = "Toggle Me ();" /> & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt;

Default.aspx.cs

  using the system; Namespace WebApplication2 {Public partial squared default: System.Web.UI.Page {Secure Zero Page_load (Object Sender, EventArgs E) {} Secure Zero Button 1_Click (Object Sender, EventArgs e) {int i = 5; }}}   

I have tried .removeClass, .addclass, .css ... I do not think this is an issue with jQuery as I am using because it

I'm running Chrome to test this if this is the case.

Thank you for your time and support!

The javascript part of your HTML executes in the user's browser Protected Zero Button 1_Click method implemented on the server Does. What is happening is that the user's browser will correctly toggle the device, so that it will disappear. Unfortunately, the HTML of the ASP.NET button will then re-submit the page, which means that the browser refreshes HTML from your server. There is no concept to toggle on the server because Javascript has not executed it, and while Int I is set to 5, after the page refreshes, after the page refresh, the Divis is reset again. Your options are as follows:

1) Do not use a server-side logic to click the button,

  & lt; Asp: button id = "button1" ... />   

and

  & lt; Input type = "button" onclick = "ToggleMe ();" & Gt; Your text & lt; / Input & gt;   

And handle all your client side logic in your javascript. If you need to set int i = 5 on the server, then use JQuery.Ajax to point back to the server.

2) To do this, use only server side. NET It is implemented like this:

  & lt; Html xmlns = "http://www.w3.org/1999/xhtml" & gt; & Lt; Head runat = "server" & gt; & Lt; Title & gt; Trial & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form id = "form1" runat = "server" & gt; & Lt; Div id = "toggleMe" runat = "server" & gt; & Lt; Label & gt; Hello World & lt; / Label & gt; & Lt; / Div & gt; & Lt; ASP: Button ID = "Button 1" Runat = "Server" Text = "Button" OnClick = "Button 1_Click" /> & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt;   

and

  Protected Zero Button 1_Click (Object Sender, EventArgs e) {int i = 5; ToggleMe.Visible =! ToggleMe.Visible; }    

No comments:

Post a Comment