ASP.net add ID values to Text Box cells of GridView template columns

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

I use the following VB.net code to populate textbox cells in my ASP.net GridView columns.
I also need to store a unique ID value for each cell. I am currently using the ToolTip to do this
Is there another way that I can embed my ID value in each cell?

    Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

        Dim oColIndex As Integer
        Dim oDate As String
        Dim oEachDate As Date
        Dim oTemplateColumn As String
        Dim oDateList As String = TryCast(Session("oDateList"), String)
       Dim oLineID_PlusDate As String 'this is eg Line ID 19 apr 2019
        Dim arrDates As Object = Split(oDateList, ",")
        For i As Integer = 0 To UBound(arrDates)
            oColIndex = i + 2
            oTemplateColumn = "D" & CStr(i + 1)
            oEachDate = CDate(arrDates(i))
            oDate = oEachDate.ToString("dd MMM yyyy")
            Me.GridView1.Columns(oColIndex).Visible = True
            If e.Row.RowType = DataControlRowType.Header Then
                Me.GridView1.Columns(oColIndex).HeaderText = oDate
            End If

            'Me.GridView1.Columns(2).Visible = True
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim D1 As TextBox = CType(e.Row.FindControl(oTemplateColumn), TextBox)
                If Not IsDBNull(e.Row.DataItem(oDate)) Then
                    D1.Text = e.Row.DataItem(oDate)
                End If
               If Not IsDBNull(e.Row.DataItem(oLineID_PlusDate)) Then
                    D.ToolTip = e.Row.DataItem(oLineID_PlusDate)
                End If
            End If
        Next
    End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Obadiah ChristopherDeveloper User Interface

Commented:
In addition to having a textbox in the templatefield, can you add a hiddenfield too? And bind the hiddenfield to oLineID_PlusDate?
Murray BrownASP.net/VSTO Developer

Author

Commented:
I don't want to do that.
Obadiah ChristopherDeveloper User Interface

Commented:
okay. Sorry, I can't think of anything else. Just curious what's the scenario? A tooltip will show the value of the id to the end-user in the UI just by moving the mouse cursor.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Murray BrownASP.net/VSTO Developer

Author

Commented:
the scenario is that I need to run an update when GridView "cell" is changed and the  LinkID is what I need in the update statement. There are currently 30 columns and might be over 300. I don't want to have to then add an additional 300 columns just to hold the ID
Obadiah ChristopherDeveloper User Interface

Commented:
okay.

I was not suggesting to add another column for the hiddenfield. However, it's still upto you to decide if you want to go with this.

<asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
                            <asp:HiddenField ID="hdn1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>

Open in new window


Dim D1 As TextBox = CType(e.Row.FindControl(oTemplateColumn), TextBox)
Dim H1Field As HiddenField = CType(e.Row.FindControl(oTemplateColumn), HiddenField)
                If Not IsDBNull(e.Row.DataItem(oDate)) Then
                    D1.Text = e.Row.DataItem(oDate)
                End If
               If Not IsDBNull(e.Row.DataItem(oLineID_PlusDate)) Then
                    H1Field.Value = e.Row.DataItem(oLineID_PlusDate)
                End If

Open in new window

Why do not you use the ID property of the Textbox ?
 D1.ID = e.Row.DataItem(oLineID_PlusDate) &
  & "_" & CStr(i + 1)

you can then get the linkid by making a split to the ID using "_" as separator
Murray BrownASP.net/VSTO Developer

Author

Commented:
Great idea! Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial