循环文本框和单选按钮列表

发布于 2024-10-31 19:15:19 字数 4551 浏览 4 评论 0原文

在我的网络表单中,我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生来就爱笑 2024-11-07 19:15:19

这应该对两个控件都有效。您还可以用两种不同的方法分离逻辑。这会递归地查找 page.controls 中的所有文本框和单选按钮。将 returnList 变量声明为全局变量(对两种方法都可用)。

 public List<String> returnList = new List<String>();

private List<string> GetTextBoxesAndRadioButtons()
{
    string txt;

    foreach (Control ctr in Page.Controls)
    {

       CallControls(ctr);

    }

string finalstring = string.Join(",", returnList.ToArray());
    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 = (TextBox)ctrlMain;
                    //txt = (TextBox)FindControl(ctrlMain.UniqueID);
                    returnList.Add(txt.Text);
                }
                else if (ctrlMain is RadioButtonList)
                {
                    RadioButtonList rad = (RadioButtonList)ctrlMain;
                    //rad = (RadioButtonList)FindControl(ctrlMain.UniqueID);
                    returnList.Add(rad.SelectedValue);
                }
    }


}

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).

 public List<String> returnList = new List<String>();

private List<string> GetTextBoxesAndRadioButtons()
{
    string txt;

    foreach (Control ctr in Page.Controls)
    {

       CallControls(ctr);

    }

string finalstring = string.Join(",", returnList.ToArray());
    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 = (TextBox)ctrlMain;
                    //txt = (TextBox)FindControl(ctrlMain.UniqueID);
                    returnList.Add(txt.Text);
                }
                else if (ctrlMain is RadioButtonList)
                {
                    RadioButtonList rad = (RadioButtonList)ctrlMain;
                    //rad = (RadioButtonList)FindControl(ctrlMain.UniqueID);
                    returnList.Add(rad.SelectedValue);
                }
    }


}
断爱 2024-11-07 19:15:19

将 ctr 转换为 TextBox

if (ctr is TextBox)
            {
                textBoxes.Add((ctr as TextBox).Text);  
            }

或者更好:

var tb = ctr as TextBox;
if( null != tb )
       textBoxes.Add(cb.Text); 

最后一个可以避免同时调用 is/as。

Cast ctr to TextBox

if (ctr is TextBox)
            {
                textBoxes.Add((ctr as TextBox).Text);  
            }

Or better:

var tb = ctr as TextBox;
if( null != tb )
       textBoxes.Add(cb.Text); 

with the last you avoid calling both is/as.

酒绊 2024-11-07 19:15:19

您需要将 ctr 转换为文本框,然后访问“Text”属性而不是调用“ToString”..尝试:

 List<string> textboxes = new List<string>();

         foreach (Control c in this.Controls)
         {
            if (c is TextBox)
            {
               textboxes.Add(((TextBox)c).Text); //oops.. forgot a set of parenthesis had to come back and edit.
            }
         }

干杯,
中电公司

you need to cast the ctr as a textbox then access the "Text" property rather than calling "ToString".. try:

 List<string> textboxes = new List<string>();

         foreach (Control c in this.Controls)
         {
            if (c is TextBox)
            {
               textboxes.Add(((TextBox)c).Text); //oops.. forgot a set of parenthesis had to come back and edit.
            }
         }

Cheers,
CEC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文