【问题标题】:MessagBox on JavaScriptJavaScript 中的消息框
【发布时间】:2011-05-15 11:35:05
【问题描述】:
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

我想用 javascript alert 替换 asp.net MsgBox...我该怎么做?注释掉的行是我已经尝试过的...请帮助我...紧急。发布网站后,asp.net MsgBox 在客户端不起作用。

【问题讨论】:

  • ClientScript.RegisterStartupScript 是正确的方法 - 当你尝试它时会发生什么?你有错误吗?
  • 当他最后重定向时,注册的客户端脚本不会被执行。
  • 当我使用ClientScript.RegisterStartupScript 时没有任何反应..它立即进入登录页面。
  • @Monodeep: @Uwe Keim 已经提到:如果你Response.Redirect到另一个页面,当前页面将不会被执行。您可以做的是:在您提醒消息后,通过客户端重定向到您的登录表单来扩展提醒(window.location.href="login.aspx")。
  • 我在哪里写这段代码???

标签: javascript .net asp.net vb.net messagebox


【解决方案1】:

您在最后一行调用Response.Redirect("~/login.aspx", True),这意味着警告javascript 甚至永远不会发送到客户端。要允许显示警报框,您还必须通过在警报后设置 document.location 而不是使用 Response.Redirect 在客户端上进行重定向。

像这样:

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

(不确定代码是否可行,我通常编写 C#)

【讨论】:

    【解决方案2】:

    不要使用Response.Redirect。它阻止执行当前页面。在 javascript 中使用 window.location.href 代替。以下是你可能会做的事情:

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

    【讨论】:

      【解决方案3】:

      试试这个方法:

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

      如果您想要很酷的消息,请尝试link

      【讨论】:

      • 您应该使用type="javascript" 而不是已弃用的language 属性。
      • 糟糕,应该是type="text/javascript",而不是type="javascript"
      • 您是否尝试过之前的方法,因为我尝试过并且有效?
      • 是的,我试过了,但它对我不起作用……我已经发布了上面的代码。
      • 你写了 type="javascript" 请写type='text/javascript'
      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多