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 9198419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:16:40+00:00 2026-06-17T22:16:40+00:00

To ask my question I have created an aspx file containing a Button and

  • 0

To ask my question I have created an aspx file containing a Button and a DataList with an SqlDataSource:

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

    <asp:DataList ID="DataList1" runat="server" DataKeyField="a" 
    DataSourceID="SqlDataSource1" >
        <ItemTemplate>
            a:
            <asp:Label ID="aLabel" runat="server" Text='<%# Eval("a") %>' />
            <br />
            b:
            <asp:Label ID="bLabel" runat="server" Text='<%# Eval("b") %>' />
            <br />
        </ItemTemplate>
    </asp:DataList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:probaConnectionString %>" 
    SelectCommand="SELECT [a], [b] FROM [PROBA_TABLE]"></asp:SqlDataSource>

In my code behind I add TextBoxes to the Items of the DataList. I add to every Item a TextBox in the Page_Load, and another TextBox in the Button Click eventhandler as well.

    public partial class _Default : System.Web.UI.Page
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                foreach (DataListItem item in DataList1.Items)
                {
                    item.Controls.Add(new TextBox());
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (DataListItem item in DataList1.Items)
            {
                item.Controls.Add(new TextBox());
            }
        }

    }
}

This works fine except one thing. When I click the Button, the TextBoxes which were created in the Page_Load keep their Text value, but the TextBoxes which were created in the Button1_Click lose their Text values. My real problem is more complicated than this, but I think solving this would help me a lot.

My site after postback

  • 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-17T22:16:41+00:00Added an answer on June 17, 2026 at 10:16 pm

    Each control that should receive data from page ViewState should be instantiated in Init or Load event handlers, because ViewState is persisted to controls BEFORE Click, Change and the rest control events (those events are triggered when ViewState changes are detected, so ViewState must be read before Click event is fired).

    So the process should look like:

    1. OnInit (static controls get created)
    2. Static control content is deserialized from ViewState
    3. OnLoad (create dynamic controls, in your case textboxes that you created in last Postback)
    4. Dynamic control content is deserialized from ViewState
    5. Click, Change and other events are fired according to changes detected comparing POST data and ViewState data

    Suggestions:

    You can use hidden fields to save additional status information, and then in OnLoad you can read that info to recreate dynamically created controls.

    Also, you should explicitly set ID property of your textboxes so that values can be properly persisted back, don’t rely on ASP.Net.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen stackoverflow ask question page they disable button until my postback event
[i meant to ask this question on StackOverflow...] I have been using VisualSVN Server
I want to ask very very basic question related to Mondrian. I have created
I have already created a login() . Trying to ask a random question each
I'm sorry to ask this question but I have spent hours trying to understand
guys I have two question to ask, they're easy, but bothering me for a
I have to ask this question because I'm stuck, and I think its because
I have to really ask this question as I donot know Python. Following are
Before I ask the question let me state that I have attempted to google
I would like to ask such question, I have XML xsd`s, which generate beans

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.