Solved

FindControl in button OnClick

Posted on 2010-08-13
7
1,527 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 500 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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