Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to get textbox values from a DataGrid Template Column

Posted on 2005-04-07
4
Medium Priority
?
384 Views
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.

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----------------------------------
0
Comment
Question by:carpetflyer
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
allanau20 earned 2000 total points
ID: 13732583
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
 
LVL 3

Author Comment

by:carpetflyer
ID: 13738801
Thanks allanau20, I will try it out and see how it goes.
0
 
LVL 3

Author Comment

by:carpetflyer
ID: 13739037
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
 
LVL 5

Expert Comment

by:allanau20
ID: 13739046
Glad I could help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

564 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