473,221 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Masterpages and FindControl

Hi everyone,

I'm slowly going mad with Masterpages and the FindControl. After a
couple days of figuring out how to use the FindControl command from
within a Masterpage, I still can't explain why this code does not
function. As you can see if you compile it with 2005, clicking
"Button" will achieve the desired result of displaying the contents of
the textbox, however the test(); function called from Page_Load does
not do anything. (for example when I'm autoposting back)

Any help would be appreciated. Below is some test code that I wrote.

test2.aspx
---------------

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2_aspx"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
Runat="Server">
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell Width="100">Length</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
</asp:Content>

test2.aspx.cs
--------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class test2_aspx : System.Web.UI.Page
{
int numberofboxes;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
numberofboxes = 1;
}
else
{
numberofboxes = Convert.ToInt32(DropDownList1.Text);
}
GenerateTextBoxes();
test();
}

void GenerateTextBoxes()
{
TableCell tcell;
TableRow trow;

for (int i = 1; i <= numberofboxes; i++)
{
trow = new TableRow();
tcell = new TableCell();

TextBox tbtest;
tbtest = new TextBox();
tbtest.ID = "hello"+ i.ToString();

tcell.Controls.Add(tbtest);
trow.Cells.Add(tcell);

Table1.Rows.Add(trow);
}
}

protected void test()
{
ContentPlaceHolder content;
content =
(ContentPlaceHolder)Page.Master.FindControl("MainC ontent");

for (int i = 1; i <= numberofboxes; i++)
{
TextBox tb = content.FindControl("hello" + i.ToString()) as
TextBox;
Response.Write(tb.Text);
}
}

protected void Button1_Click(object sender, EventArgs e)
{
ContentPlaceHolder content;
content = (ContentPlaceHolder)
Page.Master.FindControl("MainContent");
for (int i = 1; i <= numberofboxes; i++)
{
TextBox tb = content.FindControl("hello" + i.ToString()) as
TextBox;
Response.Write(tb.Text);
}

}
}
Thanks,
Nicholas

Jun 26 '06 #1
2 8267
Why dont you try to see if the textbozes have been created before you
print those values??

protected void test()
{
ContentPlaceHolder content;
content =
(ContentPlaceHolder)Page.Master.FindControl("MainC ontent");

////////////////////////////////////
//add a print statement that prints the number of textboxes!!!
Response.Write(numberofboxes);
/////////////////////////////////
for (int i = 1; i <= numberofboxes; i++)
{
TextBox tb = content.FindControl("hello" + i.ToString()) as
TextBox;
Response.Write(tb.Text);
}
}

if the value of 'numberofboxes is zero then you are calling test before
the textboxes have been created/initialized.

I dont know the solution to you problem but i'am just tryin to help!!!!

en****@gmail.com wrote:
Hi everyone,

I'm slowly going mad with Masterpages and the FindControl. After a
couple days of figuring out how to use the FindControl command from
within a Masterpage, I still can't explain why this code does not
function. As you can see if you compile it with 2005, clicking
"Button" will achieve the desired result of displaying the contents of
the textbox, however the test(); function called from Page_Load does
not do anything. (for example when I'm autoposting back)

Any help would be appreciated. Below is some test code that I wrote.

test2.aspx
---------------

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2_aspx"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
Runat="Server">
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell Width="100">Length</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
</asp:Content>

test2.aspx.cs
--------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class test2_aspx : System.Web.UI.Page
{
int numberofboxes;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
numberofboxes = 1;
}
else
{
numberofboxes = Convert.ToInt32(DropDownList1.Text);
}
GenerateTextBoxes();
test();
}

void GenerateTextBoxes()
{
TableCell tcell;
TableRow trow;

for (int i = 1; i <= numberofboxes; i++)
{
trow = new TableRow();
tcell = new TableCell();

TextBox tbtest;
tbtest = new TextBox();
tbtest.ID = "hello"+ i.ToString();

tcell.Controls.Add(tbtest);
trow.Cells.Add(tcell);

Table1.Rows.Add(trow);
}
}

protected void test()
{
ContentPlaceHolder content;
content =
(ContentPlaceHolder)Page.Master.FindControl("MainC ontent");

for (int i = 1; i <= numberofboxes; i++)
{
TextBox tb = content.FindControl("hello" + i.ToString()) as
TextBox;
Response.Write(tb.Text);
}
}

protected void Button1_Click(object sender, EventArgs e)
{
ContentPlaceHolder content;
content = (ContentPlaceHolder)
Page.Master.FindControl("MainContent");
for (int i = 1; i <= numberofboxes; i++)
{
TextBox tb = content.FindControl("hello" + i.ToString()) as
TextBox;
Response.Write(tb.Text);
}

}
}
Thanks,
Nicholas


Jun 26 '06 #2
Hi Daanish,

Since I'm using a dropdownlist, there is always a value select so I'm
not having any trouble with the number of boxes variable. I appreciate
the suggestion though!

Nicholas
DaanishRumani wrote:
Why dont you try to see if the textbozes have been created before you
print those values??

protected void test()
{
ContentPlaceHolder content;
content =
(ContentPlaceHolder)Page.Master.FindControl("MainC ontent");

////////////////////////////////////
//add a print statement that prints the number of textboxes!!!
Response.Write(numberofboxes);
/////////////////////////////////
for (int i = 1; i <= numberofboxes; i++)
{
TextBox tb = content.FindControl("hello" + i.ToString()) as
TextBox;
Response.Write(tb.Text);
}
}

if the value of 'numberofboxes is zero then you are calling test before
the textboxes have been created/initialized.

I dont know the solution to you problem but i'am just tryin to help!!!!


Jun 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: iturner100 | last post by:
Hi, I've been struggling with this one for a couple of hours without much joy. Basically, I've got a set of nested masterpages (3 as it happens). I'm dynamically generating a new page in code...
5
by: Nick Wouters | last post by:
Dear All In Classic ASP I used CSS for ALL layout. Now in ASP.NET version 2 I am testing out Masterpages as they come in very handy. It seems like it is replacing CSS for layout but what is...
3
by: salportaro | last post by:
Question: Is there a way of manipulating a master page element from a content page? For example, if I have lblStatus on the masterpage and I call anypage.aspx, can I set lblStatus from anypage?...
3
by: j-in-uk | last post by:
I have 3 pages Site.master, Album.aspx and ContinentsMenu.ascx. Continents is a usercontrol placed in Albums. In Album.aspx.cs I need to access a property from the Continents usercontrol and is...
23
by: LvBohemian | last post by:
I am playing around with creating some menus dynamically, and they create fine and show up ok when I want them to; but when I select a menu NavigateUrl at run time, the applicable url shows up...
1
by: Steve | last post by:
After a few hours of trial and error I have reached the following conclusions, can you please tell me if I am right: I have 2 aspx pages both with the same master page and I wish to pass values...
2
by: Goofy | last post by:
I was wondering how to go about setting the keywords for pages whichg derive from MasterPages, and then I thought I know, lets put a runat=server on the keywords meta tag and set the content. ...
6
by: =?Utf-8?B?U3RlcGhlbiBIYXRmaWVsZA==?= | last post by:
I have two masterpages in a web application. One is used for the login and logout pages. The other is used for all other pages in the application. The difference between the two masterpages is...
8
by: Mort Strom | last post by:
Right now the header of my master page contains all of the CSS styles for all of the pages that might be loaded in my ContentPlaceHolder. The problem is that my <styletag is getting too large to...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.