   [.net] Key capture C#
  I'm having difficulty detecting KeyPress, KeyDown, or KeyUp events in C#.  I'm currently using .NET framework 1.1, and working with web forms.  The ultimate goal is to have the web form automatically submit the information if the enter key is pressed.  I was wondering if there was a way to do this using C#, before defaulting to &#106avascript in order to handle this issue.   
  Set autopostback to true and use the text changed event.   ;Quote:Original post by kappaSet autopostback to true and use the text changed event.Could you be a bit more... specific?   ;  If you are using vs.Net all you have to do is go to the controls propperties select autopostback and set it to true, if not you can do it in the HTML code by adding OnTextChanged=[insert_function_name] to the control.In the c# code add something like... protected void insert_function_name(object sender, EventArgs e)    {  //do whatever    }example: <%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Page_Load(object sender, EventArgs e)    {    }    protected void btnOk_Click(object sender, EventArgs e)    {    }    protected void btnPreview_Click(object sender, EventArgs e)    {  }    protected void txtArtNr_TextChanged(object sender, EventArgs e)    {   txtArtNr.Text = "hej";    }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>Untitled Page</title></head><body style="background-color:#E9E9C2">    <form id="form1" runat="server">    <div>   &nbsp;   <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 32px; position: absolute;  top: 32px" Text="Compositör" Width="40px"></asp:Label>   &nbsp;   <asp:TextBox ID="txtTitle" runat="server" Style="z-index: 101; left: 120px; position: absolute;  top: 112px"></asp:TextBox>   &nbsp;   <asp:Label ID="Label2" runat="server" Height="16px" Style="z-index: 102; left: 32px;  position: absolute; top: 72px" Text="Bild" Width="40px"></asp:Label>   &nbsp;   <asp:Label ID="Label3" runat="server" Style="z-index: 103; left: 32px; position: absolute;  top: 112px" Text="CD-Titel"></asp:Label>   <asp:TextBox ID="txtComposer" runat="server" Style="z-index: 104; left: 120px; position: absolute;  top: 32px"></asp:TextBox>   <asp:TextBox ID="txtImage" runat="server" Style="z-index: 105; left: 120px; position: absolute;  top: 72px"></asp:TextBox>   <asp:Label ID="Label4" runat="server" Style="z-index: 106; left: 32px; position: absolute;  top: 152px" Text="Art. Nummer"></asp:Label>   <asp:TextBox ID="txtArtNr" runat="server" Style="z-index: 107; left: 120px; position: absolute;  top: 152px" AutoPostBack="True" OnTextChanged="txtArtNr_TextChanged"></asp:TextBox>   <asp:TextBox ID="txtPriceCode" runat="server" Style="z-index: 108; left: 120px; position: absolute;  top: 192px"></asp:TextBox>   <asp:Label ID="Label5" runat="server" Style="z-index: 109; left: 32px; position: absolute;  top: 192px" Text="Pris kod"></asp:Label>   <asp:Button ID="btnOk" runat="server" OnClick="btnOk_Click" Style="z-index: 110;  left: 224px; position: absolute; top: 232px" Text="Ok" Width="48px" />   <asp:Button ID="btnPreview" runat="server" Style="z-index: 112; left: 72px; position: absolute;  top: 232px" Text="Förhandsgranska" OnClick="btnPreview_Click" />   <br />   <br />   <br />   <br />   <br />   <br />   <br />   <br />   <br />   <br />   <br />   <br />   <br />   <table> <tr> <td rowspan="2" style="width: 76px"><asp:Image ID="Image1" runat="server" Height="100px" Width="100px" /></td> <td>  </td> </tr> <tr> <td style="width: 176px; height: 64px">  </td>  </tr>  </table>   </div>  </form></body></html>;  Although I think kappa has it right, you might be better not to implement that functionality as most users expect something to happen when they press enter.  If you change that function, your users might get a bit confused.   ;Quote:Original post by Rob LoachAlthough I think kappa has it right, you might be better not to implement that functionality as most users expect something to happen when they press enter.  If you change that function, your users might get a bit confused.I'm changing the function so that something does happen when enter is pressed.  Right now, when enter is pressed, it goes back to the main page, as opposed to submitting the form.;  I gave kappa's code a shot.  no such luck.  I'm using VS2003.  I've set all the Textboxes used in the form to AutoPostBack == true, yet no change in result.  The web application seems to focus on a 'home' button created by the guy that worked on this application before I started here.  He Created a Sidebar, and implements the side bar as a user control.  could that have something to do with my issue?   ;  Pressing the Enter key in a textbox seems to automatically do a postback even with the AutoPostBack property not set. If you want to validate, stick code in the Page_Load event:private void Page_Load(object sender, System.EventArgs e){// Put user code to initialize the page hereif (this.IsPostBack){//validate formif (this.TextBox1.Text.CompareTo("This is a test") != 0){this.Label1.Text = "Validation failed";}else{this.Label1.Text = "Validation succeeded";}}};Quote:Original post by MachairaPressing the Enter key in a textbox seems to automatically do a postback even with the AutoPostBack property not set. If you want to validate, stick code in the Page_Load event:*** Source Snippet Removed ***Thanks machaira.I figured I'd take a moment and follow up on your advice.  Your code snippet put me on the right track, this is what I did to solve the issue.private void Page_Load(object sender, System.EventArgs e){if(!this.IsPostBack){ //Create Session variable for storing search criteriaSession["Query"] = txtQuery.Text.ToString();}else{//Check for changes against previous search criteria on postback  if(txtQuery.Text!= Session["Query"].ToString()){Session["Query"] = txtQuery.Text.ToString();btnSubmit_Click(this, e);}}}