E
encoad
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
ropDownList 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
ropDownList>
<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("MainContent");
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
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

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

<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("MainContent");
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