473,403 Members | 2,323 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,403 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 8273
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.