Loading ...

Value of dynamic textbox lost on Postback | CodeAsp.Net

Value of dynamic textbox lost on Postback

0 (0votes)

In this blog I'll explain you how to retain the values of dynamically created textboxes on Postback.

Few days back, I had to create dynamic textboxes on a button click event. There was one more button on that page on which I'd to save the values of the textboxes which I entered in those dynamicaly created textboxes. But on clicking the that button values of dynamic textboxes were becoming blank.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false"  %>

<!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>Untitled Page</title>
<form id="form1" runat="server">

<asp:TextBox ID="txtEnter" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnCreate" runat="server" Text="Add" onclick="btnCreate_Click" />

<asp:Panel ID="pnlInfo" runat="server">
<asp:Button ID="btnLabel" runat="server" Text="Label" Visible="false" onclick="btnLabel_Click" />

Above is the markup of the page. There is 1 textbox where I enter the number of dynamic textboxes I want to generate, 2 buttons and 1 panel.

On the cs side I wrote the code :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)


protected override void OnInit(EventArgs e)
string selectedValue;
if (Request.Form["txtEnter"] != null)
selectedValue = Request.Form["txtEnter"].ToString();

for (int i = 1; i <= Convert.ToInt32(selectedValue); i++)
TextBox txt = new TextBox();
txt.ID = "textBox" + i.ToString();
txt.Text = "";
txt.EnableViewState = false;
btnLabel.Visible = true;

protected void btnCreate_Click(object sender, EventArgs e)


protected void btnLabel_Click(object sender, EventArgs e)
for (int i = 1; i <= Convert.ToInt32(txtEnter.Text); i++)
Label label = new Label();
label.ID = "label" + i.ToString();
TextBox text= (TextBox)pnlInfo.FindControl("textBox" + i.ToString());
label.Text = "Value of textbox" + i.ToString() + " is " + text.Text + " <br/>";


Point to be noted here is that I didn't write the code for dynamic creation of textboxes on btnCreate click event rather I wrote it on OnInit event . Question arises why I did it like this way? 

First problem which I faced was when I create the textboxes on btn create click event and enter the text in textboxes and then click btnLabel event . I had to again create the textboxes on btnLabel click event because the code for generating textboxes will not be called. Second thing was If i again write the code for generation of textboxes then the values of textboxes were getting lost on the btnLabel click event.

So in order to maintain the values of the textboxes I wrote the code in OnInit event.

AS we know the page life cycle events take place in the following order:


Important thing to note here is that I have set EnableViewState of that page to false and still the values of textboxes are maintained on postbacks.

Question comes in mind from where the values are maintained in textboxes?

The answer to the question lies in IPostBackDataHandler. All those controls which inherits from IPostBackDataHandler there
value is reloaded in the textboxes from the LoadPostbackData event.

What is IPostBackDataHandler?

IPostBackDataHandler is an interface which defines methods that ASP.NET server controls must implement to automatically load postback data.

Controls which implements IPostBackDataHandler are :

TextBox, CheckBox, CheckBoxList, DropDownList, RadioButton, RadioButtonList,ListBox,ImageButton, TreeView,
ScriptManager and many other controls .

This is all about retaining values of dynamic controls on postback.

Do let me know you feedback or comments.



Comments (no comments yet)

Top Posts