Solved

user control inside of a databound listview

Posted on 2010-08-16
4
867 Views
Last Modified: 2013-12-26
Hello,

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
Next

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:
Mydropdown.FindControl("UserControlDropDownUsers_User")

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'')")
Next

But that doesn't work

Any ideas?



0
Comment
Question by:santaspores1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33450400
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"

0
 

Author Comment

by:santaspores1
ID: 33453391
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")
Next

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
Next

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
0
 

Author Comment

by:santaspores1
ID: 33453591
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.
0
 

Author Closing Comment

by:santaspores1
ID: 33453593
Thanks for the input - I appreciate it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question