471,073 Members | 1,158 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Losing Hope! FindControl of dynamic gridview not working

PLEASE be patient and read this. I believe I've actually reached the end
of the Web trying to find the answer!

I have an aspx using a MasterPage with a ContentPlaceholder.

I have a datatable that I use to dynamically populate a gridview - I
have to do this in order to specify which control type to put in one of
the columns. All this works fine. I've run Trace and can clearly see
that the control.id I assigned (lblQuestionID) is there
(UniqueID: ctl00$cphMain$gv1$ctl02$lblQuestionID for the first row).
Yet...on a button click event I try to get the control value...

Dim QuestionID as String= CType(gv1.FindControl("lblQuestionID"),

...returns "Object reference not set to an instance of an object" error.

This is how I load the gridview (abbreviated version):

Public Sub gv1_RowDataBound(ByVal sender As System.Object, ByVal e As
GridViewRowEventArgs) Handles gv1.RowDataBound

If e.Row.RowType = ListItemType.Item Or e.Row.RowType =
ListItemType.AlternatingItem Then

'put Question ID in first column
Dim lblID As New Label
lblID.ID = "lblQuestionID"
lblID.Text = dgTable(gv1).Rows(e.Row.DataItemIndex)(0)
lblID.EnableViewState = True

I have other controls (dropdownlist, text, etc.) that I need to get the
new value of after being changed by the user. I can't get any control
values including the above example.

BTW, I tested the above FindControl syntax on a simple gridview with a
standard SqlDataSource datasource and it worked. My problem is that I
must use the dynamic controls. Also, I compared both gridview items in
Trace and they were identical right down to the UniqueIDs.

I'm begging anyone out there who could suggest a solution.


P.S. The following used to work on my old and truly missed datagrid:

QuestionID = CType(datagrid1.Rows(i).Cells(0).Controls(0), Label).Text
-- and I've tried variations such as Controls(2), etc.

*** Sent via Developersdex http://www.developersdex.com ***
May 25 '06 #1
0 2244

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

86 posts views Thread by Michael Adams | last post: by
4 posts views Thread by Stephen | last post: by
9 posts views Thread by Adrian Parker | last post: by
7 posts views Thread by DaTurk | last post: by
3 posts views Thread by David C | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.