Solved

Retrieve dynamically created TextBox value

Posted on 2004-08-12
3
423 Views
Last Modified: 2010-04-07
Hi Experts,

I have created  a table dynamically with 1 textbox and 1 button:
private dynamicTable()
{
Table Mytable=new Table();
TableRow row1=new TableRow();
TableCell cell1=new TableCell();

TextBox mytextbox=new TextBox();
mytextbox.ID="mybox";
LinkButton mybutton=new LinkButton();
mybutton.ID="mybutton";
mybutton.Click+=new EventHandler(mybutton_Click);

cell1.Controls.Add(mytextbox);
cell1.Controls.Add(mybutton);
row1.Cells.Add(cell1);
Mytable.Rows.Add(row1);

PlaceHolder1.Controls.Add(MyTable);
}

private void mybutton_Click(object sender, EventArgs e)
        {
            Table Mytable=(Table)((LinkButton)sender).Parent;
            Response.Write(((TextBox)Mytable.FindControl("mybox")).Text);
        }

The problem is when I click the button, I can't retrieve my textbox value.
Where did I make a mistake ?

Thanks for your answers.
0
Comment
Question by:noulouk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Assisted Solution

by:s_sansanwal
s_sansanwal earned 50 total points
ID: 11790449
you need to access the value using Request.Form as for dynamic controls .NET don't have any state value

So, change your code
as
private void mybutton_Click(object sender, EventArgs e)
        {

string textValue = Request.Form["mybox"].ToString() ;
}

Cheers
S Sansanwal
0
 
LVL 21

Accepted Solution

by:
tovvenki earned 400 total points
ID: 11790530
Hi,
Since you are adding controls dynamically to a page and wants to get their information after PostBack you need to recreate these elements after the PostBack.
have alook at this article that demonstrates the technique
http://weblogs.asp.net/ksamaschke/archive/2003/04/27/6098.aspx
this article demonstrate how to Save and Get values of Dynamically created controls from ViewState
http://www.codeproject.com/Purgatory/ViewState___Dynamic_Cntrl.asp
Hope that this helps you

regards,
venki
0
 
LVL 8

Assisted Solution

by:boulder_bum
boulder_bum earned 50 total points
ID: 11794132
In other words, you simply need to do this:

private void Page_Load(object sender, System.EventArgs e)
{
      //always call dynamicTable() on every load
      this.dynamicTable();
}

if you give mytextbox class scope, you can also do this:

private void mybutton_Click(object sender, EventArgs e)
{
    Response.Write(mytextbox.Text);
}
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question