Link to home
Start Free TrialLog in
Avatar of GRCHELPDESK
GRCHELPDESK

asked on

Retrieving values from DetailsView Control with template fields

Hello Experts,

I'm new to the details view control.  I've created one, and and it's pulling data perfectly.  I then created an "accept" button.  When the user clicks it, take the first 2 fields of the DetailsView control and populate text boxes with them like this:

        txtCompanyName.Text = dvCompany.Rows(0).Cells(1).Text
        txtCompanyID.Text = dvCompany.Rows(1).Cells(1).Text

This was working like a charm.  Them I turned those first 2 fields into Template Fields so I could validate them.  Ever since then, the above lines pull a blank string.  It's almost like, the above just doesn't apply to template fields.  Or something.  Does anyone know how I can get this working?

Thanks,
GRCHELPDESK
Avatar of RedKelvin
RedKelvin

If your template fields have controls within them, you will need to get the control value not the column value.

You can do it like this

Dim lblId As Label = CType(e.Item.FindControl("lblId"), Label)

that is for a label control with an id of lblId.

This needs to be used inside a grid event, otherwise e will not be found
Avatar of GRCHELPDESK

ASKER

Hey RedKelvin,

Thanks for the reply.  Although, I'm not following entirely.  I'm not sure what you mean about "inside the grid event".  Can you clarify that for me?  And also, will it be possible to have this happen in the button click event?

Thanks!
GRCHELPDESK
ASKER CERTIFIED SOLUTION
Avatar of RedKelvin
RedKelvin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey RedKelvin,

Awesome, it's working.  I had to modify it a slightly, so I'll post that code for anyone running into this.  On button click:

        Dim lblCompanyName As Label
        Dim lblCompanyID As Label

        For Each row As DetailsViewRow In dvCompany.Rows
            lblCompanyName = CType(row.FindControl("label1"), Label)
            lblCompanyID = CType(row.FindControl("label2"), Label)
            txtCompanyName.Text = lblCompanyName.Text
            txtCompanyID.Text = lblCompanyID.Text
        Next

Many thanks for that!!

Cheers,
GRCHELPDESK