asp.net 中为什么在showModalDialog弹出的窗体 用html 控件 刷新不了页面。而用服务器端控件 写C#代码可以刷新页面。但是用服务器端刷新页面不好。

asp.net 中为什么在showModalDialog弹出的窗体 用html 控件 刷新不了页面。而用服务器端控件 写C#代码可以刷新页面。但是用服务器端刷新页面不好。 如果我定义要用html控件,js方法怎么实现test2.aspx页面的刷新。

求助给位大侠帮我看看,谢谢。

test1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="WebApplication4.hua" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function onpen() {
            var newurl = 'test2.aspx';
            var hei = 'dialogHeight:500px;dialogWidth:660px';
            window.showModalDialog(newurl, null, hei);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <input type="button" onclick="onpen();" value="弹出"/>
    </div>
    </form>
</body>
</html>

test2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="WebApplication4.tanhua" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <base target="_self"/>//不能去掉
    <title></title>
    <script type="text/javascript">
        function SetVal() {
            //            var num = document.getElementById("txtNum").value;
            //            if (num == "") {
            //                document.getElementById("txtNum").value = 0;
            //            } else {
            //                document.getElementById("txtNum").value = num+1;
            //            }
            var dt = Date();
            document.getElementById("txtNum").value = dt;
        }
        function Refresh() {
            //debugger;
            window.location.reload();
            //window.location.href = window.location.href;
            //document.execCommand("Refresh");
            //window.location.reload(location.href)
        }
    </script>
</head>
<body onload="SetVal()">
    <form id="form1" runat="server">
        <div>
            <input type="text" id="txtNum"  style=" 500px;" /> 
            <input type="button" id="btnRefresh" value="刷新" onclick="Refresh();"  runat="server"/>
            <asp:Button  Text="test" runat="server" ID="test" onclick="test_Click"/>
        </div>
    </form>
</body>
</html>

test2.aspx.cs:

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

namespace WebApplication4
{
    public partial class test2: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //btnRefresh.Attributes.Add("onclick", "Refresh();");
        }

        protected void test_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.ClientScript.GetType(), null, "<script>window.location.reload();</script>");
        }
    }
}

打开test1.aspx 出现:

点击弹出按钮 出现 新的窗体test2.aspx:

点击刷按钮,时间没变,说明没有刷新页面。但是点击test按钮就可以刷新页面。 

如果用html 控件 javascript 方法,或其他方法怎么实现页面的刷新(时间要改变)。

代码:<style type="text/css">
       .btnr{ display:none;}
    </style>

<input type="button" id="Button1" value="刷新2" runat="server" onclick="javascript:document.getElementById('btnR').click();"/>
<asp:Button  Text="1" runat="server" ID="btnR" onclick="btnR_Click" CssClass="btnr"/>

C# :

        protected void btnR_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.ClientScript.GetType(), null, "<script>window.location.reload();</script>");
        }

原文地址:https://www.cnblogs.com/allenhua/p/3013455.html