Solved

Use Fincontrol on an aspx form using c#

Posted on 2008-10-17
7
469 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
Comment Utility
If the control "OfficeLis" being added progamatically or does it exist on the page?
0
 
LVL 3

Expert Comment

by:JunkMan
Comment Utility
sorry typo there, meant "OfficeList"
0
 
LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Author Comment

by:eshurak
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

11 Experts available now in Live!

Get 1:1 Help Now