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

  • Home
  • SEARCH
  • 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 9176483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:07:03+00:00 2026-06-17T17:07:03+00:00

It has been so long since I’ve used Web Forms I find myself not

  • 0

It has been so long since I’ve used Web Forms I find myself not remembering most of the perks.

I have a user control that has a button, a repeater and the ItemTemplate property of the repeater is another user control.

<asp:Button runat="server" ID="btnAdd" CssClass="btn" Text="Add" OnClick="btnAdd_Click"/>
<br/>
<asp:Repeater runat="server" ID="rptrRequests">
    <ItemTemplate>
        <uc1:ucRequest ID="ucNewRequest" runat="server" />
    </ItemTemplate>
</asp:Repeater>

The idea is that when the user clicks on the Add button a new instance of the ucRequest is added to the collection. The code behind is as follows:

public partial class ucRequests : UserControl
{
    public List<ucRequest> requests
    {
        get
        {
            return (from RepeaterItem item in rptrRequests.Items 
                    select (ucRequest) (item.Controls[1])
                    ).ToList();
        }
        set
        {
            rptrRequests.DataSource = value;
            rptrRequests.DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;

        requests = new List<ucRequest>();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        var reqs = requests;
        reqs.Add(new ucRequest());
        requests = reqs;
    }
}

After much googling I now remember that I should be binding the Repeater in the OnInit method in order for the ViewState to put the captured data of the controls within the ucRequest control on them between post backs but when I try to do that I will always have a single instance of the control on the Repeater since its Items collection is always empty.

How could I manage to do this?

Thanks in advance.

  • 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-06-17T17:07:04+00:00Added an answer on June 17, 2026 at 5:07 pm

    You just need control ids in view state stead of entire control collection.

    enter image description here

    <%@ Control Language="C#" AutoEventWireup="true" 
        CodeBehind="ucRequests.ascx.cs"
        Inherits="RepeaterWebApplication.ucRequests" %>
    <asp:Button runat="server" ID="btnAdd" CssClass="btn" Text="Add" 
       OnClick="btnAdd_Click" />
    <br /><asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
    
    <%@ Control Language="C#" AutoEventWireup="true" 
       CodeBehind="ucRequest.ascx.cs" 
       Inherits="RepeaterWebApplication.ucRequest" %>
    <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
    
    private List<int> _controlIds;
    
    private List<int> ControlIds
    {
        get
        {
            if (_controlIds == null)
            {
                if (ViewState["ControlIds"] != null)
                    _controlIds = (List<int>) ViewState["ControlIds"];
                else
                    _controlIds = new List<int>();
            }
            return _controlIds;
        }
        set { ViewState["ControlIds"] = value; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            foreach (int id in ControlIds)
            {
                Control ctrl = Page.LoadControl("ucRequest.ascx");
                ctrl.ID = id.ToString();
    
                PlaceHolder1.Controls.Add(ctrl);
            }
        }
    }
    
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        var reqs = ControlIds;
        int id = ControlIds.Count + 1;
    
        reqs.Add(id);
        ControlIds = reqs;
    
        Control ctrl = Page.LoadControl("ucRequest.ascx");
        ctrl.ID = id.ToString();
    
        PlaceHolder1.Controls.Add(ctrl);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It has been a long time since I used java and I have run
It has been long time since we have been validating our forms using JavaScript.
It has been a long time since I have developed content in Facebook, so
It has been a really long time since I used flash and I am
It has been a long time since I have written XSL, I have this
It has been a long while since I have really worked with J2EE so
It has been way to long since I have been involved in VB so
It has been so long since I have coded a page in VB. For
It has been a long time since I have programmed in C++, but I
It has been a long time since I've used C++. I want to start

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.