Retrieving value from dynamically added textbox in GridView


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Retrieving value from dynamically added textbox in GridView

  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Exclamation Retrieving value from dynamically added textbox in GridView

    Hello all

    I am hoping someone can help me out with this problem.

    I have a gridview control, I have 1 column where I add textboxes where the user can enter data (not the entire column, just a few selected cells). I do this using this code in the RowDataBound event:

    If e.Row.RowType = DataControlRowType.DataRow Then
    If e.Row.Cells(4).Text = "xxx" Then
    ' this indicates where a textbox should be placed to allow the user to enter an inv or check #
    Dim txtbx As New TextBox
    txtbx.Visible = True
    txtbx.ID = "txtRow" & e.Row.RowIndex.ToString
    txtbx.Width = Unit.Pixel(75)
    txtbx.BackColor = Color.LightYellow
    e.Row.Cells(4).Controls.Add(txtbx)
    txtbx = Nothing
    End If

    The textboxes are added and I am able to interact with them just fine.

    My problem is when I attempt to read the value of the textboxes, I can never find them in the gridview.

    I have tried using .. something like
    dim txtbox as textbox
    for x=0 to gridview.rows.count - 1
    txtbox=gridview.rows(x).findcontrol("txtBox" & x.tostring)
    next

    also tried..
    dim txtbox as textbox
    for x=0 to gridview.rows.count - 1
    txtbox=gridview.rows(x).cells(0).controls.item(0)
    next

    also used TryCast which always returns nothing as well.

    I also just tried using the full name that the gridview gave the textbox, by looking at the source in the browser I saw it had "gridview_ctl3_txtRow1"
    So I tried to loop ALL controls in the ME object to find that textbox, again it was not found.

    Is it unaccessible because I added it dynamically?

    Here is what a record that has been given a textbox looks like in the rendered HTML (via view source)

    <td>&nbsp;</td><td>(none selected)</td><td>&nbsp;</td><td align="right">$1,334.95</td><td align="center"><input name="grdExpList:_ctl3:txtRow1" type="text" id="grdExpList__ctl3_txtRow1" style="background-color:LightYellow;width:75px;" /></td>

    The control is there, I just can't get to it.

    This is a VS2008 asp.net VB project.

    Anyone have any ideas? I figure I am missing something simple as most obvious solutions are come to last.

    Thanks for any assistance you may lend.
    Jon

  2. #2
    Join Date
    Sep 2009
    Posts
    3

    Talking gridview name

    I know the seudo code I typed and the viewed source show different gridview names, I assure you they match in the code. Just to make sure that is clear.

    Thanks

  3. #3
    Join Date
    Sep 2009
    Posts
    3

    Smile of course...

    Well I found the solution, the dynamically added textbox doesn't exist really. Only on the client.

    So I created a column of textboxes (templatefield) .. then hide the textboxes on the rows where the user doesn't have to enter any values ...

    the code finds them just fine using the .FindControl method.

    So often, just explaining the issue to someone (or a group of virtual someones) is enough to flick a switch. Even in the denses of darkness. LOL

    Thanks for listening.

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    And thank you for coming back and posting your solution.

    It could help someone else with the same or similiar problem.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. ASP.NET 2 Textbox value in Gridview row
    By timmym in forum ASP.NET
    Replies: 8
    Last Post: 11-23-2008, 08:31 AM
  2. dynamically added textbox
    By ssss in forum ASP.NET
    Replies: 1
    Last Post: 05-26-2008, 09:22 AM
  3. Replies: 1
    Last Post: 02-28-2007, 07:50 PM
  4. Replies: 4
    Last Post: 09-02-2006, 10:18 AM
  5. DataGrid Header in Dynamically Added Template Columns
    By David Hirschfeld in forum ASP.NET
    Replies: 0
    Last Post: 01-05-2003, 12:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center