Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

user control inside of a databound listview

Posted on 2010-08-16
4
Medium Priority
?
879 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
  • 3
4 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

916 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