How to autofill a TextBox that is located on WebForm2 by clicking a button on the main WebForm

Bojan Serafimovski 21 Reputation points
2021-06-06T16:11:45.867+00:00

I am making an asp.net website and I have a button on the main WebForm that it is hyperlinked with WebForm2.
The WebForm2 that opens is a typical contact form that contains some text boxes, drop down lists and a button that sends an e-mail with the information that I entered. But because I am making an e-commerce website, depending on which button(on which product) is clicked on the main WebForm, I need to autofill the TextBox that contains the "Product Name" on the WebForm2.
Firstly, I had a JavaScript modal that was opened when the button was clicked, and I made independent forms with different value on the TextBox depending on the Product Name, but I could not figure out how to make the modal functional to send an e-mail, simply the information that was entered could not be proceed an the
e-mail that I get was only the mail_message text (ex. Name: , Surname: and Telephone Number: were blank) but not the text that was filled in the form.

So my question is, how to make the button on the main WebForm autofill the TextBox on the WebForm2 that contains the Product Name?

In case it matters, on the WebForm2 the code that sends me an e-mail is:

protected void button_Click(object sender, EventArgs e)
{
try
{
var from = "email";
var to = "secondemail";
const string Password = "password";
string mail_subject = txt_subject.Text.ToString();
string mail_message = "Your Name: " + txt_name.Text + "\n";
mail_message += "Your Surname: " + txt_surname.Text + "\n";
mail_message += "Your telephone number: " + txt_phone.Text + "\n";
mail_message += "Your address: " + txt_address.Text + "\n";
mail_message += "Your e-mail: " + txt_email.Text + "\n";
mail_message += "The product name: " + txt_product.Text + "\n";
mail_message += "Payment method: " + dropdownlist.SelectedValue + "\n";

        var smtp = new SmtpClient();
        {
            smtp.Host = "smtp@mail";
            smtp.Port = port;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(from, Password);
            smtp.Timeout = 20000;
        }

        smtp.Send(from, to, mail_subject, mail_message);

        confirm.Text = "Thank you for your order, our team will deliver the product you ordered at your home address as soon as possible.";

        txt_subject.Text = "";
        txt_name.Text = "";
        txt_surname.Text = "";
        txt_phone.Text = "";
        txt_address.Text = "";
        txt_email.Text = "";
        txt_subject.Text = "";
        txt_product.Text = "";
        dropdownlist.SelectedIndex = -1;
    }
    catch (Exception)
    {
        confirm.Text = "The order can't be proceed at the moment, please try again later.";
        confirm.ForeColor = Color.Red;
    }

Cheers, grateful for any help.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,270 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,066 Reputation points
    2021-06-07T01:55:58.77+00:00

    Hi @BojanSerafimoski-5906 ,
    As far as I think,there are two ways.One is Request.QueryString. Another,you could use Session.
    Request.QueryString:

    The First WebForm:

    protected void Button1_Click(object sender, EventArgs e)     
    {    
        Response.Redirect("default2.aspx ?firstname=" + Label1.Text + "&lastname=" + Label2.Text);    
    }    
    

    The Second WebForm:

    protected void Page_Load(object sender, EventArgs e)     
    {    
        string firstname = Request.QueryString["firstname"];    
        string lastname = Request.QueryString["lastname"];    
        TextBox1.Text = "welcome" + firstname + " " + lastname;    
    }   
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.

1 additional answer

Sort by: Most helpful
  1. Bojan Serafimovski 21 Reputation points
    2021-06-16T09:06:36.917+00:00

    Hello again @Yijing Sun-MSFT , I came across another problem now.
    So I used this method and it worked, but now I need to autofill another TextBox that includes the price on the WebForm2 on the same button.
    So I tried the same principle, I included another label on WebForm1, and on the same button I redirected it to to the WebForm2 but it doesn't work, it does not fill the TextBox with the price, here is the code:

    WebForm1:

        protected void Button1_Click(object sender, EventArgs e)  
        {  
            Response.Redirect("ShopNowPage.aspx?productname=" + Label1.Text);  
            Response.Redirect("ShopNowPage.aspx?productprice=" + Label2.Text);  
        }  
    

    WebForm2:

        protected void Page_Load(object sender, EventArgs e)  
        {  
            string productName = Request.QueryString["productname"];  
            txt_productname.Text = productName;  
    
            string productPrice = Request.QueryString["productprice"];  
            txt_productprice.Text = productPrice;  
         }   
    

    Any ideas how to solve this? Thank you.

    0 comments No comments