根据条件从代码隐藏加载html页面

本文关键字:加载 html 页面 隐藏 代码 条件 | 更新日期: 2023-09-27 18:29:23

是否可以通过asp.net的代码加载html页面?

我现在在test.aspx 中使用此方法和javascript

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

在后面的代码中:

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

根据条件从代码隐藏加载html页面

您可以使用ascx用户控件来代替div。然后,在aspx页面的页面加载上,您可以检查所需的条件,并通过设置其Visible属性来显示或不显示ascx控件。

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:

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:

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

如果你想在div中弹出,你可以通过调用RegisterStartupScript方法在后面的代码中执行tje javascript。然后你可以在C#代码中检查你的条件,如果它们是可以的,你可以调用脚本。

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>");
        }
    }
}