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

techquesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Solar_FlareCommented:
If you create an asp:panel and set it's DefaultButton property to the button's name, then hitting enter when inside the panel will submit the form as if you clicked the button

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit3">
   <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>
</asp:Panel>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
techquesAuthor Commented:
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
0
Solar_FlareCommented:
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?
0
techquesAuthor Commented:
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

0
techquesAuthor Commented:
I found that the panel placed in wrong place, your solution is correct. Thanks for help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.