?
Solved

Retrieve dynamically created TextBox value

Posted on 2004-08-12
3
Medium Priority
?
433 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 200 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 1600 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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