eshurak
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.Ma ster" AutoEventWireup="true" Theme="Default" CodeBehind="Default.aspx.c s" Inherits="Application.Web. Default" Title="Information Database" %>
<asp:Content ID="Content2" ContentPlaceHolderID="Cont entPlaceHo lder1" runat="server">
<table width="100%">
<tr>
<td>
<div ><asp:LinkButton Text="Clear" ID="ClearButton1" CssClass="clearButton" runat="server" OnClick="ClearButton_Oncli ck" CommandArgument="OfficeLis t"></asp:L inkButton> </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.Co mmandArgum ent);
List.SelectedIndex = -1;
<%@ Page Language="C#" MasterPageFile="~/Site1.Ma
<asp:Content ID="Content2" ContentPlaceHolderID="Cont
<table width="100%">
<tr>
<td>
<div ><asp:LinkButton Text="Clear" ID="ClearButton1" CssClass="clearButton" runat="server" OnClick="ClearButton_Oncli
</td>
protected void ClearButton_Onclick(object
{//CommandArgument contains name of control to be found.
LinkButton lb = sender as LinkButton;
ListBox List = (ListBox)FindControl(lb.Co
List.SelectedIndex = -1;
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
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
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.
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
So as i previously stated you cannot find your control using the command name you have to use the ID
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");
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.