0

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.

Manicguitarist
  • 329
  • 1
  • 2
  • 11
  • If you run server-code when a button is pressed it means that there already **is** a postback. – Alxandr Jun 09 '11 at 18:08
  • I know, I need to generate *another* postback. Push a button (first postback) and the page adds another control - but this won't appear till another postback.... – Manicguitarist Jun 09 '11 at 19:40
  • Why can't just the page be updated with the first callback? – Alxandr Jun 09 '11 at 20:16
  • It is updated on the first call back - however, I'll go into more detail. In the page load function, the controls are populated according to the database. So, on the button push, the controls are populated (as is required) and then the database updated. I can't call the populate button again cos that duplicates the control IDs unless I clear the existing controls off the screen - and then I lose the values. But on the next postback, the database is upto date and the page populates itself. I'll try and create a simple example to post. – Manicguitarist Jun 09 '11 at 22:34
  • How 'bout you try to make the button1_click/page_load (whichever runs last, guessing button1_click) a little smarter? If it's button1_Click you can just create controls for the new items (in your example add 1 more control), if it's the other one, I don't really see the problem. – Alxandr Jun 09 '11 at 23:20

2 Answers2

0

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;
    }
}
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
-1

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.

Community
  • 1
  • 1
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42
  • This doesn't actually seem relevant to the question. All the controls have unique IDs and I am having no problem finding controls' values when I need to - even though they are all dynamically generated. – Manicguitarist Jun 09 '11 at 19:41