Connecting Tech Pros Worldwide Help | Site Map

access dynamic textbox

Newbie
 
Join Date: Jan 2009
Posts: 11
#1: Jan 27 '09
How to access the value of the dynamic textbox by its id?
I created more dynamic textbox using the below in loop
Dim tb As New TextBox
tb.ID = "txt_" + Str$(i)
and it assigned unique id.
then how to access each textbox value by its id without using javascript.
Newbie
 
Join Date: Oct 2008
Location: Chennai, India.
Posts: 3
#2: Jan 27 '09

re: access dynamic textbox


Try this
Expand|Select|Wrap|Line Numbers
  1. for(i=0; i<= 10; i++)
  2. {
  3. var textBox = document.getElementById( "txt_"  + i) 
  4. ...
  5. }
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 699
#3: Jan 27 '09

re: access dynamic textbox


Quote:

Originally Posted by marisenthil View Post

How to access the value of the dynamic textbox by its id?
I created more dynamic textbox using the below in loop
Dim tb As New TextBox
tb.ID = "txt_" + Str$(i)
and it assigned unique id.
then how to access each textbox value by its id without using javascript.

Try this
Expand|Select|Wrap|Line Numbers
  1. int i = 1;
  2.         foreach (Control c in form1.Controls)
  3.         {
  4.             if (c is Button)
  5.             {
  6.                 if (c.ID == "Button"+i.ToString())
  7.                 {
  8.                     ((Button)(c)).Text = "Something";
  9.                 }
  10.             }
  11.         }
  12.  
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#4: Jan 27 '09

re: access dynamic textbox


Also, check out this article on How to use dynamic controls in asp.net.

-Frinny
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 699
#5: Jan 28 '09

re: access dynamic textbox


Instead of looping through you could also use FindControl...
Expand|Select|Wrap|Line Numbers
  1. Control c1 = null;
  2.  
  3. c1=FindControl("Button1"); // Here you pass Id of control you are trying to find ..
  4.         if (c1 != null)
  5.         {
  6.             if (c1 is Button)
  7.             {
  8.                 ((Button)(c1)).Text = "Something";
  9.             }
  10.         }
  11.  
Reply