Solved

FindControl in button OnClick

Posted on 2010-08-13
7
1,494 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

13 Experts available now in Live!

Get 1:1 Help Now