Link to home
Start Free TrialLog in
Avatar of eshurak
eshurakFlag for United States of America

asked on

Use Fincontrol on an aspx form using c#

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;
Avatar of JunkMan
JunkMan
Flag of United Kingdom of Great Britain and Northern Ireland image

If the control "OfficeLis" being added progamatically or does it exist on the page?
sorry typo there, meant "OfficeList"
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
Avatar of eshurak

ASKER

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.
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
Avatar of eshurak

ASKER

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");

ASKER CERTIFIED SOLUTION
Avatar of eshurak
eshurak
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial