467,915 Members | 1,886 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

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
  • viewed: 8078
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by iturner100 | last post: by
3 posts views Thread by salportaro | last post: by
1 post views Thread by Steve | last post: by
6 posts views Thread by =?Utf-8?B?U3RlcGhlbiBIYXRmaWVsZA==?= | last post: by
8 posts views Thread by Mort Strom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.