Solved

Use Fincontrol on an aspx form using c#

Posted on 2008-10-17
7
475 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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