Problem getting input value from a textbox in a Gridview Control

I placed a text box in one of my template columns in a gridview control:

                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField><ItemTemplate><asp:TextBox runat=server ID="TextBox" Width="30"></asp:TextBox></ItemTemplate></asp:TemplateField>

Upon opening a page, my gridview is set up to be populated with data  from a datasource, so the number of rows may increase as the database gets larger.  I am trying to allow users to type in a value into the textbox, and this value will be written to a different table in the database.  I tried to use this code technique to retrieve the value typed into the textbox:

Dim dgi As GridViewRow
        Dim ds As DataSet = Session("Dataset1")
        For Each dgi In GridView1.Rows
            Dim score As String = CType(dgi.FindControl("TextBox"), TextBox).Text

However, as I debug after putting phoney value in each text box in the control, and go through each line in the code, the value is not being grabbed.  As it goes through,  the  debugger states that  "Dim score="" " .  I typed in numbers, but the strings are coming up empty.  Can someone help?  Is it my code?  I've been on the internet and from what I read this is the correct syntax, so I don't know why the string is empty. 

5/24/2007 11:02:43 AM 72751 articles. 3 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 2


I think the problem may be with the databind and postback.

Pls check whether the binding is done only if page is not post backed, i.e. not Page.IsPostBack

Try and let me know your response.



Sandeep Kumar

MCPD - Web Developer
MCTS - .NET Framework 2.0: Web Applications
Software Associates

5/25/2007 8:49:23 AM

Here is the complete code. I tried what you suggested, but no go.  However, it did get the value when I binded the gridview using the datasource wizard, but this is impractical.  Not flexible enough.  I need the gridview to bind dynamically.

Take a look at the code:

Dim objConn As OleDbConnection
        Dim cmdSelect As OleDbCommand
        Dim dtrReader As OleDbDataReader
        Dim ds As DataSet
        Dim id As String = "''"
        Dim sqlstr = "SELECT ClassListID FROM Classes WHERE TeacherID=" & id
        Dim connstr = "Provider=Microsoft.Jet.OleDB.4.0; Data Source=d:/Gradebook.mdb"

        objConn = New OleDbConnection(connstr)
        cmdSelect = New OleDbCommand(sqlstr, objConn)
        dtrReader = cmdSelect.ExecuteReader()
        ds = Classlist.GetDataSet(sqlstr, connstr)


        With ddlclass2
            .DataTextField = "ClassListID"
            .DataSource = ds
        End With

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        For Each row As GridViewRow In GridView1.Rows

            Dim score As String = CType(row.FindControl("txtscores"), TextBox).Text

    End Sub


5/26/2007 10:52:39 PM

You should Bind GridView in Not PostBack, Check wheather Bind function is called on Update button click also, if yes remove it, however you can Re-bind after the Update operation is completed

No body is perfect, I'm nobody
5/27/2007 3:58:00 AM

You should Bind GridView in Not PostBack, Check wheather Bind function is called on Update button click also, if yes remove it, however you can Re-bind after the Update operation is completed

No body is perfect, I'm nobody
5/27/2007 4:00:39 AM

 I'm using Visual Studio Pro 2005 My code is in in the last post here you said

You should Bind GridView in Not PostBack, Check wheather Bind function is called on Update button click also, if yes remove it, however you can Re-bind after the Update operation is completed



How do you do that? I am in a similar situation where when I try to use CType(Me.GVToBeProcessed.SelectedRow.FindControl("txtNewStuId"), TextBox).Text to try and get the value I type into my textbox in a gridview What i get returned is the original value of the textbox, not the value I typed in. What you suggested sounds like it would work, but I don't know how to bind the gridview in Not Postback. 

Any help would be greatly appreciated.

thanks Josh 

11/13/2007 12:50:21 AM

Search here for answer.

you can try this way as well

<asp:GridView ID="GrdAxisvalues" runat="server"  OnRowDataBound="Grdaxisvalues_RowDataBound"

 OnDataBound ="Grdaxisvalues_DataBound" DataSourceID="SqlDataSource1">


<asp:TemplateField HeaderText="X-Axis">


<asp:TemplateField HeaderText="Y-Axis">






<asp:PlaceHolder ID="PlaceHolder1" runat="server" OnPreRender="PlaceHolder1_PreRender"></asp:PlaceHolder>




<asp:TemplateField HeaderText="handle" SortExpression="handle">


<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>



<asp:Label ID="Label1" runat="server" Text='<%# Bind("handle") %>'></asp:Label>





Protected Sub GrdAxisvalues_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GrdAxisvalues.DataBound Session.Add("arrcon", arr)

End Sub

Protected Sub PlaceHolder1_PreRender(ByVal sender As Object, ByVal e As EventArgs)

If Page.IsPostBack Then

arr = DirectCast(Session("arrcon"), ArrayList)

If arr IsNot Nothing Then

If TypeOf arr(count) Is TextBox Then

Dim txt1 As TextBox = DirectCast(arr(count), TextBox)

txt1.Text = Request.Form(txt1.UniqueID).ToString()

DirectCast(Me.GrdAxisvalues.Rows(count).Cells(0).FindControl("PlaceHolder1"), PlaceHolder).Controls.Add(txt1)


End If

count += 1

End If

End If

End Sub

Protected Sub Grdaxisvalues_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GrdAxisvalues.RowDataBound Dim plc As PlaceHolder = DirectCast(e.Row.Cells(0).FindControl("PlaceHolder1"), PlaceHolder)

Dim txt As New TextBox()

Dim lbl As Label = DirectCast(e.Row.Cells(1).FindControl("Label1"), Label)

If Not lbl Is Nothing Then



End If

end sub 

Hope this helps.

Mark as answer if you feel that post help you in some way.That will help the user to Read


Mark as Answer if any posts helps you

To Make your dreams come true, Don't Sleep

Remember that no person is an island. Communicate your thoughts and desires honestly, and encourage others to communicate honestly with you. Practice understanding and motivating other people.

11/13/2007 1:12:21 AM

Similar Artilces:

Problem getting an input value from a textbox in a GridView control
I placed a text box in one of my template columns in a gridview control:<Columns>                <asp:CommandField ShowEditButton="True" />                <asp:TemplateField><ItemTemplate><asp:TextBox runat=server ID="TextBox" Width="30"></asp:TextBox></ItemTemplate></asp:TemplateField> </Columns>Upon opening a page, my gridview is set up to be populate...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Getting the control id of a control inside gridview control
 uhmm.. a little bit confusing regarding my subject because i dont know how to say the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??  hoping i could get a reply as soon as possible... Regards,MhaeyPlease remember to click “Mark as Answer” on the post that helps you.. =) What do you want's to do by getting the id of the Text box? Do you want's to find particular Textbox with certain id ? Kamran ShahidSr. Software Engineer(MCP,,MC...

How to add values to template control fields textbox control in GridView..
Hi all,    <asp:GridView ID="GridView1" EnableViewState="true" DataKeyNames="LicenseTemplateID"                       runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"                       Width="345px">                       <Columns>                           <asp:Templa...

Get controlID from control (textbox) inside another control (gridview)
Ok, so I've been trying to get a link within the edit item template of a gridview to open a new window.  I can do that fine, but when I try to pass the new window the controlID of the textbox to pass the value back to in the originating page I can't get the correct ID.  (that was a mouthful)  Maybe my code will explain better...  <EditItemTemplate> <asp:TextBox ID="EndTextBox" runat="server" Text='<%# Bind("endtime") %>'></asp:TextBox> <a href="javascript:;" onclick="

Get value from Gridview into textbox and dropDrownList controls
  Hi to all,I (newbie) want that when user click on ID field of Gridview.The values of that particular ID row should display in the textbox and dropdown list of the page.<asp:Label ID="lblAccessID" runat="server" Text="Access ID" Width="91px" Height="16px"></asp:Label>        <asp:TextBox ID="txtAccessID" runat="server" Height="12px" Width="126px"></asp:TextBox> <br /><br />        &l...

Cannot get value from an input textbox to a listbox web control??
I am about to go nuts because I really have not done any Asp.Net programming up until now. I have mainly done desktop applications. I am trying to take the textbox in the page and pass the contents of it to the listbox on the same page. I cannot get the values from the textbox to the listbox. Below is my code for the aspx page and also for my code behind page. Can someone please tell and show me where I am going wrong? aspx page:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AspNetPolyDraw.Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML...

Issue with getting values from child controls in a gridview, to use for the update using a SQLDataSource control
Hi all, I have a gridview bound with a SQLDataSource. I am using the Update feature of the SQLDataSource to update a SQL Server database with values entered into the gridview. However I am not getting it to work. I believe this is due to the controls that contain the user entries are not the gridview itself, but rather child controls within the gridview. I have been using the names of the actual controls but nothing happens. Upon submit, the screen returns blank, and the database is not updated. Here is some code:   <asp:GridView ID="GridEditSettlement" runat="server" AutoGenerate...

GridView child of GridView
I have a Parent GridView and when you click in the Edit one of the columns has a Template Field which has a Child GridView. When you click Edit on the Child GridView I have a Template Field that has a TextBox. How to I retreive this value of the TextBox? GridViewParent-- GridViewChild (Edit - Template Field containing this GridViewChild)----- TextBox inside Template Field in GridViewChild. Thank you.Peace in ChristMarco Napoli What you can do as a solution is to override the RowDataBound and try to find the child GridView control. Once you get a reference to...

Problem Validating server control textboxes and HTML input txt fields inside a formview control
I have tried everything in the book and have not been able to come up with a solution. Have tried a custom validator calling a javascript funtion, but it doesnt work properly.  Have tried other ways, but it seems that one side works and the other doesn't as expected in everything I have tried. Anyone out there with similar problem that have found a solution.  Is it better to write a javascript to handle all validation rather than using server  side validators with the help of js functions?.  One of the problems is dealing with the insert button inside the formview, ...

Controlling User Input into a TextBox control
I want to control user input to a textbox control with a string mask. e.g. "99.9.99" where the 9s represent any numeric and the dots are automatically provided from the mask. Is it possible in an page - it is standard in dBase type languages! Would it require Javascript? Where could I find help on this? Try using a RegularExpression Validator Control, which is part of the Asp.Net toolbox. Check out for patterns and testing.RegardsAndre Colbiornsen ---------------------------------Seventh DayRåbygatan 1A,SE-223 61 LundSwedenMob.: +46-(0)708-...

Getting value from a textbox control that was filled with a value from javascript
 I have a textbox control (seen below txtMileageDollars1) that gets filled with javascript based on the number of miles a user types into another textbox (txtMileageMiles1) control.  When I post back with the submit button, the value is blank.  I've tried setting EnableViewState to false, but still cannot get a value on postback.  Does anyone have a solution?   Miles<asp:TextBox ID="txtMileageMiles1" runat="server" Width="50px"></asp:TextBox>   Total (txtMileageMiles1 * rate with javascript)<asp:TextBox...

Problem Using Slider Control in Gridview Control
Hello to all,   it is urgent.   i want to use slider control in a Gridview Template Column, if someone can help me how i can get this functionality?...

Control In Control Problem
Hi, guys.I've written two web controls for reservation system.One is a Datepicker which alows user to pick a date (pretty obvious ) and another one is a Reservation which alows user to pick a location (hotel) and number of rooms and number of people in each room.Both of them inherit WebControl and implement ICallbackEventHandler and INamingContainerI bind to them list with available days... List of hotels and regions (if it's to be used for reservation in hotels chain) well... whatever...Both of them work great while I don't try to include the DatePicker in the Reservation... Her...

Web resources about - Problem getting input value from a textbox in a Gridview Control -

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012 - Upcoming stories is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 1/9/2016 7:00:31 AM