Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FindControl in button OnClick

Posted on 2010-08-13
7
Medium Priority
?
1,612 Views
Last Modified: 2013-11-08

I have a List Picker inside a Listview control. I need to perform some operations when the  user clicks on the button to transfer an item from one list box to another.

I have so far not been able to get a reference to the ListBox so I can make changes when the button is clicked.

How can I get FindControl to get a reference to a control. My attempts are in my code below. They have all returned null

ASP.Net 3.5
C#

thanks!
protected void btnAdd_Click(object sender, EventArgs e)
        {

            //Control myControl1 = FindControl("lstAllMovies");
            //ListBox textBox = (ListBox)this.lvCustomers.FindControl("lstAllMovies");

            ListBox fooListBox = (ListBox)lvCustomers.Items[0].FindControl("lstAllMovies");

            /*
            ListItem item = lstAllMovies.SelectedItem;
            if (item != null)
            {
                lstAllMovies.Items.Remove(item);
                lstFavoriteMovies.ClearSelection();
                lstFavoriteMovies.Items.Add(item);
            }
             */
        }

///////////////

<asp:UpdatePanel ID="updatePanel" runat="server"  UpdateMode="Conditional">
                                                            <ContentTemplate>
                                                                                                           
                                                            <div class="listPicker">
                                                                <div style="float:left;width:40%">

                                                                <asp:ListBox
                                                                    id="lstAllMovies"
                                                                    DataSourceID="srcMovies"
                                                                    DataTextField="Title"
                                                                    DataValueField="Id"
                                                                    Runat="server" />
                                                                </div>
                                                                <div style="float:left;width:20%;text-align:center">
                                                                <asp:Button
                                                                    id="btnAdd"
                                                                    Text="--&gt;"
                                                                    ToolTip="Add List Item"
                                                                    Runat="server" 
                                                                    OnClick="btnAdd_Click" />
                                                                <br />
                                                                <asp:Button
                                                                    id="btnRemove"
                                                                    Text="&lt;--"
                                                                    ToolTip="Remove List Item"
                                                                    Runat="server" 
                                                                    OnClick="btnRemove_Click" />
                                                                </div>
                                                                <div style="float:left;width:40%">
                                                                <asp:ListBox
                                                                    id="lstFavoriteMovies"
                                                                    Runat="server" />
                                                                </div>
                                                                <br style="clear:both" />
                                                                </div>
                                                            </div>
                                                            </ContentTemplate>
                                                    </asp:UpdatePanel>

Open in new window

protected void btnAdd_Click(object sender, EventArgs e)
{
   //Control myControl1 = FindControl("lstAllMovies");
   //ListBox textBox = (ListBox)this.lvCustomers.FindControl("lstAllMovies");

   ListBox fooListBox = (ListBox)lvCustomers.Items[0].FindControl("lstAllMovies");

   /*
   ListItem item = lstAllMovies.SelectedItem;
   if (item != null)
   {
      lstAllMovies.Items.Remove(item);
      lstFavoriteMovies.ClearSelection();
      lstFavoriteMovies.Items.Add(item);
   }
  */
        }

///////////////

<asp:UpdatePanel ID="updatePanel" runat="server"  UpdateMode="Conditional">
                                                            <ContentTemplate>
                                                                                                           
                                                            <div class="listPicker">
                                                                <div style="float:left;width:40%">

                                                                <asp:ListBox
                                                                    id="lstAllMovies"
                                                                    DataSourceID="srcMovies"
                                                                    DataTextField="Title"
                                                                    DataValueField="Id"
                                                                    Runat="server" />
                                                                </div>
                                                                <div style="float:left;width:20%;text-align:center">
                                                                <asp:Button
                                                                    id="btnAdd"
                                                                    Text="--&gt;"
                                                                    ToolTip="Add List Item"
                                                                    Runat="server" 
                                                                    OnClick="btnAdd_Click" />
                                                                <br />
                                                                <asp:Button
                                                                    id="btnRemove"
                                                                    Text="&lt;--"
                                                                    ToolTip="Remove List Item"
                                                                    Runat="server" 
                                                                    OnClick="btnRemove_Click" />
                                                                </div>
                                                                <div style="float:left;width:40%">
                                                                <asp:ListBox
                                                                    id="lstFavoriteMovies"
                                                                    Runat="server" />
                                                                </div>
                                                                <br style="clear:both" />
                                                                </div>
                                                            </div>
                                                            </ContentTemplate>
                                                    </asp:UpdatePanel>

Open in new window

0
Comment
Question by:DTwined
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:almander
ID: 33433040
This is probably because your control is on a master page, or layered somewhere else?

Here is one inefficent but simple method to recursively search for your control.
http://www.west-wind.com/Weblog/posts/5127.aspx

Also, the sender object should be the control that was clicked.
fooListBox = (ListBox)sender;
0
 
LVL 16

Expert Comment

by:Stephan
ID: 33433041
What is lvCustomers?

I cannot find this in any code you have added.

Futhermore, you have here a control lstAllMovies, why use findcontrol?

I'm not sure what you want to do with your findcontrol instance, but if i'm looking so far, you doing it wrong..

Just use the lstAllMovies.SelectedValue and add this to your lstFavoriteMovies and remove it from the lstAllMovies.
0
 
LVL 5

Expert Comment

by:JayFromPep
ID: 33433044
try getting the parent or the parent's parent on the button.click event.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:DTwined
ID: 33433153
lvCustomers is the ListView that contains the List Picker.
The List Picker is located in the <EditItemTemplate> part of the ListView lvCustomers.

The code I attached to my question contains the controls that make up the List Picker.

stephanonline: I am not able to access the controls to perform the actions as you noted. Please see the commented section of my onClick function. The compiler says it is not able to find the controls as they appear in the commented section, which is why I have tried to implement FindControl().

Thanks!
0
 
LVL 16

Accepted Solution

by:
Stephan earned 2000 total points
ID: 33433284
Try it like this:

IList<ListViewDataItem> lst = ListView1.Items;
foreach (ListViewDataItem list in lst)
{
     list.FindControl("lstAllMovies")
}

inside the foreach you can check the ItemType of the ListViewDataItem in order to check what kind of item you need.

else I recommend you to use the OnItemCommand event to fix this issue.
0
 
LVL 5

Expert Comment

by:JayFromPep
ID: 33433330
Is this your scenario?

Two listviews, one with selectable items, one to hold the items that were selected
A button outside of those two listviews to transfer items....

If that is the case, you would need to grab the index of the selected item when you selected it.  That listview will loose focus when you click the button.  Throw that into session or a hidden control or something.  If you are doing multiples, get them all.

In the onclick event handler for the button, grab index of the items you just stored, go to the originating listview, grab the item and either copy it or move to the new listview.

I dont have a lot of time to mock it up, but that should work.
0
 

Author Closing Comment

by:DTwined
ID: 33433896

Thanks stephanonline. There was a cut and paste error in my list swapping code which is why it made no sense. Doh!

Thanks!
D
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 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