循环文本框和单选按钮列表
在我的网络表单中,我有 6 个文本框和 6 个单选按钮列表:
现在我想向访问者发送一封电子邮件,说明他们输入并单击该单选按钮列表的任何内容。电子邮件部分工作正常,但我应该如何将所有文本框和 RadioButtonLists 绑定到字符串中?
到目前为止我有这段代码:
protected void btnSubmit_Click1(object sender, EventArgs e)
{
//Specify senders gmail address
string SendersAddress = "[email protected]";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "[email protected]";
//Specify The password of gmial account u are using to sent mail(pw of [email protected])
const string SendersPassword = "test";
//Write the subject of ur mail
const string subject = "Testing Items";
List<string> st1 = GetTextBoxesAndRadioButtons();
//string body = GetTextBoxes();
try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, "Test");
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public List<String> returnList = new List<String>();
private List<string> GetTextBoxesAndRadioButtons()
{
string txt;
foreach (Control ctr in Page.Controls)
{
CallControls(ctr);
}
return returnList;
}
private void CallControls(System.Web.UI.Control p)
{
string returntext = "";
foreach (Control ctrlMain in p.Controls)
{
if (ctrlMain.HasControls())
{
/* Recursive Call */
CallControls(ctrlMain);
}
if (ctrlMain is TextBox)
{
TextBox txt;
txt = (TextBox)FindControl(ctrlMain.ID);
returnList.Add(txt.Text);
}
else if (ctrlMain is RadioButton)
{
RadioButton rad;
rad = (RadioButton)FindControl(ctrlMain.ID);
returnList.Add(rad.Text);
}
}
}
这是 UI 的标记:
<tr>
<td class="style4">
<asp:Label ID="Label1" runat="server" Text="1. "></asp:Label>
<asp:TextBox ID="tbShoppingList1" runat="server"></asp:TextBox>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" Height="16px"
RepeatDirection="Horizontal" Width="772px">
<asp:ListItem>CVS</asp:ListItem>
<asp:ListItem>Food Lion</asp:ListItem>
<asp:ListItem>Home Depot</asp:ListItem>
<asp:ListItem>Lowe`s</asp:ListItem>
<asp:ListItem>Publix</asp:ListItem>
<asp:ListItem>Target</asp:ListItem>
<asp:ListItem>Walgreens</asp:ListItem>
<asp:ListItem>WinDixie</asp:ListItem>
<asp:ListItem>Walmart</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
接下来的 5 个文本框和 RadioButtonList 也是如此!
这是行不通的。
“st1”未在其中存储任何内容。
In my webform I have 6 TextBoxes and 6 RadioButtonLists:
Now I want to send an email to the visitor that whatever they have entered and clicked on that RadioButtonLists. Email part is working fine, but how should I bind my all Textboxes and RadioButtonLists into a String?
I have this code so far:
protected void btnSubmit_Click1(object sender, EventArgs e)
{
//Specify senders gmail address
string SendersAddress = "[email protected]";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "[email protected]";
//Specify The password of gmial account u are using to sent mail(pw of [email protected])
const string SendersPassword = "test";
//Write the subject of ur mail
const string subject = "Testing Items";
List<string> st1 = GetTextBoxesAndRadioButtons();
//string body = GetTextBoxes();
try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, "Test");
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public List<String> returnList = new List<String>();
private List<string> GetTextBoxesAndRadioButtons()
{
string txt;
foreach (Control ctr in Page.Controls)
{
CallControls(ctr);
}
return returnList;
}
private void CallControls(System.Web.UI.Control p)
{
string returntext = "";
foreach (Control ctrlMain in p.Controls)
{
if (ctrlMain.HasControls())
{
/* Recursive Call */
CallControls(ctrlMain);
}
if (ctrlMain is TextBox)
{
TextBox txt;
txt = (TextBox)FindControl(ctrlMain.ID);
returnList.Add(txt.Text);
}
else if (ctrlMain is RadioButton)
{
RadioButton rad;
rad = (RadioButton)FindControl(ctrlMain.ID);
returnList.Add(rad.Text);
}
}
}
Here is the markup for UI:
<tr>
<td class="style4">
<asp:Label ID="Label1" runat="server" Text="1. "></asp:Label>
<asp:TextBox ID="tbShoppingList1" runat="server"></asp:TextBox>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" Height="16px"
RepeatDirection="Horizontal" Width="772px">
<asp:ListItem>CVS</asp:ListItem>
<asp:ListItem>Food Lion</asp:ListItem>
<asp:ListItem>Home Depot</asp:ListItem>
<asp:ListItem>Lowe`s</asp:ListItem>
<asp:ListItem>Publix</asp:ListItem>
<asp:ListItem>Target</asp:ListItem>
<asp:ListItem>Walgreens</asp:ListItem>
<asp:ListItem>WinDixie</asp:ListItem>
<asp:ListItem>Walmart</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
Similarly for next 5 textboxes and RadioButtonLists!
This is not working.
'st1' is not storing anything in it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该对两个控件都有效。您还可以用两种不同的方法分离逻辑。这会递归地查找 page.controls 中的所有文本框和单选按钮。将 returnList 变量声明为全局变量(对两种方法都可用)。
This should do it for both the controls. you can also separate out logic in two different methods. This recursively finds all textboxes and radiobuttons withing page.controls. Declare returnList variable as global (available to both methods).
将 ctr 转换为 TextBox
或者更好:
最后一个可以避免同时调用 is/as。
Cast ctr to TextBox
Or better:
with the last you avoid calling both is/as.
您需要将 ctr 转换为文本框,然后访问“Text”属性而不是调用“ToString”..尝试:
干杯,
中电公司
you need to cast the ctr as a textbox then access the "Text" property rather than calling "ToString".. try:
Cheers,
CEC