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

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 884
  • Last Modified:

user control inside of a databound listview


I have a listview object bound to a datasource.
The itemtemplate contains a number of controls
In the code behind, I am looping through my listview records and specifying the controls as follows:

Dim LOCAL_MyCheckbox As CheckBox
For X=0 To MyListview.Items.Count - 1
     LOCAL_MyCheckbox = MyListview.Items(X).FindControl("MyCheckbox")
     if (LOCAL_MyCheckbox.checked) then
          bla bla
     end if

This works just fine.  However, one of the controls inside of my itemtemplate is a user control.  It displays perfectly, but I don't know the syntax needed to refer to it in code behind.

In my item template is is:
<GetUserTag:GetUser ID="work_records_results1_emailTo" runat="server" />
The user control is a dropdown list of user names

I use that user control a lot.  When it is NOT in a listview I usually refer to it like:

I thought my code behind might be something like

Dim LOCAL_Mydropdown As DropdownList
For X=0 To MyListview.Items.Count - 1
      LOCAL_Mydropdown = MyListview.Items(X).FindControl("Mydropdown.Findcontrol(UserControlDropDownUsers_User'')")

But that doesn't work

Any ideas?

  • 3
1 Solution
So what are you trying to fine? Your UC or dropdownlist?
First you need to find the UC and then you find the DDL inside that UC

you find it by your control id

YourUC uc = DirectCast(MyListview.Items(X).FindControl("work_records_results1_emailTo''),, YourUC)
Now find DDL in "uc"

santaspores1Author Commented:
I have a user control named UserControlDropDownUsers
it has a DropDownList called UserControlDropDownUsers_User
My aspx page registers it with the following:
<%@ Register Src="~/UserControlDropDownUsers.ascx" TagName="GetUser" TagPrefix="GetUserTag" %>

I have a listview named work_records_results1_ListView
The ItemTemplate includes the following:
A checkbox - for example
<asp:Label ID="work_records_results1_Hold_Text" runat="server" Visible='<%# Eval("status_current").ToString() = "unprocessed" %>' Text="<br />Flag as hold" Enabled = "true"> </asp:Label>
And here is a reference to my user control
<GetUserTag:GetUser ID="work_records_results1_emailTo" runat="server" />

In my code behind, I can loop through my listview-associated records and get at the checkbox as follows:
Dim X_work_records_results1_ProcessCheck As CheckBox
For X=0 To work_records_results1_ListView.Items.Count - 1
     X_work_records_results1_ProcessCheck =    work_records_results1_ListView.Items(X).FindControl("work_records_results1_ProcessCheck")

However, I don't know how to get at the selected value of my drop down
The following does NOT seem to work:

Dim X_work_records_results1_emailTo As DropdownList
For X=0 To work_records_results1_ListView.Items.Count - 1
  X_work_records_results1_emailTo = work_records_results1_ListView.Items(X).FindControl("work_records_results1_emailTo")
if (X_work_records_results1_emailTo.SelectedIndex = 0) then
... this is where the app craps out
System.NullReferenceException: Object reference not set to an instance of an object.
end if

Now... if I had placed the user control somewhere on the page above or below the listview, I could get at the selected index by using the following:
Dim X_changed_by As DropDownList
X_changed_by = work_records_changed_by.FindControl("UserControlDropDownUsers_User")

SO... to get at the selected index of a user control in my listview itemtemplate I would think that I need something along the lines of....
Dim X_work_records_results1_emailTo As DropdownList
  X_work_records_results1_emailTo = work_records_results1_ListView.Items(X).FindControl("work_records_results1_emailTo").FindControl("UserControlDropDownUsers_User")

But the above doesn't seem to work
santaspores1Author Commented:
I found the solution (eventually):

X_work_records_results1_emailTo = work_records_results1_ListView.Items(X).FindControl("work_records_results1_emailTo").Findcontrol("UserControlDropDownUsers_User")

That actually does work.
santaspores1Author Commented:
Thanks for the input - I appreciate it.

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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now