首页 > 解决方案 > 在 asp.net 中使用异步加载页面

问题描述

朋友们。

我在 asp.net 中动态构建一个屏幕

我想首先使用异步方法打印加载页面,但是加载微调器(方法)上的屏幕没有改变。是因为没有返回值吗?

如何先绘制加载页面?

供参考。我准备纯 c# 代码。

这是代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        inittask();
        LoadingSpinner();
    }

    public async void inittask()
    {
        await Layout();
    }

    private async Task Layout()
    {   
        await Task.Run(() =>
        {
            HtmlGenericControl div_side = new HtmlGenericControl("div");
            div_side.ID = "div_side";
            div_side.Style.Add(HtmlTextWriterStyle.Width, "15%");
            div_side.Style.Add(HtmlTextWriterStyle.Height, "90%");
            div_side.Style.Add("float", "left");
            div_side.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#f3ba31");

            HtmlGenericControl div_header = new HtmlGenericControl("div");
            div_header.ID = "div_header";
            div_header.Style.Add(HtmlTextWriterStyle.Width, "85%");
            div_header.Style.Add(HtmlTextWriterStyle.Height, "15%");
            div_header.Style.Add("float", "right");
            div_header.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#0094ff");

            HtmlGenericControl div_contents = new HtmlGenericControl("div");
            div_contents.ID = "div_contents";
            div_contents.Style.Add(HtmlTextWriterStyle.Width, "85%");
            div_contents.Style.Add(HtmlTextWriterStyle.Height, "75%");
            div_contents.Style.Add("float", "right");
            div_contents.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#cc653e");

            HtmlGenericControl div_footer = new HtmlGenericControl("div");
            div_footer.ID = "div_footer";
            div_footer.Style.Add(HtmlTextWriterStyle.Width, "100%");
            div_footer.Style.Add(HtmlTextWriterStyle.Height, "10%");
            div_footer.Style.Add("float", "right");
            div_footer.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#808080");

            this.form1.Controls.Add(div_side);
            this.form1.Controls.Add(div_header);
            this.form1.Controls.Add(div_contents);
            this.form1.Controls.Add(div_footer);
        });          
    }

    private void LoadingSpinner()
    {
        //loading
        HtmlGenericControl loadingpanel = new HtmlGenericControl("div");
        loadingpanel.ID = "loadingpanel";
        loadingpanel.Style.Add(HtmlTextWriterStyle.Width, "100%");
        loadingpanel.Style.Add(HtmlTextWriterStyle.Height, "100%");
        loadingpanel.Style.Add(HtmlTextWriterStyle.ZIndex, "1000");
        loadingpanel.Style.Add(HtmlTextWriterStyle.BackgroundColor, "White");


        HtmlGenericControl div_loading = new HtmlGenericControl("div");
        div_loading.ID = "div_loading";
        loadingpanel.Controls.Add(div_loading);

        HtmlGenericControl label_loadingtext = new HtmlGenericControl("label");
        label_loadingtext.ID = "label_loadingtext";
        label_loadingtext.InnerText = "Loding...";
        loadingpanel.Controls.Add(label_loadingtext);

        this.form1.Controls.Add(loadingpanel);
    }

标签: c#asp.netasynchronous

解决方案


HTTP 是一种请求/响应协议。浏览器请求页面;服务器发送页面。浏览器请求页面,服务器发送页面,然后服务器发送替换页面是不可能的。async不会更改 HTTP 协议

如果你想快速显示一个页面然后更新它,那么你需要使用 AJAX 或UpdatePanel.


推荐阅读