2010-06-08 54 views
3

我有一個很大的應用程序,我打算爲它啓用快捷鍵。我會找到2個適用於我的JQuery插件(demo plug-in 1 - Demo plug-in 2)。你可以找到他們兩個in this post in StackOverFlow如何使用jQuery觸發服務器端方法

我的應用程序是一個完整的,我想增加一些功能,所以我不想再towrite代碼。

所以作爲一個快捷方式只是捕捉一個關鍵組合,我不知道如何調用快捷鍵應該觸發的服務器方法?

那麼如何通過調用我之前編寫的方法來使用這些插件中的任何一個? 其實如何使用jquery引發服務器方法?

您還可以找到一個很好的文章here, by Dave Ward


更新:這裏的情景。當用戶按下CTRL +德爾GridView1_OnDeleteCommand所以我有這個

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(); 
} 

釷等情況都是一樣的。如何啓用這些代碼的快捷方式(使用會話,NHibernate等)

+0

您可以發佈在服務器端代碼的一些例子嗎? – 2010-06-08 12:33:47

+0

@Mark,我會更新我的問題,查看代碼示例 – 2010-06-08 13:04:56

+0

您是否找到答案?如果是的話請在這裏發佈:)(謝謝你的好jQuery DatePicker插件:)我正在休閒你的網站帖子:)) – Shahin 2010-10-29 06:48:46

回答

4

這是直接從你給的鏈接。

在ASP.NET pg,PageName.aspx中,您有一個用[WebMethod]裝飾的MethodName。若要從一個快捷方式調用方法名,做這樣的事情在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

用這種方法我不能啓用捷徑。再看看我的問題。我更新了代碼的和平 – 2010-06-08 13:06:05

+0

我不知道我理解。你想分配一個按鍵來刪除一行?但是,你怎麼知道要刪除哪一行?假設您選擇了一行,並且您希望按鍵刪除該行,則必須在javascript中提取行號並將其提供給ajax()調用中的Methodname。然後你必須提取C#刪除代碼,並將其放入一個名爲MethodName的WebMethod中,如果我正確地解釋了事情。 – Steve 2010-06-08 18:05:16

+0

要刪除的shor-cut是Delete + Number,例如Delete + 3將刪除第3行,並且此功能僅適用於其他行的第9行用戶必須選擇該行,然後按CTRL + Delete,這樣會出現一些short-不僅切一把鑰匙。我真的很困惑,要實現這一點。我不知道我該怎麼做,以及如何去做。我用我的刪除行方法更新我的問題,你可以看看它,並建議一個解決方案來實現快捷方式來真正調用我的服務器端方法嗎?謝謝。 – 2010-06-09 06:46:55