Placeholder and Dynamic controls - help!

P

Phuff

I need to add controls on a button click. I would prefer to do this in
the page load or init functions, however due to the nature of the page
I have to add them later on. When I try to enumerate through the place
holder's control collection is always at 0. How can I get to the text
of these texbox controls that I added?

Here's the coded with email information @@@@@'d out.

ADDING THE CONTROLS:

for (int i = 0; i < lister.Length; i++)
{
Label tmpLabel = new Label();
TextBox tmpTxt = new TextBox();

tmpLabel.Attributes.Add("runat", "server");
tmpLabel.ID = "lblSub" + i.ToString();
tmpLabel.Text = "Subject for " + lister;

tmpTxt.Attributes.Add("runat", "server");
tmpTxt.ID = lister;
tmpTxt.Width = Unit.Pixel(275);
tmpTxt.Text = lister + " - " + bldSite.Value;

subjPlace.Controls.Add(tmpLabel);
subjPlace.Controls.Add(tmpTxt);
subjPlace.Controls.Add(new LiteralControl("<br />"));
}

ENUMERATING THROUGH:

foreach (Control c in form1.Controls)
{
if (c.GetType().ToString() ==
"System.Web.UI.WebControls.PlaceHolder")
{
foreach (Control ctr in c.Controls)
{
if (ctr.GetType().ToString() ==
"System.Web.UI.WebControls.TextBox")
{
string jobStr = ((TextBox)ctr).ID;
//string subject = subjPlace.;
string userEmail =
Request.LogonUserIdentity.Name.Replace("@@@\\", "") + "@@@@@@";

string[] parameters = new string[5];
//TODO: Fill parameters with data
parameters[0] =
Request.QueryString["quote"];
parameters[1] =
Request.QueryString["order"];
parameters[2] =
ds.Tables[0].Rows[0]["CustNam"].ToString();
parameters[3] =
ds.Tables[0].Rows[0]["QuotedBy"].ToString();
parameters[4] =
ds.Tables[0].Rows[0]["BldSite"].ToString();

string strEmailText = string.Format("------
Release to Shop Information ------\n\n"
+
"Customer Name : {2}\n"
+
"Quote # : {0}\n"
+
"Order # : {1}\n"
+
"Quoted By : {3}\n"
+
"Build Site : {4}\n\n", parameters);

strEmailText += "--------------- Job Files
---------------\n\n";

for (int i = 0; i < files.Length; i++)
{
if (files.Contains(jobStr))
{
strEmailText += files + "\n";
}
}

System.Net.Mail.MailMessage Mail = new
System.Net.Mail.MailMessage();
System.Net.Mail.MailAddress add = new
System.Net.Mail.MailAddress(userEmail);
Mail.From = add;
Mail.To.Add("@@@@@@@@");

System.Net.Mail.SmtpClient mailer = new
System.Net.Mail.SmtpClient("email");

try
{
Mail.Subject = ((TextBox)ctr).Text;
Mail.Body = strEmailText;
mailer.Send(Mail);

}
catch (Exception ex)
{
writeTrace(ex.Message + "[METHOD:
emailSTS] (" + DateTime.Now + ")", "ERROR");
}
}
}
}

}
}
catch (Exception ex)
{
writeTrace(ex.Message + "[METHOD: emailSTS] (" +
DateTime.Now + ")", "ERROR");
}
 
N

nschiffelbein

What you can do is declare an instance of each type of object. Then
using the FindControl function you can bind each instance to the
control itself and then retrieve the data within it. So, for example:

'Create an instance of the object
Dim i as TextBox()

'Bind the control the your instance in memory
'(placeHolder would be the name of your Place Holder object)
i = placeHolder.FindControl("txtMyTextBox")

'Output the TextBoxes contents
Response.Write(i.Text)

I'm not sure I got the syntax entirely correct, but something along
those lines using the FindControl function should do what your looking
for.

Nick
 
P

Phuff

Nick,

Thanks for your response. I gave that a try but no go. For some
reason the place holder's control collection is empty. It's like the
controls are not being added to the view state. This is some what
frustrating. I'm sure I could have used some other approaches, but
isn't quick and simple supposed to be the credo of .Net? What's going
on here? Anyway, I'll keep digging.

I guess I'll create a hidden field, those are working correctly, and
use javascript to fill the hidden field value for the client onchange
event of the textbox. Then I'll just parse the hidden field's value.
Quite the annoying workaround, but what are you going to do, right?

Thanks,

Paul
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top