简体   繁体   English

强制从服务器回发 asp.net 页面

[英]Force asp.net page postback from server

I have a page that is created entirely dynamically in the code behind (oh joy).我有一个在后面的代码中完全动态创建的页面(哦,快乐)。

On pressing a button on this page, more elements are added to the page - which is fine, however, the refreshed page doesn't appear until the next postback.在此页面上按下按钮时,会向页面添加更多元素 - 这很好,但是,刷新的页面直到下一次回发才会出现。 I would do a redirect, but I want to keep the state of any data entered in the existing controls.我会做一个重定向,但我想保留在现有控件中输入的任何数据的 state。

So - I need to force a postback from the server code behind code.所以 - 我需要从代码后面的服务器代码强制回发。 I imagine that this involves inserting some js code - but beyond that, I've not had any joy at all.我想这涉及到插入一些 js 代码——但除此之外,我一点也不开心。

Here is an example to make things clearer:这是一个使事情更清楚的示例:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  
    </div>
    </form>
</body>
</html>

A very simple page.一个非常简单的页面。

The code behind is simple too:后面的代码也很简单:

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

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["data"] = 1;
        }

        PopulatePage();
    }

    private void PopulatePage()
    {
        int data = (int)Session["data"];

        for (int n = 0; n < data; ++n)
        {
            TextBox tb = new TextBox();
            tb.ID = n.ToString();
            PlaceHolder1.Controls.Add(tb);
            Literal lit = new Literal();
            lit.Text = "<br/>";
            PlaceHolder1.Controls.Add(lit);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int data = (int)Session["data"];



Session["data"] = ++data;


   }
}

You click the button and more controls are added.您单击按钮并添加更多控件。 This is way simpler than the page that I am having issues with, but it demonstrates the problems that I am having.这比我遇到问题的页面要简单得多,但它说明了我遇到的问题。

The postback is not needed and is a bit of a hack , you would be better off fixing the problem at the root.回发不是必需的,而且有点hack ,最好从根本上解决问题。

private void PopulatePage()
{
    for (int n = 0; n < Counter; n++)
    {
        CreateSingleControl(n);
    } 
}

private void CreateSingleControl(int index)
{
    TextBox tb = new TextBox();
    tb.ID = String.Format("text{0}", index);
    PlaceHolder1.Controls.Add(tb);
    Literal lit = new Literal();
    lit.Text = "<br/>";
    PlaceHolder1.Controls.Add(lit);

}

protected override void CreateChildControls()
{
    PopulatePage();
    base.CreateChildControls();
}
protected void Button1_Click(object sender, EventArgs e) {
    CreateSingleControl(Counter);
    Counter++;

}

private int Counter
{
    get
    {
        return Counter = (int)(ViewState["data"] ?? 1);
    }
    set
    {
        ViewState["data"] = value;
    }
}

Use the Request forms list to search unique control ID's Something like this link below, last post, not sure his code is exactly correct, but you'll get the idea.使用请求 forms 列表来搜索唯一控件 ID 类似下面这个链接的东西,最后一个帖子,不确定他的代码是否完全正确,但你会明白的。

Searching for controls in Request.Form / Parsing a NameValueCollection在 Request.Form 中搜索控件/解析 NameValueCollection

Give your controls unique IDs' and you can search for them on the postback.为您的控件提供唯一 ID,您可以在回发时搜索它们。

Plus, i use to add other things to the ID to let the postback searching know what type of control it was, like adding txt_firstname, i know that firstname was a txtbox.另外,我使用在 ID 中添加其他内容以让回发搜索知道它是什么类型的控件,例如添加 txt_firstname,我知道 firstname 是一个 txtbox。 Use regex to search the returned strings.使用正则表达式搜索返回的字符串。

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

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