【问题标题】:Looping through TextBoxes and RadioButtonLists循环通过 TextBoxes 和 RadioButtonLists
【发布时间】:2011-04-12 06:14:41
【问题描述】:

在我的网络表单中,我有 6 个文本框和 6 个 RadioButtonList:

现在我想向访问者发送一封电子邮件,说明他们输入并点击了 RadioButtonLists。电子邮件部分工作正常,但我应该如何将所有文本框和 RadioButtonLists 绑定到字符串中?

到目前为止我有这个代码:

         protected void btnSubmit_Click1(object sender, EventArgs e)
    {
        //Specify senders gmail address
        string SendersAddress = "test@gmail.com";

        //Specify The Address You want to sent Email To(can be any valid email address)
        string ReceiversAddress = "test@gmail.com";

        //Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
        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 个文本框和 RadioButtonLists 也是如此!

这不起作用。

'st1' 里面没有存储任何东西。

【问题讨论】:

  • 不太明白你在这里问什么。
  • Page.Controls 不是控件的递归列表。与其比较对象,不如将 ctr.GetType() 与 typeof(TextBox) 进行比较。我也没有看到实例化 box 对象的意义?
  • 我会用 if 语句强行执行此操作,然后继续。 (我的拙见)
  • @Gabriel:我知道,但我想知道这个概念:)
  • @John:我更新了我的代码。希望你能从中得到一个要点。

标签: c# asp.net textbox radiobuttonlist


【解决方案1】:

这对两个控件都应该这样做。您还可以用两种不同的方法分离出逻辑。这会递归地找到所有带有 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);
                }
    }


}

【讨论】:

  • 所以基本上在我的 returnList 中我会输入所有文本框和单选按钮的文本?
  • 我试过 Priyank。但它甚至没有存储到 List 中。查看我的更新答案!
  • @Priyank:它给了我这个错误:对象引用未设置为对象的实例。在此:returntext = txt.Text;
  • 您是否采用了最新编辑的代码。我在这段代码中没有“returntext = txt.Text;”这样的行
  • 向代码添加一些验证 - 比如 findcontrol 不返回 null 或 txt.text/rad.text 不为 null。它最终应该会起作用。
【解决方案2】:

将 ctr 转换为文本框

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

或者更好:

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

最后你避免同时调用 is/as。

【讨论】:

    【解决方案3】:

    您需要将 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.
                }
             }
    

    干杯, 中电联

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2015-05-21
      相关资源
      最近更新 更多