CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

MS SQL 2008 on ASP.NET Hosting



Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.master_pages_themes_and_navigation_controls Tags:
Item Type: NewsGroup Date Entered: 12/7/2005 4:20:48 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 3 Views: 67 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
4 Items, 1 Pages 1 |< << Go >> >|
idoyael
Asp.Net User
Why the FindControl doesn't find a control in page that have MasterPage?12/7/2005 4:20:48 PM

0/0

I'm try to find a control and the control return null.

It's a very simple code, nothing new and it's can not find the txtbox also.

what can be the reason?

Thanks,

Ido y

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server" >

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"

EnableTheming="False" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

</asp:Content>

code behind

protected void Page_Load(object sender, EventArgs e)

{

bool b = Page.HasControls();

Control c = Page.FindControl("RequiredFieldValidator1");

//TextBox1.Text = c.GetType().ToString();

}


Ido Yahalomi,
.NET Architect www.reptor.com.
billrob458
Asp.Net User
Re: Why the FindControl doesn't find a control in page that have MasterPage?12/7/2005 5:50:36 PM

0/0

Your text box control is being placed inside a content region inside a master page.  With the INamingContainer interface, the IDs of your control are being renamed.  ie Your TextBox's ID is something like

ctl00$MainContent_TextBox1

First you should be able to reference the controls by their name in the codebehind.

TextBox1.Text = "My text";

Or for a workaround, I will place the controls inside a placeholder control, then use the place holder's FindControl method to load up the correct control.

<asp:Content ...>
  <asp:PlaceHolder id="ph" runat="server">
    <asp:TextBox id="TextBox1" runat="server" />
  </asp:PlaceHolder>
</asp:Content>

Then from CodeBehind:

TextBox textBox = ph.FindControl( "TextBox1" ) as TextBox;

bill

idoyael
Asp.Net User
Re: Why the FindControl doesn't find a control in page that have MasterPage?12/8/2005 7:00:13 AM

0/0

Thanks. the workaround working great.
Ido Yahalomi,
.NET Architect www.reptor.com.
smercurio05
Asp.Net User
Re: Why the FindControl doesn't find a control in page that have MasterPage?12/8/2005 3:28:15 PM

0/0

Bill's response will work. If you don't want to use placeholder you can also do something like this (Note that this is in VB.NET):

        Dim cb As Control
        Dim ctl As RequiredFieldValidator ' or whatever datatype is the control you're looking for

        cb = Me.Page.Form.FindControl("MainContent")        ' Gets content part of page
        ctl = cb.FindControl("RequiredFieldValidator1")       ' Get the RequiredFieldValidator in here
       ...

4 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
ASP.NET 2.0: A Developer's Notebook Authors: Wei Meng Lee, Pages: 326, Published: 2005
ASP. NET V. 2. 0-the Beta Version: The Beta Version Authors: Alex Homer, Dave Sussman, Rob Howard, Pages: 620, Published: 2004

Web:
can't find control in master page Old 06-02-2006, 10:59 AM. Default can't find control in master page. Hi, In my master page I have :
...
ASP.NET 2.0 MasterPages and FindControl() - Rick Strahl's Web Log NET Forums - FindControl() doesn't work if page has MasterPage ... Find control method In conent.aspx.cs on Page_Load event I have the below code. ...
Visual Studio Magazine Online | Practical ASP.NET: Using ... May 16, 2008 ... I tried using find control in a rowDataBound event handler to get the value of a checkbox in a gridView. The page uses a master page too. ...
FindControl() doesn't work if page has MasterPage - ASP.NET Forums FindControl() doesn't work if page has MasterPage ... signature of the method so it accepts all Control objects and not just Page objects: ...
FIND CONTROLS IN AN ASP.NET PAGE WITH A MASTERPAGE | Dev102.com Mar 17, 2008 ... Page.FindControl("LoginButton");. This will work only if we don’t have a MasterPage present. If a master page is present we must add the ...
Access Controls Contained in a MasterPage through a Content Page ... Control masterPageControl = Page.Master.FindControl("MyLabelID"); ... The exception indicates that the masterpage can't find it. Rob. 5/9/2008 Allan ...
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps Apr 11, 2006 ... If a specific page doesn’t want it’s master page set, ...... Just as we have to be careful with FindControl, we have to be careful with ...
CodeProject: Master Page solution just like in ASP.NET 2!. Free ... I try it but unfortunately I can't use the FindControl methode anymore. When I want to find a control of the page - Either 'this.FindControl("ControlId")' ...
Access property and control of master page from a customized ... and have a master page, their relation is as below ... however this method doesn 't work on access a user control. how to solve? ...
Get controls on a page, that is using a masterpage | Tech Off ... Jul 27, 2006 ... public static Control Find(Control C, String ControlName) ... FindControl doesn' t work with a master page. example: Create a master page: ...




Search This Site:










wrong percepetion about how .skin output should work?

sitemappath, dynamically created

changing background color to yellow

menu with seperators

object reference not set to an instance of an object

how to skin a fileupload control/

q: how to disable a node in a treeview?

exposing properties of master page

insert an <object /> into masterpage

page load order

tree menu control event

securitytrimmingenabled bug

master page and javascript

can't apply themes to master pages - how do you style the master pages then?

i can't apply stylesheet theme to my page

custom control's events in master page

query regarding menu control in master page

need help with code in my content page

treeview does not stay expanded when you click on a node

alternating row colors using an onclick event

treeview doesn't refresh (on postback) after removing a node

skin, can have onmouseover?

disable menu items,sub menu items, treeview items,subitems in master page based on certain conditions

sitemap spacing

css vs master pages

theme in master page

datepick popup in a gridview does not work in master page

worth using master pages on a static site?

skinning

changing font/forecolor on menus

  Privacy | Contact Us
All Times Are GMT