Solved

Retrieve dynamically created TextBox value

Posted on 2004-08-12
3
406 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now