Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Onblur event of a textbox cancels onclick event of a button.

Onblur event of a textbox cancels onclick event of a button.

Scheduled Pinned Locked Moved ASP.NET
helpcsharphtmlsysadmintutorial
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bin_bin1
    wrote on last edited by
    #1

    Hello there, I met a problem. I have a textbox, requirefiledValidator and a button on the same row. If I don't put anything in the textbox first and hit the button. The validator is triggered. (The display of the validator is dynamic.) Then I put something in the textbox and hit the button again. But the button's onclick event is not triggered. I figured out the reason. I think the mouse click caused the onblur event of the textbox, then the validator error message is gone, then the button position is changed. So the onclick is not on the button anymore. The sample code is here. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="textbox1" Display="Dynamic" ErrorMessage="RequiredFieldValidator">This field is required</asp:RequiredFieldValidator> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> <br /> </form> </body> </html> Is there a way to make the button onclick event still be fired? I think I can use setTimeout to delay the validation a little bit. But I don't know how to do it. Any suggestion will be greatly appreciated. Bin

    V 1 Reply Last reply
    0
    • B bin_bin1

      Hello there, I met a problem. I have a textbox, requirefiledValidator and a button on the same row. If I don't put anything in the textbox first and hit the button. The validator is triggered. (The display of the validator is dynamic.) Then I put something in the textbox and hit the button again. But the button's onclick event is not triggered. I figured out the reason. I think the mouse click caused the onblur event of the textbox, then the validator error message is gone, then the button position is changed. So the onclick is not on the button anymore. The sample code is here. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="textbox1" Display="Dynamic" ErrorMessage="RequiredFieldValidator">This field is required</asp:RequiredFieldValidator> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> <br /> </form> </body> </html> Is there a way to make the button onclick event still be fired? I think I can use setTimeout to delay the validation a little bit. But I don't know how to do it. Any suggestion will be greatly appreciated. Bin

      V Offline
      V Offline
      Venkatesh Mookkan
      wrote on last edited by
      #2

      bin_bin1 wrote:

      ControlToValidate="textbox1" Display="Dynamic" ErrorMessage="RequiredFieldValidator">This field is required

      bin_bin1 wrote:

      I think the mouse click caused the onblur event of the textbox, then the validator error message is gone, then the button position is changed. So the onclick is not on the button anymore.

      You controls are in flow direction (one behind the others). Use BREAKS next to each controls or tables with fixed column width.

      Venkatesh Mookkan My: Website | Yahoo Group | Blog Spot

      B 1 Reply Last reply
      0
      • V Venkatesh Mookkan

        bin_bin1 wrote:

        ControlToValidate="textbox1" Display="Dynamic" ErrorMessage="RequiredFieldValidator">This field is required

        bin_bin1 wrote:

        I think the mouse click caused the onblur event of the textbox, then the validator error message is gone, then the button position is changed. So the onclick is not on the button anymore.

        You controls are in flow direction (one behind the others). Use BREAKS next to each controls or tables with fixed column width.

        Venkatesh Mookkan My: Website | Yahoo Group | Blog Spot

        B Offline
        B Offline
        bin_bin1
        wrote on last edited by
        #3

        Thank you so much for your reply. My code is just an example. I am working on an application page in SharePoint. I cannot change the design. the button will change position when the validation messages show or hide. I think there are two possible solutions. 1) Make the textbox's onblur event does not cause validation. I don't know how to do that. I tried to add Textbox1.Attributes.Add["onblur", "return false;"]; But it is not working. 2) Delay the validation for 100milli second by using setTimeout method. I really don't know how to do that. Please help me with this. Thank you so much. Best regards, Bin

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups