How to get textbox values from a DataGrid Template Column

Posted on 2005-04-07
Last Modified: 2010-04-07
Hello everyone.

Instead of having the Datagrid edit a record one row at a time, i want it to edit one column at a time.

So the way it works is I have a user choose a column from a dropdown list, and his/her choice will show the column to edit.

Now what I have is an asp:button which the user clicks when he/she is done updating the column.

My question is how can I loop through the column's TextBox controls and get the changed values?

I have been trying
Dim ds As Dataset
ds = CType(DataGrid1.DataSource, DataSet)

in the button click event, but ds is still null.


Code (I do have lots of template columns but i only showed one):
---------------START index.aspx-------------------------------------------------------------
<asp:button id="btnUpdateDb" onclick="btnUpdateDb_Click" runat="server" text="Update Column"></asp:button>
<asp:datagrid id="DataGrid1" runat="server" autogeneratecolumns="false" showheader="False">
                  <asp:boundcolumn headertext="Col Header1" datafield="field1"></asp:boundcolumn>
                  <asp:boundcolumn headertext="Col Header 2" datafield="field2" itemstyle-width="30"></asp:boundcolumn>
                  <asp:templatecolumn headertext="Month" visible="False">
                              <asp:textbox runat="server" id="txtMonth" columns="10" text='<%#  DataBinder.Eval( Container.DataItem, "monthField" ) %>'>
-----------------------------END index.aspx-----------------------------------

--------------------------START index.aspx.vb----------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     'Grab Data from DB
     'Fill DataSet and Bind it
End Sub

Public Sub btnUpdateDb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
     'Dim ds As Dataset
     'ds = CType(DataGrid1.DataSource, DataSet)
End Sub
-------------------------END index.aspx.vb----------------------------------
Question by:carpetflyer
    LVL 5

    Accepted Solution

    What u can do is loop through your datagrid to get your values, look at this link:

    Accessing Controls value from DataGrid in Asp.Net

    LVL 3

    Author Comment

    Thanks allanau20, I will try it out and see how it goes.
    LVL 3

    Author Comment

    Wow allanau20, what an easy solution! I spent hours on finding a solution and yours was nice and simple.

    Since I have 15 template columns, instead of manually typing the name of the control ie:
    string = CType(dGridItem.FindControl("txtBoxName"),TextBoxt).Text

    I renamed each textbox's id to txtBox1, txtBox2, etc ie:
    <asp:templatecolumn headertext="Month" visible="False">
                        <asp:textbox runat="server" id="txtBox1" columns="10" text='<%#  DataBinder.Eval( Container.DataItem, "monthField" ) %>'>

    And did:
    For Each dgridItem As DataGridItem In DataGrid1.Items
                For i As Integer = 0 To 12
                    Response.Write(CType(dgridItem.FindControl("txtBox" & i), TextBox).Text)

    Now i will put the changes in the DataSet and update the DB.

    Thanks again allanau20! :)
    LVL 5

    Expert Comment

    Glad I could help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now