How to access DropDownList info and values that aren't known until runtime?

G

Guest

Hi all,

Longtime ASP developer getting feet wet with ASP.NET, has banged head on
this problem for many hours, go easy. :)

The Code Project has a nice article on making a "self configuring form
mailer web control." Basically, you apply this control to any form with
ASP.NET textbox server controls on it and it turns the form into something
that's automatically mailed out to an e-mail you specify. Read all about it
here:
http://www.codeproject.com/aspnet/SelfConfigFormMailer.asp

The code works great for textboxes. The author of the article leaves it as
"an exercise for the reader" to configure the program for other types of
controls. The relevant section of code reads as follows:

{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
if (textBox.Text.Length > 0)
{
if (textBox.ID.ToLower().EndsWith("subject"))
{
subject = textBox.Text; // subject is added as a mail header later
in the code
}
else if (textBox.ID.ToLower().EndsWith("emailaddress"))
{
replyTo = textBox.Text; // add a reply-to header, and
// append the to the text of the message
messageText.Append(mailTemplate.getTemplateText(textBox.ID,
textBox.Text));
}
else
{ // general case, just append to the email body
messageText.Append(mailTemplate.getTemplateText(textBox.ID,
textBox.Text));
}
}
}
// >>> add more types of controls within else if clauses here <<<
}

So what you have going on is the script generates an e-mail string at
runtime using the IDs and Text that are specified by the form developer.

I want the script to work with DropDownLists. I can't figure out how to make
it access the values of the selected listitems. Most of the dropdown scripts
I've seen around the net presume that the name of the parent control
(DropDownList) is specified prior to runtime, but wiring this information in
defeats the point of this generic script.

I've tried a +lot+ of variations, e.g.:
messageText.Append(mailTemplate.getTemplateText(DropDownList.ID.Selecteditem
..Value, DropDownList.ID.Selecteditem.Text))

No joy so far. Can someone help out with what's probably an easy question?

Thanks,

Ken Fine
(e-mail address removed)
http://www.ideapod.org
 
A

aa7im

I don't have time to look at the article you referenced but based on
the above code snippet you would need something like:

if(control is DropDownList)
{
DropDownList ddList = (DropDownList)control;

messageTest.Append(mailTemplate.getTemplateText(ddList.ID,ddList.SelectedItem.Text);
}

this code would go after or before the "if(control is TextBox)" code
blocks....
 
G

Guest

Thanks, but unfortunately this change didn't work. Any other ideas for
making the preceding code work? Appreciate your time.
 
G

Guest

After fighting a bunch with this, I finally got it going with your help.
Many thanks.

New to VS.NET and making some +really+ dumb newbie errors with it. Gotta
start somewhere. :)
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top