Solved

FindControl in button OnClick

Posted on 2010-08-13
7
1,502 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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!
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

12 Experts available now in Live!

Get 1:1 Help Now