开发者

How to fire server-side methods with jQuery

I have a large application and I'm going to enabling short-cut key for it. I'd find 2 JQuery plug-ins (demo plug-in 1 - Demo plug-in 2) that do this for me. you can find both of them in this post in StackOverFlow

My applicat开发者_StackOverflow社区ion is a completed one and I'm goining to add some functionality to it so I don't want towrite code again.

So as a short-cut is just catching a key combination, I'm wonder how can I call the server methods which a short-cut key should fire?

So How to use either of these plug-ins, by just calling the methods I'd written before? Actually How to fire Server methods with Jquery?

You can also find a good article here, by Dave Ward


Update: here is the scenario. When User press CTRL+Del the 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();
}

Th other scenario are the same. How to enable short-cut for these kind of code (using session , NHibernate, etc)


This is directly from the links you gave.

In your ASP.NET pg, PageName.aspx, you have a MethodName decorated with [WebMethod]. To call MethodName from a shortcut, do something like this 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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜