简体   繁体   English

从条件后面的代码中加载html页面

[英]load html page from code behind on condition

Is possible through the code behind of asp.net to load an html page on condition? 是否可以通过asp.net后面的代码按条件加载html页面?

I am now using this method with javascript in my test.aspx 我现在在test.aspx中将此方法与javascript一起使用

<script type="text/javascript">
$(document).ready(function () {
$('#<%= webform.ClientID %>').load('exemple.html'); })
</script>

<div id="webform"  runat="Server" visible="false"> 
</div>

and in the code behind: 并在后面的代码中:

if (Request.Url.ToString().Contains("https://www.exemple.com/test.aspx?action=webform_RIGHT"))
webform.Visible = true;

Instead of a div you can use an ascx user control. 您可以使用ascx用户控件来代替div。 Then on the page load of the aspx page you can check conditions you want and show or not the ascx control by setting its Visible property. 然后,在aspx页面的页面加载中,可以通过设置其Visible属性来检查所需条件,以及显示或不显示ascx控件。

WebForm1.aspx: WebForm1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="prefix" TagName="Ctrl" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        UC:
        <prefix:Ctrl ID="ctrlTest" runat="server" />
    </div>
    </form>
</body>
</html>

WebForm1.aspx.cs: WebForm1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (something)
            {
                ctrlTest.Visible = true;
            }
            else
            {
                ctrlTest.Visible = false;
            }
        }
    }
}

WebUserControl1.ascx: WebUserControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
User control inside

If you want a popup in a div you can execute tje javascript in the code behind by calling a RegisterStartupScript method. 如果要在div中弹出,则可以通过调用RegisterStartupScript方法在后面的代码中执行javascript。 Then you can check your conditions in C# code and if they're ok you can call the script. 然后,您可以使用C#代码检查条件,如果还可以,则可以调用脚本。

WebForm1.aspx.cs: WebForm1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterStartupScript("test", "<script>alert('test');</script>");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM