Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to press enter on keyboard to submit a form in aspx and c#

Hi

I have a asp button which I want to enable it to press enter on keyboard to trigger it.

But the following code does not work.

If the txtrate is empty and I press enter, the js will popup a dialog box.
When I filled in txtrate, txtamount and txtcountervalue and press enter key, it does not submit the form data to DB.


<asp:textbox Runat="server" ID="txtrate" CssClass="form" Width=50 onchange="cald();" />
<asp:textbox Runat="server" ID="txtamount" CssClass="form" Width=60 onchange="cald();" />
<asp:textbox Runat="server" ID="txtcountervalue" CssClass="form" Width=60 onchange="cald();" />
<asp:button id="btnSubmit3" runat="server" cssclass="button" text="Submit" OnClientClick="return checkinputcald();return false" ></asp:button>
 
 
VC# pageload
 
txtrate.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + this.btnSubmit3.UniqueID + "').click();return false;}} else {return true}; ");
txtamount.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + this.btnSubmit3.UniqueID + "').click();return false;}} else {return true}; ");
txtcountervalue.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + this.btnSubmit3.UniqueID + "').click();return false;}} else {return true}; ");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Solar_Flare
Solar_Flare

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of techques
techques

ASKER

I added the panel, but it still cannot insert form data to DB

I also added
Panel1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + this.btnSubmit3.UniqueID + "').click();return false;}} else {return true}; ");
           
Still not work
is the problem that is isn't posting back, or that your code behind isn't inserting data into the database?

if you set break points in your code behind, can you see that it is finding all the form data ok?
yes, you are right, when i add a break point in submit button, it did not fire.

Below is the full source code of asp and c#


<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit3">
<tr align=left>
<td align=left><asp:DropDownList Runat="server" ID="transactiontypeid" CssClass="form" Width=60 /></td>				
<td align=left><asp:DropDownList Runat="server" ID="currency" CssClass="form" AutoPostBack="True" Width=60 onchange="cald();" /></td>
<td align=left><asp:RadioButtonList runat="server" ID="op" name="op" RepeatDirection="Horizontal" TextAlign="Left" onclick="cald();">
<asp:ListItem Value="/" Selected="true">/</asp:ListItem>
<asp:ListItem Value="*">*</asp:ListItem>
</asp:RadioButtonList>
</td>
<td align=left>					    
<asp:textbox Runat="server" ID="txtrate" CssClass="form" Width=50 onchange="cald();" /></td>
<td align=left><asp:textbox Runat="server" ID="txtamount" CssClass="form" Width=60 onchange="cald();" /></td>
<td align=left><asp:textbox Runat="server" ID="txtcountervalue" CssClass="form" Width=60 onchange="cald();" /></td>
<td align=left><asp:textbox Runat="server" ID="txtclientreference" CssClass="form" Width=80 /></td>
<td align=left><asp:textbox Runat="server" ID="txtremarks" CssClass="form" Width=80 /></td>
<td align=left><asp:textbox Runat="server" ID="txtremarks1a" CssClass="form" Width=80 /></td>
<td align=left><asp:textbox Runat="server" ID="txtremarks2a" CssClass="form" Width=80 /></td>
<td align=left><asp:textbox Runat="server" ID="txtremarks3a" CssClass="form" Width=80 /></td>
<td align=left><asp:textbox Runat="server" ID="txtremarks4a" CssClass="form" Width=80 /></td>
<td align=left><asp:textbox Runat="server" ID="txtremarks5a" CssClass="form" Width=80 /></td>
<td align=left><asp:DropDownList Runat="server" ID="bank" CssClass="form" AutoPostBack="True" Width=80 /></td>
<td align=left><asp:DropDownList Runat="server" ID="type" CssClass="form" AutoPostBack="True" Width=80 /></td>
<td align=left><asp:DropDownList Runat="server" ID="location" CssClass="form" Width=160 /></td>
<td align=left><asp:textbox Runat="server" ID="txthandlingincome" CssClass="form" Width=50 /></td>
<td align=left><asp:textbox Runat="server" ID="txthandlingexpense" CssClass="form" Width=50 /></td>
<td align="center"><asp:button id="btnSubmit3" runat="server" cssclass="button" text="submit" OnClientClick="return checkinputcald();return false" ></asp:button>
</td>
</tr></asp:Panel>
 
this.btnSubmit3.Click += new System.EventHandler(this.btnSubmit3_Click);
 
private void btnSubmit3_Click(object sender, System.EventArgs e) 
{
            if (Page.IsValid)
            {
                string cur = "", locate = "";
                if (this.clientid.SelectedItem.Value != "-2")
                {
                    SP_Add_Transaction(0, 0, this.transactiontypeid.SelectedItem.Value, this.clientid.SelectedItem.Value, locate, cur, Convert.ToDecimal(this.txtrate.Text), Convert.ToDecimal(this.txtamount.Text), Convert.ToDecimal(this.txtcountervalue.Text), Convert.ToDecimal(this.txthandlingincome.Text), Convert.ToDecimal(this.txthandlingexpense.Text), this.txtclientreference.Text, this.txtremarks.Text, this.txtremarks1a.Text, this.txtremarks2a.Text, this.txtremarks3a.Text, this.txtremarks4a.Text, this.txtremarks5a.Text);
                }
                else
                {
                }
        }
}

Open in new window

I found that the panel placed in wrong place, your solution is correct. Thanks for help