【发布时间】:2010-10-30 05:28:31
【问题描述】:
我有一个简单的任务 - 在复选框状态更改时更改页面链接 - 但我是 ASP.NET 新手,遇到了一些麻烦。
我可以使用 HtmlControl 和 JavaScript 做同样的事情:
<script type="text/javascript" language="javascript">
function checkbox_onChanged(checked) {
if (checked) {
document.location = '?type=request_in&show=all';
}
else {
document.location = '?type=request_in&show=unapproved';
}
}
function checkbox_onLoad(checkbox) {
checkbox.checked = true;
}
</script>
<form action="" method="get">
<input type="checkbox" name="checkbox"
onload="checkbox_onLoad(this)"
onchange="checkbox_onChanged(this.checked)" />Show all
</form>
但我想对用户隐藏它。所以我这样做:
<asp:CheckBox runat="server" ID="check"
OnCheckedChanged="check_CheckedChanged"
AutoPostBack="True" Text="Show all" />
protected void check_CheckedChanged(object sender, EventArgs e)
{
Response.Redirect(String.Format("{0}?type=request_in&show={1}", Request.Path,
checkViewRequestIn.Checked ? "all" : "unapproved"));
}
protected void Page_Load(object sender, EventArgs e)
{
var show = Request["show"];
if (!String.IsNullOrEmpty(show) && String.Equals(show, "all"))
{
checkViewRequestIn.Checked = true;
}
}
但似乎加载时检查状态更改会再次引发事件,并且复选框始终处于选中状态!
Ans another question - 有没有其他方法可以重定向到同一页面而不给出它的文件名?我的意思是像在 JavaScript 中一样 - 只提供需要的变量?
【问题讨论】:
标签: .net asp.net checkbox postback