Solved

user control inside of a databound listview

Posted on 2010-08-16
4
852 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the input - I appreciate it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now