【问题标题】:Populate bootstrapdialog text from codebehind从代码隐藏填充引导对话框文本
【发布时间】:2017-09-28 07:24:03
【问题描述】:

在代码隐藏中为我的弹出窗口设置文本时遇到问题,它将刷新页面,这意味着弹出窗口消失了。

我尝试按照示例/教程进行操作,但似乎无法正常工作。我试过移动更新面板,并在按钮上使用 autopostback = false 。我确定这很愚蠢,但我就是看不到它..

这是我的 aspx 页面;

<%@ Page Language="vb" AutoEventWireup="false" 
CodeBehind="ShowDialog.aspx.vb" Inherits="BootStrapDialog.WebForm1" 
MaintainScrollPositionOnPostBack="true" %>

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"
    rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div class="container">
    <div class="row">
        <h1>
            Hello, world! Hello, world!</h1>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

        <asp:Button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" Text="Launch demo modal" OnClick="GenerateModalText" runat="server">

        </asp:Button>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <!-- Modal -->
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                    aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="myModalLabel">
                                    Modal title</h4>
                            </div>
                            <div class="modal-body">
                                <asp:TextBox ID="TextBox1" runat="server" class="form-control"></asp:TextBox><br />
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                    Close</button>
                                <%--<button type="button"  class="btn btn-primary">
                                    Save changes</button>--%>
                                <asp:Button Text="Save" OnClick="Submit" runat="server" />
                            </div>
                        </div>
                    </div>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</div>
</form>

这是我的 VB.NET 代码隐藏;

Public Class WebForm1
Inherits System.Web.UI.Page

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

Protected Sub Submit()

End Sub

Protected Sub GenerateModalText()
    Dim txt As String = "This is sample text genereated from codebehind"
    TextBox1.Text = txt
End Sub



End Class

任何帮助将不胜感激

【问题讨论】:

  • 刷新页面的那部分后显示模式的代码在哪里?默认情况下不显示模式,因此如果页面明显刷新,它将被隐藏。老实说,这将是一个可怕的用户体验。你可能会闪烁。出于好奇,您为什么使用 webforms 和 vb.net?有更好的成熟的东西可以使用。
  • @TheMuffinMan 这是他们在我开始工作的地方使用的东西,我知道有更好的但必须使用他们都知道的东西。为了获得更清晰的用户体验,您会推荐什么?
  • 不知道能不能把更新面板放到modal里面,这样里面就刷新而不是整个容器了。
  • @TheMuffinMan 尝试将其放入模态内容中,结果相同

标签: asp.net vb.net code-behind


【解决方案1】:

根据您是否动态绑定模态,您有两种解决方案之一。

首先你可以注册一个启动脚本,以便在页面重新加载时打开模式。

                Page.ClientScript.RegisterStartupScript(GetType(), "UpdateModal", string.Format("OpenModal(\"{0}\", \"{1}\");", "modal-blank", "static"), true);

我有一些框架可以打开我的模式,但是用引导模式打开代码替换 javascript 函数,它应该可以工作。

其次,您可以在 Page_Load 事件期间重新绑定模式。

基本上,一旦你按下一个按钮,整个页面就会在触发按钮事件之前再次经历它的生命周期。如果你不绑定modal页面不知道它存在来设置文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-09
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    相关资源
    最近更新 更多