Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:33:33+00:00 2026-05-28T20:33:33+00:00

Background I have a User Control (an .ascx file) which is being dynamically inserting

  • 0

Background

I have a User Control (an .ascx file) which is being dynamically inserting into an asp:PlaceHolder control on a page. That User Control contains an asp:Repeater, which I’m binding to a DataTable.

Theoretically, on the User Control’s first load the DataTable is initialized and 3 empty rows are added. A button on the User Control adds additional empty rows to the Repeater, one at a time.

Problem

The issue is that after any PostBack event on the page (namely the button in this example being clicked), the DataTable for the Repeater is empty.

User Control (.ascx)
(simplified)

<asp:TextBox ID="controlOutsideRepeater" runat="server" />
<asp:Repeater ID="myRepeater" runat="server">
  <ItemTemplate>
    <p><asp:Textbox ID="firstControlInRepeater" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "A") %>' /></p>
    <p><asp:Textbox ID="secondControlInRepeater" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "B") %>' /></p>
  </ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="addItemButton" runat="server" Text="Add Item" onclick="addNewItem" />

Code Behind (.ascx.cs)
(also simplified)

public DataTable items {
  get {
    object i = ViewState["items"];
    if (i == null) {
      DataTable t = new DataTable();
      t.Columns.Add("A");
      t.Columns.Add("B");
      // add 3 blank items/rows:
      t.Rows.Add(t.NewRow());
      t.Rows.Add(t.NewRow());
      t.Rows.Add(t.NewRow());
      ViewState["items"] = t;
      return t;
    } else {
      return (DataTable)i;
    }
  set { ViewState["items"] = value; }
}

protected void Page_Init(object sender, EventArgs e) {
  myRepeater.DataSource = this.items;
  myRepeater.DataBind();
}

public void addNewItem(object sender, EventArgs e) {
  DataRow r = this.items.NewRow();
  this.items.Rows.Add(r);
  myRepeater.DataBind();
}

Behavior

The first time the UserControl is loaded, the Repeater contains 3 empty items: good! However, after entering some text in the textboxes both inside and outside the repeater and clicking the “Add Item” LinkButton, the page does a refresh/postback and shows 4 empty items, however the textbox -outside- the Repeater retains it’s text. Clicking the “Add Item” LinkButton again also performs a postback and still shows 4 empty items, yet the TextBox outside the Repeater again retains it’s text.

My Crazy Guess

I’ve tried wrapping the Repeater databinding in a (!Page.IsPostBack), but this prevented the Repeater from -ever- being bound, as the UserControl is only programmatically added to the page after a PostBack (a button on the Page adds the UserControl on a click, and then the Page checks each PostBack to see if there should be a user control present and re-adds it to the Page if needed). So I’m guessing there’s a problem with the Page re-creating the User Control on every PostBack, but can’t explain why the TextBox outside the Repeater would retain it’s value, and why the ViewState doesn’t seem to remember my item (on each postback ViewState[“items”] is null and gets re-built within the getter).

HELP!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:33:35+00:00Added an answer on May 28, 2026 at 8:33 pm

    The problem is you are data binding every single request when really you only want to data bind on the first request. Since you don’t data bind on the first page load, you will have to check if you are data bound in a way other than !Page.IsPostBack. You could add a property to your user control to handle this and then check against that every page load / page init.

    Update: With more details from comments

    I see your AddItem() now. I’ve had problems using viewstate this way though I’m not entirely sure why. I’ve had to do it more like the following:

    public void addNewItem(object sender, EventArgs e) {
      DataTable theItems = this.items;
      DataRow r = theItems.NewRow()
      theItems.Rows.Add(r);
      this.items = theItems
      myRepeater.DataBind(); //I'm not sure if this belongs here because of the reasons said before
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a C# user control, which has got it's own background worker
Background: I have a custom user control based around a WPF TreeView. I've been
Background: I work in a suite of ASP.NET applications that have several different modules.
I have a in a user control. In that, I have added a HTML
Background : I have created this UserControl. In the constructor of the user control
I have two controls which are both contained within a user control. Panel location:
I have a very specific problem. I have a user control (code below) That
I Have a Strongly typed user control which i use for Searching a certain
I have a user control which acts as a footer for my website and
I have a web user control where I have ten asp buttons. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.