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">
<table>
<tr>
<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>
<table>
<tr>
<td colspan="2">
<asp:DropDownList AutoPostBack="true" ID="MyDropDownList" runat="server" DataSourceID="ObjectDataSource1" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
DataTextField="Description"
DataValueField="Code" >
</asp:DropDownList>
<asp:ObjectDatasSource etc..
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!
Beemray
Member
52 Points
22 Posts
FindControl
Jul 02, 2006 10:27 AM|LINK
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">
<table>
<tr>
<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>
<table>
<tr>
<td colspan="2">
<asp:DropDownList AutoPostBack="true" ID="MyDropDownList" runat="server" DataSourceID="ObjectDataSource1" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
DataTextField="Description"
DataValueField="Code" >
</asp:DropDownList>
<asp:ObjectDatasSource etc..
haidar_bilal
All-Star
45607 Points
8728 Posts
MVP
Re: FindControl
Jul 02, 2006 01:35 PM|LINK
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()
HTH,
Regards
Beemray
Member
52 Points
22 Posts
Re: FindControl
Jul 02, 2006 09:08 PM|LINK
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!
haidar_bilal
All-Star
45607 Points
8728 Posts
MVP
Re: FindControl
Jul 03, 2006 12:08 PM|LINK
I am glad I was able to help you out!!
Regards