Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to read Texbox value in a grid

How to read Texbox value in a grid

Scheduled Pinned Locked Moved ASP.NET
helpcsstutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lav naphade
    wrote on last edited by
    #1

    Sir, I m adding texbox in grid at runtime . it added successfully but my problem is that how to read a value in a texbox i m sending my code plz check this code give me reply SqlDataAdaptor da=new SqlDataAdaptor("Select * from mytable",MyConnectionString); DataSet dtSet=new DataSet(); da.Fill(dtSet); GridView1.DataSource=dtSet.Table[0]; GridView1.DataBind(); TextBox txt ; string strID; //Add Textbox in a grid for (int i = 0; i < dtSet.Tables[0].Rows.Count; i++) { for (int j = 0; j < dtSet.Tables[0].Columns.Count; j++) { txt = new TextBox(); strID = i.ToString()+j.ToString(); txt.ID=strID; GridView1.Rows[0].Cells[0].Controls.Add(txt); } } //Read This TextBox Value string strTextID; for (int i = 0; i <GridView1.Rows.Count ; i++) { for(int j=0;j<GridView1.Columns.Count;j++) { strTextID=i.ToString()+j.ToString(); TextBox txt = (TextBox)GridView1.Rows[i].Cells[j].FindControl(strTextID); string strText=Txt.Text;//This Statement Give me error } } plz send me reply Thanks & Regards Lav Naphade

    lav naphade

    M 1 Reply Last reply
    0
    • L lav naphade

      Sir, I m adding texbox in grid at runtime . it added successfully but my problem is that how to read a value in a texbox i m sending my code plz check this code give me reply SqlDataAdaptor da=new SqlDataAdaptor("Select * from mytable",MyConnectionString); DataSet dtSet=new DataSet(); da.Fill(dtSet); GridView1.DataSource=dtSet.Table[0]; GridView1.DataBind(); TextBox txt ; string strID; //Add Textbox in a grid for (int i = 0; i < dtSet.Tables[0].Rows.Count; i++) { for (int j = 0; j < dtSet.Tables[0].Columns.Count; j++) { txt = new TextBox(); strID = i.ToString()+j.ToString(); txt.ID=strID; GridView1.Rows[0].Cells[0].Controls.Add(txt); } } //Read This TextBox Value string strTextID; for (int i = 0; i <GridView1.Rows.Count ; i++) { for(int j=0;j<GridView1.Columns.Count;j++) { strTextID=i.ToString()+j.ToString(); TextBox txt = (TextBox)GridView1.Rows[i].Cells[j].FindControl(strTextID); string strText=Txt.Text;//This Statement Give me error } } plz send me reply Thanks & Regards Lav Naphade

      lav naphade

      M Offline
      M Offline
      Mark J Miller
      wrote on last edited by
      #2

      Why are you adding the text box dynamically instead of declaring the text box in the item template of the control markup? See the reference to TemplateField here in the MS docs: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx[^]

      Mark's blog: developMENTALmadness.blogspot.com

      L 1 Reply Last reply
      0
      • M Mark J Miller

        Why are you adding the text box dynamically instead of declaring the text box in the item template of the control markup? See the reference to TemplateField here in the MS docs: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx[^]

        Mark's blog: developMENTALmadness.blogspot.com

        L Offline
        L Offline
        lav naphade
        wrote on last edited by
        #3

        sir, my problem is my gridview column size is not fix so i can not add column in a template control so adding textbox in a grid at run time . how to add textbox in a ItemTemplate at runtime or template controlt? plz sir give me proper solution .

        lav naphade

        M 1 Reply Last reply
        0
        • L lav naphade

          sir, my problem is my gridview column size is not fix so i can not add column in a template control so adding textbox in a grid at run time . how to add textbox in a ItemTemplate at runtime or template controlt? plz sir give me proper solution .

          lav naphade

          M Offline
          M Offline
          Mark J Miller
          wrote on last edited by
          #4

          To add the control you should be using the RowDataBound event (http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx[^]) like this: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ if(e.Row.RowType == RowType.DataRow){ txt = new TextBox(); strID = "textBox1"; txt.ID=strID; e.Row.Cells(1).Controls.Add(txt) } } Try recursively searching the result of : GridView1.Rows[i].Cells[j]. Instead of calling GridView1.Rows[i].Cells[j].FindControl(strTextId). Do a recursive search of each of the child controls like this: Control ctrl = GridView1.Rows[i].Cells[j]; TextBox txt = FindControlRecursive(ctrl, strTextId) as TextBox; if(txt != null){ //read the value of the text box } Where your recursive function looks like this: private Control FindControlRecursive(Control current, String textId){ Control result = current.FindControl(textId); if(result != null) return result; foreach(Control ctrl in current.Controls){ result = FindControlRecursive(ctrl, textId) if(result != null) return result; } return null; }

          Mark's blog: developMENTALmadness.blogspot.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups