Solved

Use Fincontrol on an aspx form using c#

Posted on 2008-10-17
7
478 Views
Last Modified: 2013-12-17
I am trying to use FindControl on aspx form using c#.  My code is as follows and FinControl returns null.  Thanks

<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" Theme="Default" CodeBehind="Default.aspx.cs" Inherits="Application.Web.Default" Title="Information Database" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table width="100%">
        <tr>
            <td>
               <div  ><asp:LinkButton Text="Clear" ID="ClearButton1" CssClass="clearButton" runat="server" OnClick="ClearButton_Onclick" CommandArgument="OfficeList"></asp:LinkButton></div>
            </td>

        protected void ClearButton_Onclick(object sender, EventArgs e)
        {//CommandArgument contains name of control to be found.
            LinkButton lb = sender as LinkButton;
             ListBox List = (ListBox)FindControl(lb.CommandArgument);
            List.SelectedIndex = -1;
0
Comment
Question by:eshurak
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:JunkMan
ID: 22746350
If the control "OfficeLis" being added progamatically or does it exist on the page?
0
 
LVL 3

Expert Comment

by:JunkMan
ID: 22746351
sorry typo there, meant "OfficeList"
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22747548
you cannot find a control by using the commandargument

if you use lb.ID you will be able to find your control and later you can use your command argument to take a decision what has to be done
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 3

Author Comment

by:eshurak
ID: 22760130
JunkMan - OfficeList already exists on the page.  No new objects are being created programatically.

Ragi - the lb.ID is the name of the Label that is being clicked.  I don't want to find my labels.  I want to use the command arguement of the Label to find the corresiponding listbox and clear it's selected value.  There are several label and listbox pairs on the aspx page.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22760392
If you look up for the definition of FindControl in MSDN, it tells that you need to pass the ID of the control. Passing anything else will not return you the control.
So as i previously stated you cannot find your control using the command name you have to use the ID
0
 
LVL 3

Author Comment

by:eshurak
ID: 22761537
Ragi- Right, ID is string which is what is contained in the CommandArgument.

Below is the example from MSDN (http://msdn.microsoft.com/en-us/library/486wc64h.aspx) using a string:

Control myControl1 = FindControl("TextBox2");

0
 
LVL 3

Accepted Solution

by:
eshurak earned 0 total points
ID: 22761830
Found the solution:

            LinkButton lb = sender as LinkButton;
            ListBox List = (ListBox)Master.FindControl("ContentPlaceHolder1").FindControl(lb.CommandArgument);
            List.SelectedIndex = -1;

I had to be very specific on where to look.  Now List is no longer null.  
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

734 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