Search code examples
c#asp.nettextboxradiobuttonlist

Looping through TextBoxes and RadioButtonLists


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 = "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);
            }
        }


    }

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.


Solution

  • 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);
                    }
        }
    
    
    }