【问题标题】:Need help to bootstrap modal asp.net需要帮助来引导模态 asp.net
【发布时间】:2012-08-21 09:57:41
【问题描述】:

我在 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。我该怎么做?

编辑……编辑……编辑………… ....编辑......编辑......编辑...... .....编辑................ @瑞安

如果我这样做,当会话为 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    

【问题讨论】:

    标签: jquery asp.net twitter-bootstrap modal-dialog


    【解决方案1】:

    将以下代码段添加到您的 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     
    

    【讨论】:

    • 嗨,Ryan,我是新手,我在哪里调用 myModal div !?
    • 在这个例子中:runJQueryCode("$('#myModal').modal('show');")
    • 您的 ASP 代码是什么样的?您可能需要将 添加到您的 ASP 页面以运行 JS。
    • 我谢谢。现在它的工作,我只有一个问题,我如何添加静态背景,runJQueryCode("$('#myModal').modal('show');")
    • 我想我是用 runJQueryCode("$('#myModal').modal({show:true, background:'static', keyboard:false});")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2015-12-24
    • 2014-10-09
    • 2023-03-27
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多