Problem getting input value from a textbox in a Gridview Control

Last post 11-12-2007 8:12 PM by Mc.Gem. 6 replies.

Sort Posts:

  • Problem getting input value from a textbox in a Gridview Control

    05-24-2007, 7:02 AM
    • Loading...
    • Juwar
    • Joined on 12-05-2005, 10:27 AM
    • Posts 73
    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 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
                ListBox1.Items.Add(score)
            Next

    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. 

    Anyone?
  • Re: Problem getting input value from a textbox in a Gridview Control

    05-25-2007, 4:49 AM
    • Loading...
    • ASP.NET Dev
    • Joined on 12-09-2006, 7:20 AM
    • Kozhikode, Kerala, India
    • Posts 259

    Hi,

    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.

    Regards,

    Dev

    Regards,
    Sandeep Kumar
    MCTS - .NET Framework 2.0 (Web Applications)
    --------------------------------------------------
  • Re: Problem getting input value from a textbox in a Gridview Control

    05-26-2007, 6:52 PM
    • Loading...
    • Juwar
    • Joined on 12-05-2005, 10:27 AM
    • Posts 73

    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 = "'xxxxx@aol.com'"
            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)
            objConn.Open()
            cmdSelect = New OleDbCommand(sqlstr, objConn)
            dtrReader = cmdSelect.ExecuteReader()
            ds = Classlist.GetDataSet(sqlstr, connstr)

            dtrReader.Close()
            objConn.Close()

            With ddlclass2
                .DataTextField = "ClassListID"
                .DataSource = ds
                .DataBind()
            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

            Next
        End Sub

     

  • Re: Problem getting input value from a textbox in a Gridview Control

    05-26-2007, 11:58 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
  • Re: Problem getting input value from a textbox in a Gridview Control

    05-27-2007, 12: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
  • Re: Problem getting input value from a textbox in a Gridview Control

    11-12-2007, 7:50 PM
    • Loading...
    • Carehalf
    • Joined on 06-18-2007, 5:58 PM
    • Posts 46

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

    saifi_hasan@yahoo.co.in:

    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 

  • Re: Problem getting input value from a textbox in a Gridview Control

    11-12-2007, 8:12 PM
    • Loading...
    • Mc.Gem
    • Joined on 11-11-2007, 11:15 PM
    • Posts 102

    Search here for answer.

    http://forums.asp.net/t/1177443.aspx

    you can try this way as well

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

     OnDataBound ="Grdaxisvalues_DataBound" DataSourceID="SqlDataSource1">

    <Columns>

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

    </asp:TemplateField>

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

    <ItemTemplate>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField>

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

     

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

    <EditItemTemplate>

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

    </EditItemTemplate>

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

    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)

    ListBox1.Items.Add(txt1.Text)

    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

    plc.Controls.Add(txt)

    arr.Add(txt)

    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.

    Cheers,
    RR
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter