首页 > 解决方案 > 无法将表单值一页传递到另一个 asp.net

问题描述

我必须使用 asp.net 将值从一种形式传递给另一种形式 我只能传递给另一个 regno 并且只命名另一个值不能传递我得到错误缺失指令到目前为止我所尝试的内容我附在下面

<div class="row">
                       <div class="col-sm-4">
                           <div class="form-group">
                               <label>Reg No</label>    
                          <asp:TextBox ID="regno" runat="server"  class="form-control" required></asp:TextBox>
                       </div>

                        <div class="form-group">
                               <label>Name</label>
                           <asp:TextBox ID="name" runat="server"  class="form-control" required></asp:TextBox>
                        </div>

                        <div class="form-group">
                               <label>Address</label>
                               <input type="text" id="phone" name="phone" class="form-control" required />
                        </div>

                        <div class="form-group">
                              <label>Gender</label>
                              <asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Male" />
                              <asp:RadioButton ID="RadioButton2" runat="server" Text="FeMale" />
                           </div>

                             <div class="form-group">
                               <label>Interest</label>
                                  <asp:CheckBox ID="CheckBox1" runat="server" Text="Cricket" />
                                <asp:CheckBox ID="CheckBox2" runat="server" Text="Football" />
                                <asp:CheckBox ID="CheckBox3" runat="server" Text="Cinima" />
                           </div>

                            <div class="form-group">
                              <asp:Button ID="Button1" runat="server" Text="Submit" class="btn btn-success" OnClick="Button1_Click"  />
                           </div>



                       </div>

按钮事件

 protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("form2.aspx?regno=" +
            this.regno.Text + "&name=" +
            this.name.Text  + "&phone=" + 
            this.phone.Text ); this line i got error missing directive


        }

第二种形式

Label1.Text = Request.QueryString["regno"];
             Label2.Text = Request.QueryString["name"];
             Label3.Text = Request.QueryString["phone"];

标签: asp.net

解决方案


phone 似乎是普通输入而不是 TextBox(注意输入标签和小写 id)。此外,您在 id=phone 时请求 txtphone。

请注意,字符串数据应在通过查询字符串发送之前进行 Url 编码。

编辑:替换

    <input type="text" id="phone" name="phone" class="form-control" required />

    <asp:TextBox ID="phone" runat="server"  class="form-control" required></asp:TextBox>

编辑:对于复选框,使用类似

var interests = new List<string>();
if (chkCheckBox1.Checked)
    interests.Add(chkCheckBox1.Text);
if (chkCheckBox2.Checked)
    interests.Add(chkCheckBox2.Text);
if (chkCheckBox3.Checked)
    interests.Add(chkCheckBox3.Text);
var interestsString = string.Join(",", interests);

var sex = RadioButton1.Checked ? RadioButton1.Text : RadioButton1.Text;

Response.Redirect("form2.aspx" +
    "?regno=" + this.regno.Text + 
    "&name=" + this.name.Text + 
    "&phone=" + this.phone.Text +
    "&sex=" + HttpUtility.UrlEncode(sex) + 
    "&interest=" + HttpUtility.UrlEncode(interestsString) +


// if needed, decode with HttpUtility.UrlDecode(Request.QueryString["interest"]:
// var interests = HttpUtility.UrlEncode(interestsString);


推荐阅读