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

Last Reply one year ago By mukesh1

Posted one year ago

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 one year ago Modified on one year ago

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 one year ago

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.