FindControl RRS feed

  • Question

  • User-1929006223 posted

    I'm not able to use FindControl in pageload to get a valid handle on controls.
    Why doesn't this code work? I'm going out of my mind.

    Syntax in pageload:

    public partial class mypage_aspx : System.Web.UI.Page
    public mypage_aspx()
    void Page_Load(object sender, EventArgs e)
    //some code..
    //then get a handle on dropdown, does not work?!
      DropDownList DropList = (DropDownList)this.FindControl("MyDropDownList");

    Code in mypage.aspx:

    <%@ Page Language="C#" MasterPageFile="MasterPage.master" CodeFile="mypage.aspx.cs"
    Inherits="mypage_aspx" Title="Mypage" Culture="auto" UICulture="auto" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="maincontent" runat="Server">
    <div id="adminedit">   
                    <td valign="top">
                    <fieldset id = "fieldset1">
                    <!-- add H2 here and hide it with css since you can not put h2 inside a legend tag -->
                    <h2 class="none">
                        <asp:Literal ID="Myliteral1" Text="MyPage" runat="server"/></h2>
                    <asp:Label id="Label2" runat="server" />
                    <br />
                    <br />
                    <legend><asp:Literal ID="Myliteral2" Text="Mypage" runat="server"/></legend>
                                <td colspan="2">
                        <asp:DropDownList AutoPostBack="true" ID="MyDropDownList" runat="server" DataSourceID="ObjectDataSource1" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
                            DataValueField="Code" >


    Sunday, July 2, 2006 6:27 AM

All replies

  • User-1069184416 posted

    FindControl is not a recursive method! Check out this method to see how to FindControls when you are working with MasterPages and ContentPages:

    ASP.NET 2.0 MasterPages and FindControl()



    Sunday, July 2, 2006 9:35 AM
  • User-1929006223 posted

    Thanks, that worked as a charm! I'm very greatful for your answear. It helped me a lot, I don't have to write repetitive code for each dropdown. Lebanon rules!

    Sunday, July 2, 2006 5:08 PM
  • User-1069184416 posted

    I am glad I was able to help you out!!


    Monday, July 3, 2006 8:08 AM