How to get textbox values from a DataGrid Template Column

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.

Thanks.

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">
            <columns>
                  <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">
                        <itemtemplate>
                              <asp:textbox runat="server" id="txtMonth" columns="10" text='<%#  DataBinder.Eval( Container.DataItem, "monthField" ) %>'>
                              </asp:textbox>
                        </itemtemplate>
                  </asp:templatecolumn>
                 </columns>
</asp:datagrid>
-----------------------------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)
      'WHAT SHOULD I DO HERE?
     'Dim ds As Dataset
     'ds = CType(DataGrid1.DataSource, DataSet)
End Sub
-------------------------END index.aspx.vb----------------------------------
LVL 3
carpetflyerAsked:
Who is Participating?
 
allanau20Commented:
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

http://www.c-sharpcorner.com/Code/2003/Jan/AccessDataGridVal.asp

HtH
0
 
carpetflyerAuthor Commented:
Thanks allanau20, I will try it out and see how it goes.
0
 
carpetflyerAuthor Commented:
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">
           <itemtemplate>
                    <asp:textbox runat="server" id="txtBox1" columns="10" text='<%#  DataBinder.Eval( Container.DataItem, "monthField" ) %>'>
                    </asp:textbox>
           </itemtemplate>
</asp:templatecolumn>

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)
                Response.Write("<br>")
            Next
 Next


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

Thanks again allanau20! :)
0
 
allanau20Commented:
Glad I could help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.