0

我在 asp.net 页面后面的代码中有一个 if 语句,如下所示:

If Session("UserActiv") IsNot Nothing Then
    If Session("UserActiv").ToString() = "N" Then
        ClientScript.RegisterStartupScript(Me.GetType(),
            "Details", "LoadDetails();", True)
    End If
Else
    ClientScript.RegisterStartupScript(Me.GetType(),
        "Details", "LoadDetails();", True)
End If

如果我的会话不是什么都没有,如果它是 N,那么它运行函数 LoadDetails() 如果没有,那么它也加载该函数,如果 Y 则什么也不做。

然后我在我的主页上有这个功能,我唯一的问题是,每次我加载页面时它都会加载这个功能,如果会话是 Y,我已经检查了上限 Y/N 和下限 y/n 问题以及所有大写。所以那里没有问题。

我的 loadDetails() 函数是这样的:

<script language="javascript" type="text/javascript">
    function LoadDetails() {
        myModal.load();
    }

    $(document).ready(function () {

        $("#myModal").modal({
            "backdrop": "static",
            // if true, then the backdrop can be closed with a click
            // if false then there is no backdrop.
            "keyboard": false
        })

});
</script>    

我希望它像现在一样在 page_load 加载,但只有当会话什么都不是或者它是 N 时。我该怎么做呢?

编辑……编辑……编辑…… .编辑……编辑……编辑………… ..编辑......@RYAN

如果我这样做,当会话为 N 时不会发生任何事情

Imports System.Web.Security
Imports System.IO
Imports System.Data
Imports System.Data.OleDb

Partial Class _default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Session("UserActiv") IsNot Nothing Then
        If Session("UserActiv").ToString() = "N" Then
            runJQueryCode("$('#myModal').modal('show');")
        End If
    Else
        runJQueryCode("$('#myModal').modal('show');")
    End If

    If (Not Page.IsPostBack) Then

        Dim htmlString As New StringBuilder()
        ' Has the request been authenticated?
        If Request.IsAuthenticated Then
            ' Display generic identity information.
            ' This is always available, regardless of the type of
            ' authentication.
            htmlString.Append("<h3>Generic User Information</h3>")
            htmlString.Append("<b>name: </b>")
            htmlString.Append(User.Identity.Name)
            htmlString.Append("<br><b>Authenticated With: </b>")
            htmlString.Append(User.Identity.AuthenticationType)
            htmlString.Append("<br><b>User ID: </b>")
            htmlString.Append(Session("UserID"))
            htmlString.Append("<br><br>")
            htmlString.Append(Session("UserActiv"))
        End If
        ' Was forms authentication used?

        If TypeOf User.Identity Is FormsIdentity Then
            ' Get the ticket.
            Dim ticket As FormsAuthenticationTicket = (DirectCast(User.Identity, FormsIdentity)).Ticket
            htmlString.Append("<h3>Ticket User Information</h3>")
            htmlString.Append("<b>Name: </b>")
            htmlString.Append(ticket.Name)
            htmlString.Append("<br><b>Issued at: </b>")
            htmlString.Append(ticket.IssueDate)
            htmlString.Append("<br><b>Expires at: </b>")
            htmlString.Append(ticket.Expiration)
            htmlString.Append("<br><b>Cookie version: </b>")
            htmlString.Append(ticket.Version)
            htmlString.Append("<br><b>Cookie CookiePath: </b>")
            htmlString.Append(ticket.CookiePath)
            htmlString.Append("<br><b>Cookie Expired: </b>")
            htmlString.Append(ticket.Expired)
            htmlString.Append("<br><b>Cookie isPersistent: </b>")
            htmlString.Append(ticket.IsPersistent)
            htmlString.Append("<br><b>User Data: </b>")
            htmlString.Append(ticket.UserData)

            ' Display the information.
            LegendInfo.Text = htmlString.ToString()
        End If

        If User.IsInRole("Manager") Then
            ' Display sensitive material
            Session("userrole") = "Site Manager"
        ElseIf User.IsInRole("Admin") Then
            ' Display sensitive material
            Session("userrole") = "Site Admin"
        ElseIf User.IsInRole("User") Then
            ' Display sensitive material
            Session("userrole") = "Alm. Bruger"
        Else
            ' Display only bland material
        End If
    End If
End Sub

Public Function runJQueryCode(ByVal message As String) As Boolean
    Dim requestSM As ScriptManager = ScriptManager.GetCurrent(Page)
    If requestSM IsNot Nothing AndAlso requestSM.IsInAsyncPostBack Then
        ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), Guid.NewGuid().ToString(), getjQueryCode(message), True)
    Else
        Page.ClientScript.RegisterClientScriptBlock(GetType(Page), Guid.NewGuid().ToString(), getjQueryCode(message), True)
    End If

    Return True
End Function

Private Function getjQueryCode(ByVal jsCodetoRun As String) As String
    Dim sb As New StringBuilder()
    sb.AppendLine("$(document).ready(function() {")
    sb.AppendLine(jsCodetoRun)
    sb.AppendLine(" });")

    Return sb.ToString()
End Function

'Private Sub cmdSignOut_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSignOut.ServerClick
'FormsAuthentication.SignOut()
'FormsAuthentication.RedirectToLoginPage()
'End Sub
End Class    
4

1 回答 1

0

将以下代码部分添加到您的 VB.net 代码中:

Public Function runJQueryCode(ByVal message As String) As Boolean
        Dim requestSM As ScriptManager = ScriptManager.GetCurrent(Page)
        If requestSM IsNot Nothing AndAlso requestSM.IsInAsyncPostBack Then
            ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), Guid.NewGuid().ToString(), getjQueryCode(message), True)
        Else
            Page.ClientScript.RegisterClientScriptBlock(GetType(Page), Guid.NewGuid().ToString(), getjQueryCode(message), True)
        End If

        Return True
    End Function

    Private Function getjQueryCode(ByVal jsCodetoRun As String) As String
        Dim sb As New StringBuilder()
        sb.AppendLine("$(document).ready(function() {")
        sb.AppendLine(jsCodetoRun)
        sb.AppendLine(" });")

        Return sb.ToString()
    End Function

然后使用:

If Session("UserActiv") IsNot Nothing Then
        If Session("UserActiv").ToString() = "N" Then
           runJQueryCode("SUCCESS!modaljquerystuff")
        End If
    Else
        runJQueryCode("FAIL!modaljquerystuff")
End If     
于 2012-08-21T12:48:05.503 回答