![](/img/trans.png)
[英]How to call javascript function in item template when using gridview
[英]How to call JavaScript function in GridView Command Field?
我已經返回一個Javascript函數,它向用戶詢問確認消息。 JavaScript函數是
function ConfirmOnDelete() {
if (confirm("Are you sure to delete?"))
return true;
else
return false;
和GridView如下所示:
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
這里我想在用戶單擊GridView命令字段中的Delete時調用JavaScript函數。 怎么稱呼這個?
假設您要繼續使用CommandField
,可以使用GridView的OnRowDataBound
事件以編程方式執行此操作。
在GridView
聲明中為RowDataBound事件指定事件處理程序:
<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound"....
然后在你的事件處理程序(代碼隱藏)中找到你的按鈕(這里我假設ImageButton
,雖然這取決於你的CommandField
ButtonType
屬性)並將JavaScript添加到它的OnClientClick
屬性。
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((ImageButton)e.Row.Cells[cell].Controls[ctrl]).OnClientClick = "return confirm('Are you sure you want to delete?');"; // add any JS you want here
}
}
在上面的示例中, cell指的是CommandField
的列索引, ctrl指的是您引用的單元格中的控件索引(Delete按鈕)。
您最好避免在服務器端詢問確認刪除,使用javascript捕獲用戶最終決定,然后轉到服務器端執行您的邏輯。 CommandField不是這里最好的解決方案。
JS:
<script type="text/javascript">
function DeleteConfirm() {
if (confirm("Are you sure you want to delete this customer from excluded customer list ?")) {
return true;
}
return false;
}
</script>
HTML:
<asp:TemplateField HeaderText=" ">
<ItemTemplate>
<asp:LinkButton ID="lnk_RemoveFromlist" runat="server" Text="Delete"
CommandName="Delete" OnCommand="Delete_Command" CommandArgument='<%# Eval("ID").ToString()' OnClientClick='return DeleteConfirm()'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
碼:
protected void Remove_Command(object sender, CommandEventArgs e)
{
//Implement your Delete Logic
}
取消按鈕的命令名稱為“取消”,刪除按鈕“刪除”,檢查
if(e.CommandName == "delete")
{
//script to delete();
}
else if(e.commandName == "cancel")
{
//close with some script;
}
在codebehind中調用javascript函數,
Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function","loadPopupBox();", true);
通常,您必須指定OnClientClick="return confirm('Are you sure you want to delete ?');"
但這不適用於CommandField更好地使用TemplateField ,這解釋得更好http://davesquared.net/2007/10/confirm-delete-for-gridview.html
您可以使用按鈕的OnClientclick事件來調用該函數。例如
<asp:button id="btndelete" runat="server" Text="delete" Onclientclick="if(!ConfirmOnDelete())return false;"/>
使用啟動腳本
ScriptManager.RegisterStartupScript(Page, this.GetType(), "ConfirmOnDelete", "ConfirmOnDelete();", true);
或者您也可以使用onclientclick
HTML:
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="gv1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CommandField ShowDeleteButton="true" HeaderText="delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
碼:
protected void gv1_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
GridView1.Attributes.Add("OnClick", "return confirm('Really wanna delete?');");
// implement your delete logic
Response.Write("<script>alert("Delete Successful");</script>");
}
當我們點擊Gridview中的“刪除”命令字段時,這將調用Javascript函數,Response.write函數將發出數據已被刪除的警報。 您可以在此函數中添加要執行的whwtever函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.