Solved

Retrieve dynamically created TextBox value

Posted on 2004-08-12
3
419 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
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

Industry Leaders: 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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

762 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