Solved

user control inside of a databound listview

Posted on 2010-08-16
4
853 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

778 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