将文本输入到表单中并在下面的文本框中显示它

时间:2018-01-30 04:04:09

标签: c# html asp.net

所以我必须在C#中创建一个ASP.NET网站,其中包含一个用户表单,其中在文本框中填写了几条信息。我们的想法是让用户在表单中填写的内容显示在下面的文本框中。当用户键入时,文本应出现在文本框中,当输入更改时,应删除旧输入。我无法在下面的文本框中显示用户输入文本。我试图做的是在下面的表单和文本框中显示默认值,我成功地做到了这一点。因此,我有一个起点,但我不知道接下来该做什么。我包含了包含我的表单的.aspx页面和包含C#代码本身的.cs文件。

aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Models.aspx.cs" Inherits="Models" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Home Page</title>
</head>
<body>
    <form method ="POST" id="form1" runat="server">
  <p>This page is where the weather forecasting models can be found and this is provided by the tropical tidbits and pivotal weather websites.
The global weather models, along with the mesoscale weather models are included and these can be viewed by the public with no premium membership
needed.</p>
<h3>Registration Form</h3>
    <table>  
               <tr>  
                    <td>Name :</td>  
                    <td>  
                        <asp:TextBox ID="TextBox1" NAME="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>  
                    </td>  

               </tr>  
                <tr>  
                    <td>Address</td>  
                     <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>  
                </tr>  
                <tr>  
                    <td>City</td>  
                    <td>  
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
                    </td>  
                </tr>
                <tr>  
                    <td>State</td>  
                    <td>  
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>  
                    </td>  
                </tr>
                <tr>  
                    <td>Zip</td>  
                    <td>  
                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>  
                    </td>  
                </tr>
                <tr>  
                    <td>Phone</td>  
                    <td>  
                        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>  
                    </td>  
                </tr>
        </table>  
        <p>
            <asp:TextBox ID="TextBox8" runat="server" Height="106px" Width="250px" OnTextChanged="TextBox8_TextChanged" ReadOnly="True" TextMode="MultiLine"></asp:TextBox>
        </p>
    </form>
</body>
</html>

cs page

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Models : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string input1;
            input1 = TextBox1.Text;
            TextBox2.Text = "Kauffman Road";
            TextBox3.Text = "Annville";
            TextBox4.Text = "Pennsylvania";
            TextBox5.Text = "17003";
            TextBox6.Text = "7173892295";
            TextBox8.Text = "Name: " + TextBox1.Text + "\n" + "Address: " + TextBox2.Text + "\n" + "City: " + TextBox3.Text + "\n" + "State: " + TextBox4.Text + "\n" + "Zip: " + TextBox5.Text + "\n" + "Phone: " + TextBox6.Text;
        }

        protected void TextBox8_TextChanged(object sender, EventArgs e)
        {

        }



        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个:
aspx页面

//Add AutoPostBack="true"
<asp:TextBox ID="TextBox1" NAME="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

对其他控件做同样的事情。

cs page

    protected void Page_Load(object sender, EventArgs e)
    {
        //Add !IsPostBack
        if (!IsPostBack)
        {
            string input1;
            input1 = TextBox1.Text;
            TextBox2.Text = "Kauffman Road";
            TextBox3.Text = "Annville";
            TextBox4.Text = "Pennsylvania";
            TextBox5.Text = "17003";
            TextBox6.Text = "7173892295";
            TextBox8.Text = "Name: " + TextBox1.Text + "\n" + "Address: " + TextBox2.Text + "\n" + "City: " + TextBox3.Text + "\n" + "State: " + TextBox4.Text + "\n" + "Zip: " + TextBox5.Text + "\n" + "Phone: " + TextBox6.Text;
        }
    }
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox8.Text = "Name: " + TextBox1.Text + "\n" + "Address: " + TextBox2.Text + "\n" + "City: " + TextBox3.Text + "\n" + "State: " + TextBox4.Text + "\n" + "Zip: " + TextBox5.Text + "\n" + "Phone: " + TextBox6.Text;
    }

TextBox8的文本将在TextBox1的文本被更改时更改。