Solved

FindControl in button OnClick

Posted on 2010-08-13
7
1,515 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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