Solved

Use Fincontrol on an aspx form using c#

Posted on 2008-10-17
7
477 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

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…
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.

726 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