views:

216

answers:

2

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 application 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)

+2  A: 

Probably this link here can help you out

Greco
Thanks alot Dimi, but this case is different from my case. I have serverside methods and Ijust want towrite Jquery code. I didn't underestand what was the answer.
Nasser Hadjloo
+3  A: 

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();
}
Steve
@Steve, with this approach I can't enable short-cut. take a lookt at my question again. I Update it with a peace of code
Nasser Hadjloo
I'm not sure I understand. You want to assign a keystroke to delete a row? But then, how do you know which row to delete? Assuming you have a row selected and you want the keystroke to delete that row, you have to in javascript, extract the row number and feed it to the Methodname in the ajax() call. Then you have to extract the C# delete code and put it in a WebMethod called MethodName if I'm interpreting things correctly.
Steve
@steve - the shor-cut to delete is the Delete+Number for example Delete+3 will remove line 3 and this functionality just work for 9 row for other rows user has to select the row and then press CTRL+Delete so there are some short-cut key not only one. and I really confused to implement this. I don't know what should Ido and how to do it. I Update my question with my delete row method, can you take a look at it and suggest a solution to implement short-cut to really call my server-side methods? thank you.
Nasser Hadjloo
@Hajloo - I'd bind each shortcut, ctrl+1,2,3 ... to zzz1(), zzz2(), zzz3() and call MethodName(rowNum) based on the zzz function called, see the update.
Steve