Solved

Retrieve dynamically created TextBox value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

19 Experts available now in Live!

Get 1:1 Help Now