Masterpages and FindControl

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: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("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
 
D

DaanishRumani

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

////////////////////////////////////
//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!!!!
 
E

encoad

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top