VB.NET OnClientClick javascript server side
Here is the scenario:
I have a aspx page that displays Table with few data columns driven by database and an additional column which displays a dynamically created "Delete" button. This button is created at run time in the server side code (vb.net). When the user clicks this delete button, i want to display a confirmation message.
Solution:
1. Define the following javascript function within aspx page:
<script type = "text/javascript">
function ConfirmDeleteClient() {
var confirm_output = document.createElement("INPUT");
confirm_output.type = "hidden";
confirm_output.name = "confirm_output";
if (confirm("Are you sure you want to delete this item?")) {
confirm_output.value = "Yes";
} else {
confirm_output.value = "No";
}
document.forms[0].appendChild(confirm_output);
}
</script>
2. Code for creating the "delete" button dynamically in Page_load():
btnDelete.Text = "Delete"
btnDelete.ID = sqlRead("record_id").ToString()
btnDelete.PostBackUrl = "~/xxx.aspx"
AddHandler btnDelete.Click, AddressOf btnDelete_Click
btnDelete.OnClientClick = "ConfirmDeleteClient()"
3. Click handler:
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = CType(sender, Button)
Try
Dim confirmValue As String = Request.Form("confirm_output")
If confirmValue = "Yes" Then
'Whatever action you want to take
End If
'result = MsgBox("Are you sure you want to delete this letter?", MsgBoxStyle.ApplicationModal + MsgBoxStyle.YesNo, "Please confirm")
Catch ex As Exception
logger.Error(ex)
End Try
End Sub
I have a aspx page that displays Table with few data columns driven by database and an additional column which displays a dynamically created "Delete" button. This button is created at run time in the server side code (vb.net). When the user clicks this delete button, i want to display a confirmation message.
Solution:
1. Define the following javascript function within aspx page:
<script type = "text/javascript">
function ConfirmDeleteClient() {
var confirm_output = document.createElement("INPUT");
confirm_output.type = "hidden";
confirm_output.name = "confirm_output";
if (confirm("Are you sure you want to delete this item?")) {
confirm_output.value = "Yes";
} else {
confirm_output.value = "No";
}
document.forms[0].appendChild(confirm_output);
}
</script>
2. Code for creating the "delete" button dynamically in Page_load():
btnDelete.Text = "Delete"
btnDelete.ID = sqlRead("record_id").ToString()
btnDelete.PostBackUrl = "~/xxx.aspx"
AddHandler btnDelete.Click, AddressOf btnDelete_Click
btnDelete.OnClientClick = "ConfirmDeleteClient()"
3. Click handler:
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = CType(sender, Button)
Try
Dim confirmValue As String = Request.Form("confirm_output")
If confirmValue = "Yes" Then
'Whatever action you want to take
End If
'result = MsgBox("Are you sure you want to delete this letter?", MsgBoxStyle.ApplicationModal + MsgBoxStyle.YesNo, "Please confirm")
Catch ex As Exception
logger.Error(ex)
End Try
End Sub
Comments