0
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
        '...vwmr for view more; atcr for add to cart
        Dim var As String = e.CommandArgument.ToString
        Dim type As String = Left(var, 4)
        Dim ItemID As String = Replace(var, type, "")
        'Dim ad As LinkButton = CType(ListView1.FindControl("addToCart"), LinkButton)
        Dim myStringVariable As String = "You need to login first before adding anything to the cart!"

        If type = "vwmr" Then     '........go to view more page....
            Dim url As String = "~/dedicatedItemPage_aspx/dedicatedItemPage.aspx?typeOfItem=" & _
                                Request.QueryString("typeOfItem") & "&itemID=" & ItemID
            Response.Redirect(url, True)

        ElseIf type = "atcr" Then     '..........add the item to the cart.....
            If User.Identity.Name = "" Then
                Response.Write("<script type='javascript'>window.alert('" + myStringVariable + "')</script>")


                '            ClientScript.RegisterStartupScript(Me.[GetType](), "myalert", _
                '"alert('" + myStringVariable + "');" + _
                '"document.location = '" + ResolveUrl("~/login.aspx") + "';", True)
                '            'MsgBox("You need to login first before adding anything to the cart!", , "")
                'Response.Write("<script>alert('Hello')</script>")
                'ServerAlert.Show()

                'ClientScript.RegisterStartupScript(Me.[GetType](), "myalert", "alert('" + myStringVariable + "');", True)

                'ad.Attributes.Add("onclick", _
                ' "return confirm('Are you sure you want to delete?');")
                'Alert.Show("You need to login first before adding anything to the cart!")
                'MessageBox1.ShowInfo("You need to login first before adding anything to the cart!", 300, 400)
                'Response.Redirect("~/login.aspx", True)

            Else
                '..........pull details from database..........
                Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;" & _
                                               "AttachDbFilename=|DataDirectory|\database.mdf;" & _
                                               "Integrated Security=True;User Instance=True")
                Dim sql As New SqlClient.SqlCommand("SELECT * FROM " & typeOfItems & " WHERE " & _
                                                    "itemID = '" & ItemID & "'", con)
                Dim reader As SqlClient.SqlDataReader
                con.Open()
                reader = sql.ExecuteReader
                reader.Read()
                Dim itemName As String = reader.Item("itemName")
                Dim itemPrice As String = reader.Item("ourPrice")
                Dim offer As String
                If reader.Item("offer").ToString = "" Then
                    offer = ""
                Else
                    offer = reader.Item("offer")
                End If
                con.Close()
                '.......................................................

                Dim userID As String = User.Identity.Name
                Dim sCart = New cart
                If sCart.CheckIfItemPresent(userID, ItemID, itemPrice, offer) = True Then
                    Exit Sub
                End If

                Dim buyNo As String = sCart.findLatestBuyNo(userID)
                Session("buyNo") = buyNo

                Session("buyNo") = sCart.AddToCart(ItemID, itemName, itemPrice, offer, buyNo, userID)
            End If
        End If   
    End Sub

i want to replace the asp.net MsgBox with javascript alert...how can i do that? the commented out lines are the once i have already tried out...please help me ...urgent.the asp.net MsgBox does not work on client side after publishing the website.

Monodeep
  • 1,392
  • 1
  • 17
  • 39
  • The `ClientScript.RegisterStartupScript` is the correct way - what happens when you try it? You get error? – Shadow The GPT Wizard May 15 '11 at 11:37
  • When he redirects at the end, the registered client side script won't be executed. – Uwe Keim May 15 '11 at 11:39
  • when i use `ClientScript.RegisterStartupScript` nothing happens..it straight away goes to the login page. – Monodeep May 15 '11 at 11:42
  • 1
    @Monodeep: @Uwe Keim already mentioned: if you `Response.Redirect`to another page, the current page won't be executed. What you could do is: extend the alert with a clientside redirect to your login-form after you've alert the message(`window.location.href="login.aspx"`). – Tim Schmelter May 15 '11 at 11:53
  • where shud i write this code??? – Monodeep May 15 '11 at 11:56
  • @Monodeep: you have to register the javascript via `ClientScript.RegisterStartupScript`. The js should include both, the alert + the redirect to the login(see my last comment). So when the user clicked ok in the alert, he will be redirected to the login. – Tim Schmelter May 15 '11 at 12:01
  • i am very new to asp.net please write me he code..if u dnt mind..and where to use the code??in code behind or aspx?? – Monodeep May 15 '11 at 12:03
  • `type='text/javascript'` not `type="javascript"` – Anyname Donotcare May 15 '11 at 12:27

3 Answers3

3

You are calling Response.Redirect("~/login.aspx", True) at the last line, which means the alert javascript never even gets sent to the client. To allow showing the alert box, you'd also have to do the redirect on the client by setting document.location after the alert, instead of using Response.Redirect.

Like this:

ClientScript.RegisterStartupScript(Me.[GetType](), "myalert", _
    "alert('" + myStringVariable + "');" + _
    "document.location = '" + ResolveUrl("~/login.aspx") + "';", True)

(Not sure if the code will work, I usually code C#)

SirViver
  • 2,411
  • 15
  • 14
1

Don't use Response.Redirect. It prevents the execution of the current page. Use window.location.href in javascript instead. Here's what you'd probably do:

ClientScript.RegisterStartupScript(Me.[GetType](), "myalert",   
"alert('" + myStringVariable + "'); document.location = 'login.aspx';", True)
Kamyar
  • 18,639
  • 9
  • 97
  • 171
0

Try this method:

private void alert(string Msg)
{
    Response.Write("<script type='text/javascript'>window.alert('" + Msg + "')</script>");
}

If you want a cool messages try the link.

Community
  • 1
  • 1
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392