WebForm Enter Key

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am sure this has been asked before but I cannot find it...

I would like to assign the Enter Key to one of the buttons on my web form. I cannot see a property or anything in the SDK.
 
Hi Dirk,

I take it you're looking to set a default button. You can do that with

Page.RegisterHiddenField _
("__EVENTTARGET", "Button1")

You need to provide the name of the button (Button1) in this example. Sample
code below.

Does this help?

Ken
Microsoft MVP [ASP.NET]


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Page.RegisterHiddenField _
("__EVENTTARGET", "Button1")
End Sub
Private Sub Button2_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click
Label1.Text = "You clicked Cancel"
End Sub
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = "You clicked OK"
Label2.Text = TextBox1.Text
End Sub

<form id="Form1" method="post" runat="server">
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P>&nbsp;
<asp:Button id="Button2" runat="server"
Text="Cancel"></asp:Button>&nbsp;
<asp:Button id="Button1" runat="server" Text="OK"></asp:Button></P>
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>
<P>
<asp:Label id="Label2" runat="server"></asp:Label></P>
</form>
 
if u can use a "submit" button, u can fix this easily. it is the same way
with the old fashion.

hope this helps,
CMA


Dirk said:
I am sure this has been asked before but I cannot find it....

I would like to assign the Enter Key to one of the buttons on my web form.
I cannot see a property or anything in the SDK.
 
Thanks Ken, that is what I want to do. However it does not work, the enter key still does nothing. The only difference is that I am using c#. See below:

<body><form id="Form1" method="post" runat="server"><asp:Button id="Button1" runat="server" Text="Button1"></asp:Button><asp:Button id="Button2" runat="server" Text="Button2"></asp:Button><asp:Label id="Label1" runat="server">Label</asp:Label></form></body>

public class EnterKey : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Page.RegisterHiddenField("__EVENTTARGET", "Button1");
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = "You pressed button one";
}

private void Button2_Click(object sender, System.EventArgs e)
{
Label1.Text = "You pressed button two";
}
}
 
Back
Top