asp.net - 无法将表单值一页传递到另一个 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"];
解决方案
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);
推荐阅读
- reactjs - 为什么 Sublime Text 3 在 React 的这一行末尾添加一个逗号?
- c++ - 在可变参数模板类中派生(虚拟)函数参数
- terraform - 使用 Terraform 引用同级目录中的 Lambda 源文件
- html - 按钮加载 react-hooks-nextjs
- json - json 日志记录中的 Fluentd 时间字段处理
- javascript - 单击复选框后按钮未打开另一个 HTML 文件
- json - 如何使用 Serilog TCP Sink 将日志事件的消息格式化为 Json
- reactjs - 如何自动将文本动态添加到图像并导出?
- reactjs - 除非我使用超时,否则 React + Jest + Enzyme 表单 onSubmit 测试失败
- cellular-automata - 预测元胞自动机的第 n 个状态