C# .NET - How can i get Values from all TextBoxes

Asked By balaji mogadali
11-May-11 08:45 AM
 Hi frds
 how can i get  values from dynamic created textboxes from a panel

 pls Help
  dipa ahuja replied to balaji mogadali
11-May-11 08:51 AM
suppose textbox id is txt1:

protected void Button2_Click(object sender, EventArgs e)
  {
    //from page
    TextBox tx = (TextBox)Page.FindControl("txt1");
    Response.Write(tx.Text.ToString());
 
    //from panel
    TextBox txt = (TextBox)Panel1.FindControl("txt1");
    Response.Write(txt.Text.ToString());
  }
  Ravi S replied to balaji mogadali
11-May-11 08:58 AM
HI

If you are using dynamically created controls, you have to create them in Page_PreInit for the page to properly process the events and viewstate. This will help you to write code like this for getting textbox values:

TextBox tb = FindControl(BaseChildSectionName + i.ToString()) As TextBox;
xtw.WriteString(tb.Text);

If you are not interested in recreating your textboxes, then you will have to get the textbox value directly from the Form parameters like this:

xtw.WriteString(Request.Form[BaseChildSectionName + i.ToString()]);


(or)

try this

protected void btnDoStuff_click(Object sender, EventArgs args)
{
   
TextBox txtBox = (TextBox) pnlDynamicButtons.FindControl("txtBox1");
}
  Reena Jain replied to balaji mogadali
11-May-11 09:24 AM
hi,

Like this you can get the data from dynamic generated text box

protected void Page_Load(object sender, EventArgs e) 
   
     if (IsPostBack) 
     
       if (TextBox1.Text != string.Empty) 
       
         int count = 0; 
         int.TryParse(TextBox1.Text, out count); 
          
         for (int i = 0; i < count; i++) 
         
           TextBox txt = new TextBox(); 
           txt.ID = "dynamicText" + i.ToString(); 
           if (PlaceHolder1.FindControl(txt.ID) != null
           
             PlaceHolder1.Controls.Add(txt); 
           
         
       
     
      
   
   protected void Button1_Click(object sender, EventArgs e) 
   
     if (TextBox1.Text != string.Empty) 
     
       int count=0; 
       int.TryParse(TextBox1.Text,out count); 
       for (int i = 0; i < count; i++) 
       
         TextBox txt = new TextBox(); 
         txt.ID = "dynamicText" + i.ToString(); 
         PlaceHolder1.Controls.Add(txt); 
       
     
   }

Hope this will help you
Create New Account
help
validator code behind protected void lnkInsert_Click(object sender, EventArgs e) { TextBox name = (TextBox)TextBox.FindControl("name").ToString(); . . . . is it correct . . ??if not . . .make it correction protected void lnkInsert_Click(object sender, EventArgs e) { TextBox name = (TextBox)Page.FindControl("name"); or you can directly also access the text box like string strName = name
Hi I want to get server side control in static web methods in c# using page.findcontrol("textbox") Page page = (Page)HttpContext.Current.Handler; Panel ltr = (Panel)page.FindControl("panelID"); but it return null in ltr object. . . . how can i access server side public static string Result( string controlName) { return RenderControl(controlName); } public static string RenderControl( string controlName) { Page page = new Page (); UserControl userControl = ( UserControl )page.LoadControl(controlName); userControl.EnableViewState = false ; HtmlForm form = new HtmlForm
findcontrol in my page p1 = planetlist.SelectedValue.ToString(); foreach (Control page_control in Page.Controls) { foreach (Control c in page_control .Controls) { if (c is TextBox) { if (c = = (TextBox)Page.FindControl("txt1")) { ((TextBox)c).Text = p1.ToString(); } else if (c = = (TextBox)Page.FindControl("TextBox1")) { ((TextBox)c).Text = p1.ToString(); } else if (c = = (TextBox)Page.FindControl("TextBox2")) { ((TextBox
regarding ((TextBox)LoginView1.FindControl("UserName")).Text Can I do string A = (( TextBox )LoginView1.FindControl( "UserName" )).Text ? So, it depends on where you call this line of code. TextBox textBox = (TextBox)LoginView1.FindControl( "UserName" ); if (textBox ! = null ) { string A = textBox.Text; } string A = ""; system.web.UI.webcontrol myControl = loginview1.findcontrol("username"); if ( (myControl ! = null) && (myControl GetType() = = TypeOf(TextBox)) ) a = mycontrol.text; I am still getting same error: Message: Exception of type 'System.Web
i m adding rows dynamically on add button click using javascript like: name age salary textbox textbox textbox(on add button click) textbox textbox textbox(on add button click) textbox textbox textbox(on add button click) there is also a delete button for each row to