Javascript 전 asp:ButtonField 클릭
-
03-07-2019 - |
문제
나 GridView
에서 제어 Asp.net 응용 프로그램,가 <asp:buttonField>
의 type="image"
고 CommandName="Delete"
.
가를 실행하는 방법을 조각을 자바스크립트에 도달하기 전에 OnRowDelete
이벤트?
내가 원하는 단순한 확인을 삭제하기 전에 행이 있습니다.
감사합니다!
편집:시 <asp:ButtonField>
태그 이 없 인 OnClientClick
특성이 있습니다.
해결책
내가 사용하는 것 TemplateField 대신,그리고 채우 ItemTemplate 일반 asp:버튼 또는 asp:ImageButton 따라 중 하나이 무엇이 필요합니다.할 수 있습한 후 실행하여 동일한 논리는 RowCommand 이벤트려고 할 때 그것을 가로채는 삭제 명령입니다.
중 하나에서 그의 버튼이 나는 것 다음 사용하 OnClientClick 속성을 실행하 JavaScript 확인창이다.
<script type="text/javascript">
function confirmDelete()
{
return confirm("Are you sure you want to delete this?");
}
</script>
...
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="DeleteButton" runat="server"
ImageUrl="..." AlternateText="Delete" ToolTip="Delete"
CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
OnClientClick="return confirmDelete();" />
</ItemTemplate>
</asp:TemplateField>
다른 팁
이 작업을 수행하는 가장 우아한 방법은 jQuery를 사용하여 OnClick 이벤트를 연결하는 것입니다.
<script type="text/javascript">
$(".deleteLink").click(function() {
return confirm('Are you sure you wish to delete this record?');
});
</script>
...
<asp:ButtonField ButtonType="Link" Text="Delete"
CommandName="Delete" ItemStyle-CssClass="deleteLink" />
임의의 CSS 클래스를 사용하여 링크 버튼을 식별합니다.
에서 GridView
'에스 RowCreated
이벤트 핸들러, 사용 FindControl
명명 된 버튼을 찾고 속성 컬렉션에 추가하려면 다음과 같습니다.
btn.Attributes.Add("onclick", "return confirm('delete this record?');");
ASP.NET 코드는 확인 ()가 true 인 경우에만 실행됩니다. 즉, 즉 확인되었습니다.
그래서 자바 스크립트 기능이 있습니다.
function confirmDeleteContact() {
if (confirm("Are you sure you want to delete this contact?")) {
document.all.answer.value="yes";
} else {
document.all.answer.value="no";
}
}
그리고 나는 그것을 같은 그리드 항목으로 연결합니다.
Sub dgbind(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgcontacts.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
CType(e.Item.Cells(9).Controls(0), System.Web.UI.WebControls.LinkButton).Attributes.Add("onclick", "javascript:confirmDeleteContact();")
End Select
End Sub
이것은 오래된 코드이므로 변경할 수있는 몇 가지 사항을 볼 수 있지만 도덕은 다음과 같습니다. 다른 모든 것이 실패하면 행 바인딩 중에 JavaScript "Onclick"을 추가하십시오. "document.all.answer.value"는 숨겨진 필드입니다 runat=server
Postback에서 값을 읽을 수 있도록.
ButtonField를 사용하는 경우 추가 참조 시스템을 추가하여 더 좋습니다.
버튼 필드가 최선의 선택이라면 이것이 당신의 선택입니다 .. 샘플 :
using System.Windows.Forms;
protected void BorrowItem_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
if (System.Windows.Forms.MessageBox.Show("Do you want to delete", "Delete",MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification) != System.Windows.Forms.DialogResult.OK)
{
return;
}
}
//Continue execution...
}
//drimaster