Post 后未选中复选框返回
Checkbox not Checked after Post Back
我正在处理一些遗留的内联 Web 窗体代码。我添加了一个默认设置的复选框。问题是当复选框未选中时,它仍然会在提交表单时翻转为已选中。每个人都没有页面事件处理程序。
在 JavaScript 中,如果我设置警报,它会将值显示为空白,这在未选中复选框时是正确的。但是,当提交表单时,复选框总是恢复到选中状态。
标记:
<input type="hidden" id="inYard" name="inYard" runat="server" />
<input type='checkbox' id="excludeYard" name='excludeYard' onclick="CheckYardSelection()"
runat="server" />Include Items in Yard
JavaScript:
function CheckYardSelection()
{
var yardCheckbox = document.getElementById('excludeYard');
if (!yardCheckbox.checked)
document.getElementById('inYard').value = "";
else
document.getElementById('inYard').value = "Y";
}
C#:
<script runat="server">
...
if(!String.IsNullOrEmpty(Request.Form["inYard"]))
{
if (Request.Form["inYard"].ToString() == "Y")
{
excludeYard.Value = Request.Form["inYard"].ToString();
excludeYard.Checked = true;
}
}
...
</script>
您没有 post 完整的代码,但如果您以这种方式实现所有内容,就不会再遇到此问题:
aspx 文件:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebForms._Default" %>
<script src="Default.js" language="javascript" type="text/javascript"></script>
<form runat="server">
<input type="hidden" id="inYard" name="inYard" runat="server" />
<input type='checkbox' id="excludeYard" name='excludeYard' onclick="CheckYardSelection()" runat="server" />
<input type="submit" value="Post" />
</form>
隐藏代码:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Request.Form["inYard"]))
{
if (Request.Form["inYard"].ToString() == "Y")
{
excludeYard.Value = Request.Form["inYard"].ToString();
excludeYard.Checked = true;
// query += " AND IC.NewWellPad <> 'YARD'";
}
}
}
}
Default.js
function CheckYardSelection() {
var yardCheckbox = document.getElementById('excludeYard');
if (!yardCheckbox.checked)
document.getElementById('inYard').value = "";
else
document.getElementById('inYard').value = "Y";
}
我正在处理一些遗留的内联 Web 窗体代码。我添加了一个默认设置的复选框。问题是当复选框未选中时,它仍然会在提交表单时翻转为已选中。每个人都没有页面事件处理程序。
在 JavaScript 中,如果我设置警报,它会将值显示为空白,这在未选中复选框时是正确的。但是,当提交表单时,复选框总是恢复到选中状态。
标记:
<input type="hidden" id="inYard" name="inYard" runat="server" />
<input type='checkbox' id="excludeYard" name='excludeYard' onclick="CheckYardSelection()"
runat="server" />Include Items in Yard
JavaScript:
function CheckYardSelection()
{
var yardCheckbox = document.getElementById('excludeYard');
if (!yardCheckbox.checked)
document.getElementById('inYard').value = "";
else
document.getElementById('inYard').value = "Y";
}
C#:
<script runat="server">
...
if(!String.IsNullOrEmpty(Request.Form["inYard"]))
{
if (Request.Form["inYard"].ToString() == "Y")
{
excludeYard.Value = Request.Form["inYard"].ToString();
excludeYard.Checked = true;
}
}
...
</script>
您没有 post 完整的代码,但如果您以这种方式实现所有内容,就不会再遇到此问题:
aspx 文件:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebForms._Default" %>
<script src="Default.js" language="javascript" type="text/javascript"></script>
<form runat="server">
<input type="hidden" id="inYard" name="inYard" runat="server" />
<input type='checkbox' id="excludeYard" name='excludeYard' onclick="CheckYardSelection()" runat="server" />
<input type="submit" value="Post" />
</form>
隐藏代码:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Request.Form["inYard"]))
{
if (Request.Form["inYard"].ToString() == "Y")
{
excludeYard.Value = Request.Form["inYard"].ToString();
excludeYard.Checked = true;
// query += " AND IC.NewWellPad <> 'YARD'";
}
}
}
}
Default.js
function CheckYardSelection() {
var yardCheckbox = document.getElementById('excludeYard');
if (!yardCheckbox.checked)
document.getElementById('inYard').value = "";
else
document.getElementById('inYard').value = "Y";
}