简体   繁体   中英

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.

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.

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.

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.

Searching for controls in Request.Form / Parsing a NameValueCollection

Give your controls unique IDs' and you can search for them on the postback.

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. Use regex to search the returned strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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