Save data in Database on TextBox enter key press using jQuery in ASP.Net

Last Reply on May 23, 2018 03:41 AM By mukesh1

Posted on May 22, 2018 04:03 AM

how can save data in webform in database in asp.net webform on enter key press.Enter key code is 13.

on textbox if i click enter it will save in database.in webform i didn't find any keypress event.

$('[id*=txtbox]').keypress(function(e){
        if (e.keyCode == 13) {
var value=document.getElementById("txtbox").value

 

Posted on May 22, 2018 06:20 AM Modified on on May 22, 2018 11:44 PM

Hi mukesh1,

There is no server side KeyPress event for TextBox in ASP.Net. So you need to add a Button and make its style to display none. Then write the saving code in Button click event.

When the TextBox keypress event is fired (i.e. enter button is pressed) at client side you need to trigger the Button click event at client side.

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSave" Text="text" runat="server" Style="display: none" OnClick="Save" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('[id*=txtName]').keypress(function (e) {
            if (e.keyCode == 13) {
                $('[id*=btnSave]').trigger('click');
                return false;
            }
        });
    });
</script>

Code

protected void Save(object sender, EventArgs e)
{
    string name = txtName.Text;
    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Name is : " + name + "')", true);
}

Screenshot


Posted on May 23, 2018 03:41 AM

my code was not trigger button event coz on button i used Visible="false" in place of Style="display:none"

now i changed,it's working fine.