我需要在asp.net GridView中使用jQuery捕獲'更新'單擊事件,並且無法知道從何處開始。我還是比較新的jQuery。我的GridView連接到一個SQLDataSource,自然而然地,組合提供了所有的花裏胡哨的東西。任何幫助將不勝感激。如何使用jQuery在ASP.NET GridView中捕獲'更新'單擊事件
回答
只需在聲明GridView後的任意位置添加腳本塊,它就可以與默認的非模板化GridView列配合使用。在代碼隱藏中沒有代碼,因爲它純粹是一個Javascript解決方案。
使用此,如果您使用的是菱型的GridView列:
<script type="text/javascript">
// a:contains(The text of the link here)
$('#<%= theGridViewID.ClientID %> a:contains(Update)').click(function() {
alert('Update click event captured from the link!');
// return false: stop the postback from happening
// return true or don't return anything: continue with the postback
});
</script>
使用此,如果您使用的是按鈕式的GridView列,您不希望您的Javascript功能來阻止回傳:
<script type="text/javascript">
// :button[value=The text of the button here]
$('#<%= theGridViewID.ClientID %> :button[value=Update]').click(function() {
alert('Update click event captured from the button!');
});
</script>
使用此,如果您使用的是按鈕式的GridView列,你想擁有控制是否繼續回發或不:
<script type="text/javascript">
// :button[value=The text of the button here]
var updateButtons = $('#<%= theGridViewID.ClientID %> :button[value=Update]');
updateButtons
.attr('onclick', null)
.click(function() {
alert('Update click event captured from the button!');
var doPostBack = true; // decide whether to do postback or not
if (doPostBack) {
var index = updateButtons.index($(this));
// 'Update$' refers to the GridView command name + dollar sign
__doPostBack('<%= theGridViewID.UniqueID %>', 'Update$' + index);
}
});
</script>
更新:我認爲這將是替換最後一個(第三)腳本塊我上面提出的更好的解決方案,因爲你不會需要更新__doPostBack函數調用手動基於命令名,因此,它應該不太容易出錯:
<script type="text/javascript">
// :button[value=The text of the button here]
var updateButtons = $('#<%= theGridViewID.ClientID %> :button[value=Update]');
updateButtons.each(function() {
var onclick = $(this).attr('onclick');
$(this).attr('onclick', null).click(function() {
alert('Update click event captured from the button!');
var doPostBack = true; // decide whether to do postback or not
if (doPostBack) {
onclick();
}
});
});
</script>
感謝Aristos的這個想法。 :)
我說腳本塊應該放在GridView聲明後的某個地方,但是如果因爲某種原因你堅持它出現在某個地方之前(在頭部或許?),你總是可以用$(function(){/* scripts in here * /}),這樣腳本只有在DOM準備就緒後才能執行。 – Amry 2010-04-20 16:38:58
不錯的工作我也很喜歡你的工作。我唯一公平的是,你實際上試圖重新創建doPostBack - 是否有任何方法可以在OnClick事件中獲取控件實際存在的內容,然後再進行更改,將其保存在函數變量中,並在工作之後調用它。 – Aristos 2010-04-21 10:17:51
@Aristos:確實很好的建議。奇怪我不知怎的想到這一點。更新我的答案,包括這一點,信用給你。 :) – Amry 2010-04-21 14:53:33
您需要將客戶端事件偵聽器附加到Update [link]按鈕的click事件。如果你這樣做,我認爲不能使用AutoGenerateEditButton =「true」來完成。您需要使用TemplateField,以便操作按鈕。然後你可以使用jQuery綁定到按鈕的點擊事件。
將更新列添加到列模板。將它轉換爲一個自定義列,然後修改它,讓它可以用jquery綁定到它,即向它添加一個css類。
好的,這裏是我的解決方案,從按鈕只捕獲一個更新(或更多)。
這是我在更新點擊運行
<script type="text/javascript">
function NowRunTheUpdate(){
alert("ok I capture you");
}
</script>
這裏的JavaScript代碼的頁面代碼
`<asp:GridView ID="MyGridView" runat="server" OnRowDataBound="MyGridView_RowDataBound" ... >`
<asp:ButtonField Text="update" CommandName="Update" ButtonType="Button" />
...
下面是代碼多數民衆贊成在運行的後面,設置的JavaScript。
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
// check all cells in one row
foreach (Control control in cell.Controls)
{
// I go to get the button if exist
Button button = control as Button;
if (button != null && button.CommandName == "Update")
// Add delete confirmation
button.OnClientClick = "NowRunTheUpdate();";
}
}
}
}
Gridview只不過是一堆「tr」和「td」表。如果你理解了這個概念,那麼你很容易在客戶端處理任何事情。如果您啓用了自動設置,那麼它將成爲編輯,刪除,更新或取消(檢查視圖源)的鏈接。下面給出的代碼應該捕獲更新click事件:
$("a:contains(Update)").live("click", function() {
//alert("hi"); do what needs to be done
return false;//would not sent the control back to server
});
HTH
@Raja我喜歡這個想法 - 它需要一些工作,以防萬一你有很多網格,並且一定不要與其他更新按鈕衝突,但我喜歡它。你也需要jQuery,但我仍然使用它。我認爲更好的部分是,你不需要在代碼隱藏方面循環網格。我會嘗試測試它,並可能使用它。 – Aristos 2010-04-21 10:05:33
@Raja我現在檢查代碼,我認爲它需要更多的東西 - 如果你可以幫忙的話。這不是鏈接,而是輸入,然後輸入使postBack onClick。如何捕獲並保存postBack中已存在的所有輸入內容(使用jQuery? – Aristos 2010-04-21 10:09:36
@Raja 這裏是你需要插入你的OnLick的代碼,你可以幫忙嗎? – Aristos 2010-04-21 10:10:54
- 1. 如何使用asp.net mvc捕獲fullcalender中單擊事件的事件數據
- 2. 如何使用單擊按鈕單擊ASP.net中的gridview更新多行?
- 3. ASP.Net Gridview更新事件
- 4. 如何捕獲右鍵單擊事件?
- 5. 如何使gridview列超鏈接可點擊並捕獲事件?
- 6. 單擊'新文檔'時Tinymce捕獲單擊事件
- 7. 如何使用jQuery爲iframe捕獲點擊事件?
- 8. 如何使用getMouse捕獲右鍵單擊事件()
- 9. 用Python捕獲Windows單擊事件
- 10. 在圖表上單擊捕獲事件
- 11. 捕獲標籤單擊事件
- 12. 更新gridview的點擊使用更新面板在asp.net
- 13. 如何在flex中捕獲鼠標滾輪單擊事件?
- 14. 使用MessageFilter在控件上捕獲單擊事件
- 15. 如何捕獲WindowsFormsHost控件中的鼠標單擊事件?
- 16. 在jQuery中單擊更改事件
- 17. 在鼠標單擊事件上編輯和更新gridview行
- 18. 如何在asp.net gridview中使用jquery tablesorter?
- 19. 如何在JQuery中捕獲TextArea文本更改事件
- 20. 如何在jQuery Mobile中更改制表符時捕獲事件
- 21. 如何在UWP中的GridView中捕獲滾動事件
- 22. 捕獲右鍵單擊事件
- 23. 如何從gridview中的按鈕獲取單擊事件
- 24. 在asp.net中使用SqlDataSource更新gridview
- 25. DevExpress MVC GridView - 如何獲取單元格單擊事件
- 26. jquery onHover從單擊事件中捕獲一個css值?
- 27. Phonegap - jquery手機:按鈕單擊事件不能捕獲在android
- 28. 使用javascript捕獲點擊事件
- 29. 捕獲點擊事件
- 30. 捕捉單擊事件和使用validate.js
什麼是「更新」,用戶會點擊 - 它是一個按鈕,鏈接,或其他什麼東西? – 2010-04-16 16:44:27