Reading data from Template Column

J

jcriv

Hi,
I have a datagrid on a webform and I also have an OK Button. I designed the
datagrid in Visual Studio. The dg has 3 'databound' coulmns and 3 template
columns. I used the drop down tool 'Edit Template' to 'Item Template' to a
TextBox with an initial text value.

When I run the program I bind the grid to a dataset which has 3 columns.
The grid displays just fine. My first three columns have the data from my
dataset and the last three columns have my initial text value in the text
boxes.

What I want to do is to let a user tab thru the template textboxes and make
entries and when done press the OK button on the form. I then want to
retieve the values the user entered in the template textboxes.

In my 'code behind' I have code the looks like

For i = 0 to dsMyDataSet.Tables(0).Rows.Count - 1

strText = dgMyGrid.Items(i).Cells(3).Text

Next

But, the text values I get in all cases is "" (blank). I don't get whet
the user types in nor do I get my initial default value. Note: For Cells
(0,1,2) I do get back the databound values. Cells(3) would be the first
'Template' cell.

Any idea what I might be doing wrong here?

Thanks
 
J

Jos

jcriv said:
Hi,
I have a datagrid on a webform and I also have an OK Button. I
designed the datagrid in Visual Studio. The dg has 3 'databound'
coulmns and 3 template columns. I used the drop down tool 'Edit
Template' to 'Item Template' to a TextBox with an initial text value.

When I run the program I bind the grid to a dataset which has 3
columns. The grid displays just fine. My first three columns have
the data from my dataset and the last three columns have my initial
text value in the text boxes.

What I want to do is to let a user tab thru the template textboxes
and make entries and when done press the OK button on the form. I
then want to retieve the values the user entered in the template
textboxes.

In my 'code behind' I have code the looks like

For i = 0 to dsMyDataSet.Tables(0).Rows.Count - 1

strText = dgMyGrid.Items(i).Cells(3).Text

Next

But, the text values I get in all cases is "" (blank). I don't get
whet the user types in nor do I get my initial default value. Note:
For Cells (0,1,2) I do get back the databound values. Cells(3)
would be the first 'Template' cell.

Any idea what I might be doing wrong here?

Thanks

Cells(3).Text would be the Text value of the table cell, not
the Text value of the TextBox.

Use:
strText = CType(dgMyGrid.Items(i).FindControl("IDOfTextBox"),TextBox).Text

where IDOfTextBox is the ID that you assigned to the TextBox.
 
J

jcriv

Thanks, that works. In the mean time I also discovered that the following
works
dim tb as System.Web.UI.WebControls.TextBox

tb = dgMyGrid.Items(i).Cells(3).Controls(1)

myStr = tb.Text

I'm not sure how all of this is related. I looked around the VS Help and
found a lot of info about constructing you own templates, etc but not much
about simply using what's already there. If you know a good link I'd
appreciate it. Preferably a VS designer example and not one heavy on html.

Thanks
 
J

Jos

jcriv said:
Thanks, that works. In the mean time I also discovered that the
following works
dim tb as System.Web.UI.WebControls.TextBox

tb = dgMyGrid.Items(i).Cells(3).Controls(1)

myStr = tb.Text

I'm not sure how all of this is related. I looked around the VS Help
and found a lot of info about constructing you own templates, etc but
not much about simply using what's already there. If you know a good
link I'd appreciate it. Preferably a VS designer example and not one
heavy on html.

The textbox is the second control in the cell.
The first control is a Literal control, created by ASP.NET to
represent any white space between the
<EditItemTemplate> tag and the <asp:TextBox> tag.

I prefer FindControl because it keeps working, even
if you add other controls to the template later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top