Imports System.Data
Partial Class SwapnaTestPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sJSCode As New System.Text.StringBuilder
If Not IsPostBack Then
If Request.QueryString("Type") = "A" Then
sJSCode.Append(" <script language=""javascript"">" & vbCrLf)
sJSCode.Append(" var sMessage = 'Submitted Successfully';" & vbCrLf)
sJSCode.Append(" alert(sMessage);" & vbCrLf)
sJSCode.Append(" </script>" & vbCrLf)
ClientScript.RegisterStartupScript(GetType(String), "MyCode", sJSCode.ToString)
End If
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lngArticleBannedTextID As Long
lngArticleBannedTextID = 12
If lngArticleBannedTextID > 0 Then
ClientScript.RegisterStartupScript(GetType(String), "MyCode", "<Script Language=Javascript>myfunction()</script>")
End If
If HiddenField1.Value = "True" Then
Response.Redirect("SwapnaTestPage.aspx?Type=A")
Else
Response.Redirect("SwapnaTestPage.aspx?Type=N")
End If
End Sub
End Class
Based on the hidden field value after i click the confirm button i want to show the output.
But in my code it is not showing the confirm box. but it is showing the alert message which is supposed to be shown only after pressing on of the button in confirm box.
Please tell me what is wrong
Thanks
Swapna
Please click "mark as answer" if this post helped you.
I think this code may be conflicting with regestering the JS to the page. In the line before you are registering the JS to the page, but in the very next line you are re-directing back to the same page. Your JS will never get run.
If you want that JS to appear, you are going to need to remove that block of code that re-directs the page. Then your JS function will run. Then you will need to redirect from your JS function or re-work your code to do what you need. If you need to redirect
in JS, the syntax is as follows:
swapnasamson...
Member
410 Points
299 Posts
how to get the javascript confirm message work in server side vb.net
Sep 24, 2009 06:19 AM|LINK
Hello,
I'm using vb.net 2005. Here is my code:
<%@ Page Language="VB" MasterPageFile="~/SwapnaMaster.master" AutoEventWireup="false" CodeFile="SwapnaTestPage.aspx.vb" Inherits="SwapnaTestPage" title="Untitled Page" %> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" > <script language="javascript" type="text/javascript"> function myfunction() { var hiddenfield = document.getElementById('<%=HiddenField1.clientID%>'); if(confirm('Are you sure you want to Delete ?')) { hiddenfield.value = true; } else { hiddenfield.value = false; } } </script> <center> <table width="100%"> <tr> <td> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:HiddenField ID="HiddenField1" runat="server" /> </td> </tr> </table> </center> </asp:Content>vb code
Imports System.Data Partial Class SwapnaTestPage Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim sJSCode As New System.Text.StringBuilder If Not IsPostBack Then If Request.QueryString("Type") = "A" Then sJSCode.Append(" <script language=""javascript"">" & vbCrLf) sJSCode.Append(" var sMessage = 'Submitted Successfully';" & vbCrLf) sJSCode.Append(" alert(sMessage);" & vbCrLf) sJSCode.Append(" </script>" & vbCrLf) ClientScript.RegisterStartupScript(GetType(String), "MyCode", sJSCode.ToString) End If End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim lngArticleBannedTextID As Long lngArticleBannedTextID = 12 If lngArticleBannedTextID > 0 Then ClientScript.RegisterStartupScript(GetType(String), "MyCode", "<Script Language=Javascript>myfunction()</script>") End If If HiddenField1.Value = "True" Then Response.Redirect("SwapnaTestPage.aspx?Type=A") Else Response.Redirect("SwapnaTestPage.aspx?Type=N") End If End Sub End ClassBased on the hidden field value after i click the confirm button i want to show the output.
But in my code it is not showing the confirm box. but it is showing the alert message which is supposed to be shown only after pressing on of the button in confirm box.
Please tell me what is wrong
Swapna
Please click "mark as answer" if this post helped you.
anfran.mende...
Participant
1398 Points
241 Posts
Re: how to get the javascript confirm message work in server side vb.net
Sep 24, 2009 12:38 PM|LINK
Try changing the bold part in your apps.
atconway
All-Star
16846 Points
2756 Posts
Re: how to get the javascript confirm message work in server side vb.net
Sep 24, 2009 02:05 PM|LINK
I think this code may be conflicting with regestering the JS to the page. In the line before you are registering the JS to the page, but in the very next line you are re-directing back to the same page. Your JS will never get run.
This link explains your situation exactly:
http://blog.dmbcllc.com/2009/01/28/responseredirect-and-javascript/
If you want that JS to appear, you are going to need to remove that block of code that re-directs the page. Then your JS function will run. Then you will need to redirect from your JS function or re-work your code to do what you need. If you need to redirect in JS, the syntax is as follows:
http://www.sbp-romania.com/Blog/2009/01/27/making-a-response-redirect-from-plain-html-or-javascript.aspx