开发者

MessagBox on JavaScript

开发者 https://www.devze.com 2023-03-06 05:33 出处:网络
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
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....
            Di开发者_运维百科m 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.


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#)


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号