How to Capture Click Event of Button While Pressing Enter Key From Textbox ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • todashah
    New Member
    • Feb 2008
    • 26

    How to Capture Click Event of Button While Pressing Enter Key From Textbox ?

    Hi ! All,

    I want to developed simple web page using asp.net 2.0
    & C#. This web page page contain one text box & two
    button just like a page of google. Assume that text of Button
    is First & Second. I want to Provide a functionality that once
    i type anything in text box & press enter key then First button
    is invoke & whatever text typed in text box is display in
    message box. If u have any idea then guide me.

    Thanks in advance
  • alamodgal
    New Member
    • Dec 2008
    • 38

    #2
    hi
    you can use panel in your page and set its default button property to id of your button which u want to press first.Just like this example:

    Code:
     <asp:Panel ID="panel1" runat="server" DefaultButton="first">
        <asp:TextBox ID="name" runat="server"></asp:TextBox>
        <asp:Button ID ="first" runat="server" text="First"/>
        <asp:Button id="second" runat="server" Text="Second"></asp:Button>
      </asp:Panel>

    And then onclick event of your button show msgbox like this:

    Code:
     Protected Sub first_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles first.Click
            MsgBox("hello")
    
        End Sub
    Last edited by Frinavale; Feb 23 '09, 03:55 PM. Reason: added [code] tags

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Alamodgal's suggestion's correct.
      The default button property is a very useful feature that can be used to solve your problem.

      Just be aware that MsgBox("My message") will not work in an ASP.NET application.
      Instead you should have some sort of control (like a Label, or Literal or something) on the page that you can set the text for.

      For example, you'd have to add a Label to the Panel:
      Code:
      <asp:Panel ID="panel1" runat="server" DefaultButton="first">
          <asp:TextBox ID="name" runat="server"></asp:TextBox>
          <asp:Button ID ="first" runat="server" text="First"/>
          <asp:Button id="second" runat="server" Text="Second"></asp:Button> 
          <asp:Label id="myMessage" runat="server"></asp:Label>
        </asp:Panel>
      And you would set the Label's Text when the button is clicked in the Button Click Event to display which button was clicked:
      Code:
      protected void first_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      {
         myMessage.Text = "First Button Clicked";
      }

      Comment

      Working...