How to add the date selected to an ASP.net form with text boxes ? / v 3,5

Matt Lee 40 Reputation points
2024-10-03T18:59:32.04+00:00

hi,

I an using a simple asp,net form to get the name, number and email address from my web page. It works fine but there is also a JS date picker which looks good and I am happy with it. My question is how do I pass the selected date to the code behind which already captures the entry to the 3 text boxes. name, number, email.... Code below

aspx

                        <form class="contact-form" id="form1" runat="server">

                    <div class="input-group">


                    

   <asp:TextBox ID="name" placeholder="Name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'name'" class="input--style-2" type="text" runat="server"></asp:TextBox>

                    </div>

                    **<div class="row row-space">**

                        **<div class="col-2">**

                            **<div class="input-group">**

                                **<input class="input--style-2 js-datepicker" type="text" placeholder="Best visit date" name="birthday">**

                                **<i class="zmdi zmdi-calendar-note input-icon js-btn-calendar"></i>**

                            **</div>**

                        **</div>**

                                         <div class="row row-space">

                        <div class="col-2">

                            <div class="input-group">

   <asp:TextBox ID="number" placeholder="Best phone number" onfocus="this.placeholder = ''" onblur="this.placeholder = 'name'" class="input--style-2" type="text" runat="server"></asp:TextBox>

                            </div>

                        </div>

                    </div>

  <div class="row row-space">

                        <div class="col-2">

                            <div class="input-group">

   <asp:TextBox ID="email" placeholder="Best email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'name'" class="input--style-2" type="text" runat="server"></asp:TextBox>

                            </div>

                        </div>

                    </div>

                    <div class="p-t-30">
``` <asp:LinkButton ID="Button1" OnClick="Button1_Click" Class="btn btn--radius btn--green" runat="server">Submit</asp:LinkButton>

                </div>

            </form>

protected void Button1_Click(object sender, EventArgs e)

{


mailMessage.IsBodyHtml = true;

mailMessage.Subject = "xxxxxxxxxx" + " ";

mailMessage.Body = "<html><body>" +

 "<P style='font-family:Verdana; font-size: 1em;'>" +

"Client Email: " + email.Text + "<br>" + "<br>" +


here i want to add the selected date

  **"Sender DATE: " + number.Text + "<br>" +**

 "</p>" +x

    

       mailMessage.To.Add("xxxxxxxxxxxxxx");

mailMessage.From = new MailAddress("xxxxxxxxxxxx");

//send the message




//send the message

      System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("xxxxxxxxx");

System.Net.NetworkCredential User = new System.Net.NetworkCredential("xxxxxxxxxxxxx", "xxxxxxxxxxxx");

smtp.Credentials = User;

smtp.Send(mailMessage);

}

thank you

Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2024-10-03T19:54:32.5866667+00:00

    The birthday input is missing the runat="server" and ID attribute. So you can't access the input value by dot syntax.

    Rather you can fetch the value using the classic asp syntax in the Page_Load event on a PostBack.

    https://www.w3schools.com/ASP/coll_form.asp

        <input type="text" name="birthday" />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
    
            protected void Page_Load(object sender, EventArgs e)
            {
                string birthDay = Request.Form["birthday"];
                Label1.Text = birthDay;
            }
    
    

    If you want to use Web Forms binding then add runat="server" and an Id

        <input type="text" id="birthday" name="birthday" runat="server" />   
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
    
            protected void Page_Load(object sender, EventArgs e)
            {
                string birthDay = birthday.Value;
                Label1.Text = birthDay;
            }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-10-04T07:32:37.1433333+00:00

    Hi @Matt Lee,

    You need to add runat="server" and an ID attribute to your input.

     <input id="datepicker" class="input--style-2 js-datepicker" runat="server" type="text" placeholder="Best visit date" name="birthday" />
    

    Code behind

      "Sender DATE: " + datepicker.Value + "<br>" +
    

    User's image

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.