How to run server methods using jQuery

I have a great application, and I'm going to include a short key for it. I would find 2 JQuery plugins ( demo plugin 1 - Demo plug-in 2 ) that do this for me. can you find both of them https://stackoverflow.com/a/464677/

My application is complete, and I continue to add some features to it, so I don’t want the hard code loaded again.

Since a short cut simply catches a key combination, I wonder how I can call server methods that the short key should light up?

So, how to use any of these plugins just by calling the methods I wrote before? Actually How to start server methods using jQuery?

You can also find a good article here by Dave Ward


Update: Here is the script. When user press CTRL + Del on GridView1_OnDeleteCommand, so I have this

protected void grdDocumentRows_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    try
    {
        DeleteRow(grdDocumentRows.DataKeys[e.Item.ItemIndex].ToString());
        clearControls();
        cmdSaveTrans.Text = Hajloo.Portal.Common.Constants.Accounting.Documents.InsertClickText;
        btnDelete.Visible = false;
        grdDocumentRows.EditItemIndex = -1;
        BindGrid();
    }
    catch (Exception ex)
    {
        Page.AddMessage(GetLocalResourceObject("AProblemAccuredTryAgain").ToString(), MessageControl.TypeEnum.Error);
    }
}

private void BindGrid()
{
    RefreshPage();
    grdDocumentRows.DataSource = ((DataSet)Session[Hajloo.Portal.Common.Constants.Accounting.Session.AccDocument]).Tables[AccDocument.TRANSACTIONS_TABLE];
    grdDocumentRows.DataBind();
}

private void RefreshPage()
{
    Creditors = (decimal)((AccDocument)Session[Hajloo.Portal.Common.Constants.Accounting.Session.AccDocument]).Tables[AccDocument.ACCDOCUMENT_TABLE].Rows[0][AccDocument.ACCDOCUMENT_CREDITORS_SUM_FIELD];
    Debtors = (decimal)((AccDocument)Session[Hajloo.Portal.Common.Constants.Accounting.Session.AccDocument]).Tables[AccDocument.ACCDOCUMENT_TABLE].Rows[0][AccDocument.ACCDOCUMENT_DEBTORS_SUM_FIELD];
    if ((Creditors - Debtors) != 0)
        labBalance.InnerText = GetLocalResourceObject("Differentiate").ToString() + "‏" + (Creditors - Debtors).ToString(Hajloo.Portal.Common.Constants.Common.Documents.CF) + "‏";
    else
        labBalance.InnerText = GetLocalResourceObject("Balance").ToString();

    lblSumDebit.Text = Debtors.ToString(Hajloo.Portal.Common.Constants.Common.Documents.CF);
    lblSumCredit.Text = Creditors.ToString(Hajloo.Portal.Common.Constants.Common.Documents.CF);

    if (grdDocumentRows.EditItemIndex == -1)
        clearControls();
}

The other scenario is the same. How to enable shorthand for this type of code (using session, NHibernate, etc.)

+3
source share
1 answer

This is directly from the links you provided.

In your ASP.NET pg, PageName.aspx, you have a method name decorated with [WebMethod]. To call MethodName from a shortcut, do something similar in javascript:

$(document).bind('keydown', 'Ctrl+c', zzz);  // hotkeys plugin

function zzz() {

    $.ajax({
        type: "POST",
        url: "PageName.aspx/MethodName",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Update your pg accordingly
        }
    });
}

UPD:

[WebMethod]
public static void MethodName(int rownum)
{
    DeleteRow(rownum.ToString());
    clearControls();
    cmdSaveTrans.Text = Hajloo.Portal.Common.Constants.Accounting.Documents.InsertClickText;
    btnDelete.Visible = false;
    grdDocumentRows.EditItemIndex = -1;
    BindGrid();
}
+4
source

Source: https://habr.com/ru/post/1748992/


All Articles