C# 更新面板激发时未保存复选框状态
我有一个divC# 更新面板激发时未保存复选框状态,c#,javascript,asp.net,updatepanel,C#,Javascript,Asp.net,Updatepanel,我有一个divid=activitydiv,我动态加载它的内容 内容是复选框 我有一个更新面板,每30秒启动一次 我的问题 当“更新”面板触发时,复选框返回默认状态,即未选中 我会给你我所有的密码。实际上它是非常简单的代码您可以在visual studio中复制粘贴它,它将正常工作 WebForm4.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="TestD
id=activitydiv
,我动态加载它的内容
内容是复选框
我有一个更新面板,每30秒启动一次
我的问题
当“更新”面板触发时,复选框返回默认状态,即未选中
我会给你我所有的密码。实际上它是非常简单的代码您可以在visual studio中复制粘贴它,它将正常工作
WebForm4.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="TestDropdownChecklist.WebForm4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="StyleSheet1.css"/>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick">
</asp:Timer>
<div id="campaignDiv" runat="server">
<ul>
</ul>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
<asp:UpdatePanel ID="upStuff" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="campaignDiv">
<asp:CheckBoxList runat="server" RepeatLayout="UnorderedList" ID="myCheckboxList" TextAlign="Left">
</div>
</asp:CheckBoxList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
</Triggers>
</asp:UpdatePanel>
请注意,我已经在使用runat服务器
。也许我应该以另一种方式动态添加复选框
编辑
在通过互联网准备之后,我发现我可能需要像这样将复选框添加到我的更新面板中
UpdatePanel1.ContentTemplateContainer.Controls.Add(动态控件)代码>
但问题是,在我的例子中,复选框是字符串而不是对象。对吧?
非常感谢你的帮助
为伟大的用户Schadensbegrenzer编辑
你的回答真的很有效,我感谢你,但我还是没有解决这个问题,
您的代码生成了这个呈现的html
<ul id="campaignDiv"> <li><input id="campaignDiv_0" type="checkbox" name="campaignDiv$0" checked="checked" value="default"><label for="campaignDiv_0">default</label></li> <li><input id="campaignDiv_1" type="checkbox" name="campaignDiv$1" value="sales"><label for="campaignDiv_1">sales</label></li> <li><input id="campaignDiv_2" type="checkbox" name="campaignDiv$2" value="direct"><label for="campaignDiv_2">direct</label></li> </ul>
在您的代码中,我将campaginDiv设置为asp:CheckBoxList
的id,以便为代码留出更多空间。下面是我对CheckBoxList的建议。我希望这就是你想要的
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="TestDropdownChecklist.WebForm4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="StyleSheet1.css"/>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick">
</asp:Timer>
<div id="campaignDiv" runat="server">
<ul>
</ul>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
<asp:UpdatePanel ID="upStuff" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="campaignDiv">
<asp:CheckBoxList runat="server" RepeatLayout="UnorderedList" ID="myCheckboxList" TextAlign="Left">
</div>
</asp:CheckBoxList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
</Triggers>
</asp:UpdatePanel>
.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestDropdownChecklist
{
public partial class WebForm4 : System.Web.UI.Page
{
private string CreateLiCheckbox(string checkBoxText)
{
return string.Format("<li><span class=\"textDropdown\">{0}</span><input runat=\"server\" id=\"{1}\" value=\"{0}\" type=\"checkbox\"><label for=\"{1}\"></label></li>", checkBoxText, checkBoxText + "dropdownID");
}
protected void Page_Load(object sender, EventArgs e)
{
int refreshtime = 30000;
Timer1.Interval = refreshtime;
if (!IsPostBack)
{
string[] comps = new string[] { "default", "sales", "direct"};
string html = "<ul>";
for (int i = 0; i < comps.Count(); i++)
{
html = html + CreateLiCheckbox(comps[i]);
}
html = html + "</ul>";
campaignDiv.InnerHtml = html;
}
else
{
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
}
}
}
#DropdownSeviceLink {
float: right;
margin-right: 10px;
}
a#DropdownServiceLink:visited {
color: inherit;
}
#campaignDiv {
background-color: #374954;
width: 200px;
height: 170px;
padding-bottom: 10px;
position: absolute;
top: 40px;
right: 0;
}
#campaignDiv ul {
color: #fff;
list-style: none;
overflow: auto;
padding-left: 5px;
height:100%;
}
#campaignDiv input[type=checkbox] {
visibility: hidden;
}
#campaignDiv input[type=checkbox]:checked + label {
left: 60px;
background: #26ca28;
}
#campaignDiv li {
width: 100px; /*120*/
height: 25px; /*40*/
background: #333;
margin: 13px 0px; /*20px 60px*/
border-radius: 50px;
position: relative;
}
#campaignDiv li:before {
content: 'On';
position: absolute;
top: 4px; /*12*/
left: 13px;
height: 2px;
color: #26ca28;
font-size: 16px;
}
#campaignDiv li:after {
content: 'Off';
position: absolute;
top: 4px; /*12*/
left: 71px; /*84*/
height: 2px;
color: #111;
font-size: 16px;
}
#campaignDiv li label {
display: block;
width: 36px; /*52*/
height: 18px; /*22*/
border-radius: 50px;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-o-transition: all .5s ease;
-ms-transition: all .5s ease;
transition: all .5s ease;
cursor: pointer;
position: absolute;
top: 4px; /*9*/
z-index: 1;
left: 12px;
background: #ddd;
}
.textDropdown {
margin:0px;
padding:0px;
margin-left: 110px;
}
if (!IsPostBack)
{
var comps = new[] { "default", "sales", "direct" };
for (int i = 0; i < comps.Count(); i++)
{
myCheckboxList.Items.Add(new ListItem{Text = comps[i]});
}
}
if(!IsPostBack)
{
var comps=new[]{“default”、“sales”、“direct”};
对于(int i=0;i
这样,它将保持复选框处于选中状态,以便为代码留出更多空间。下面是我对复选框列表的建议。我希望这就是你想要的
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="TestDropdownChecklist.WebForm4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="StyleSheet1.css"/>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick">
</asp:Timer>
<div id="campaignDiv" runat="server">
<ul>
</ul>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
<asp:UpdatePanel ID="upStuff" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="campaignDiv">
<asp:CheckBoxList runat="server" RepeatLayout="UnorderedList" ID="myCheckboxList" TextAlign="Left">
</div>
</asp:CheckBoxList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
</Triggers>
</asp:UpdatePanel>
.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestDropdownChecklist
{
public partial class WebForm4 : System.Web.UI.Page
{
private string CreateLiCheckbox(string checkBoxText)
{
return string.Format("<li><span class=\"textDropdown\">{0}</span><input runat=\"server\" id=\"{1}\" value=\"{0}\" type=\"checkbox\"><label for=\"{1}\"></label></li>", checkBoxText, checkBoxText + "dropdownID");
}
protected void Page_Load(object sender, EventArgs e)
{
int refreshtime = 30000;
Timer1.Interval = refreshtime;
if (!IsPostBack)
{
string[] comps = new string[] { "default", "sales", "direct"};
string html = "<ul>";
for (int i = 0; i < comps.Count(); i++)
{
html = html + CreateLiCheckbox(comps[i]);
}
html = html + "</ul>";
campaignDiv.InnerHtml = html;
}
else
{
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
}
}
}
#DropdownSeviceLink {
float: right;
margin-right: 10px;
}
a#DropdownServiceLink:visited {
color: inherit;
}
#campaignDiv {
background-color: #374954;
width: 200px;
height: 170px;
padding-bottom: 10px;
position: absolute;
top: 40px;
right: 0;
}
#campaignDiv ul {
color: #fff;
list-style: none;
overflow: auto;
padding-left: 5px;
height:100%;
}
#campaignDiv input[type=checkbox] {
visibility: hidden;
}
#campaignDiv input[type=checkbox]:checked + label {
left: 60px;
background: #26ca28;
}
#campaignDiv li {
width: 100px; /*120*/
height: 25px; /*40*/
background: #333;
margin: 13px 0px; /*20px 60px*/
border-radius: 50px;
position: relative;
}
#campaignDiv li:before {
content: 'On';
position: absolute;
top: 4px; /*12*/
left: 13px;
height: 2px;
color: #26ca28;
font-size: 16px;
}
#campaignDiv li:after {
content: 'Off';
position: absolute;
top: 4px; /*12*/
left: 71px; /*84*/
height: 2px;
color: #111;
font-size: 16px;
}
#campaignDiv li label {
display: block;
width: 36px; /*52*/
height: 18px; /*22*/
border-radius: 50px;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-o-transition: all .5s ease;
-ms-transition: all .5s ease;
transition: all .5s ease;
cursor: pointer;
position: absolute;
top: 4px; /*9*/
z-index: 1;
left: 12px;
background: #ddd;
}
.textDropdown {
margin:0px;
padding:0px;
margin-left: 110px;
}
if (!IsPostBack)
{
var comps = new[] { "default", "sales", "direct" };
for (int i = 0; i < comps.Count(); i++)
{
myCheckboxList.Items.Add(new ListItem{Text = comps[i]});
}
}
if(!IsPostBack)
{
var comps=new[]{“default”、“sales”、“direct”};
对于(int i=0;i
通过这种方式,它将首先选中您的复选框,将runat=“server”
添加到您的html字符串在这里是没有意义的,因为它不会被服务器处理,而是按原样发送到客户端
现在,更新面板将始终发出相同的内容,因为它是从视图状态重新填充的,如果禁用更新面板视图状态,下次刷新时它将返回一个空面板(没有可读取的视图状态)
那在这里该怎么办呢!您需要在回发时刷新更新面板内容,并手动读取客户端状态
如果视图状态大小确实与您有关,请执行以下操作:
public partial class WebForm4 : System.Web.UI.Page
{
private string CreateLiCheckbox(string checkBoxText)
{
var checkState = !string.IsNullOrEmpty(Request.Form[string.Format("chk_{0}", checkBoxText)]) ? "checked=\"checked\"" : "";
return string.Format("<li><span class=\"textDropdown\">{0}</span><input id=\"{1}\" name=\"chk_{0}\" value=\"{0}\" type=\"checkbox\" {2}><label for=\"{1}\"></label></li>",
checkBoxText,
checkBoxText + "dropdownID",
checkState);
}
protected void Page_Load(object sender, EventArgs e)
{
int refreshtime = 5000;
Timer1.Interval = refreshtime;
if (!IsPostBack)
PopulateCheckboxes();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
PopulateCheckboxes();
}
private void PopulateCheckboxes()
{
string[] comps = new string[] { "default", "sales", "direct" };
string html = "<ul>";
for (int i = 0; i < comps.Count(); i++)
{
html = html + CreateLiCheckbox(comps[i]);
}
html = html + "</ul>";
campaignDiv.InnerHtml = html;
}
}
public分部类WebForm4:System.Web.UI.Page
{
私有字符串CreateLiCheckbox(字符串checkBoxText)
{
var checkState=!string.IsNullOrEmpty(Request.Form[string.Format(“chk{0}”,checkBoxText)])?“checked=\“checked\:”;
返回string.Format(“{0} ”,
复选框文本,
checkBoxText+“dropdownID”,
检查状态);
}
受保护的无效页面加载(对象发送方、事件参数e)
{
int刷新时间=5000;
Timer1.间隔=刷新时间;
如果(!IsPostBack)
PopulateCheckboxes();
}
受保护的无效计时器1_刻度(对象发送方,事件参数e)
{
PopulateCheckboxes();
}
私有void PopulateCheckboxes()
{
字符串[]comps=新字符串[]{“默认”、“销售”、“直接”};
字符串html=“”;
对于(int i=0;i”;
activitydiv.InnerHtml=html;
}
}
首先,将runat=“server”
添加到html字符串在这里是没有意义的,因为它不会被服务器处理,而是按原样发送到客户端
现在,更新面板将始终发出相同的内容,因为它是从视图状态重新填充的,如果禁用更新面板视图状态,下次刷新时它将返回一个空面板(没有可读取的视图状态)
那在这里该怎么办呢!您需要在回发时刷新更新面板内容,并手动读取客户端状态
如果视图状态大小确实与您有关,请执行以下操作:
public partial class WebForm4 : System.Web.UI.Page
{
private string CreateLiCheckbox(string checkBoxText)
{
var checkState = !string.IsNullOrEmpty(Request.Form[string.Format("chk_{0}", checkBoxText)]) ? "checked=\"checked\"" : "";
return string.Format("<li><span class=\"textDropdown\">{0}</span><input id=\"{1}\" name=\"chk_{0}\" value=\"{0}\" type=\"checkbox\" {2}><label for=\"{1}\"></label></li>",
checkBoxText,
checkBoxText + "dropdownID",
checkState);
}
protected void Page_Load(object sender, EventArgs e)
{
int refreshtime = 5000;
Timer1.Interval = refreshtime;
if (!IsPostBack)
PopulateCheckboxes();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
PopulateCheckboxes();
}
private void PopulateCheckboxes()
{
string[] comps = new string[] { "default", "sales", "direct" };
string html = "<ul>";
for (int i = 0; i < comps.Count(); i++)
{
html = html + CreateLiCheckbox(comps[i]);
}
html = html + "</ul>";
campaignDiv.InnerHtml = html;
}
}
public分部类WebForm4:System.Web.UI.Page
{
私有字符串CreateLiCheckbox(字符串checkBoxText)
{
var checkState=!string.IsNullOrEmpty(Request.Form[string.Format(“chk{0}”,checkBoxText)])?“checked=\“checked\:”;
返回string.Format(“{0} ”,
复选框文本,
checkBoxText+“dropdownID”,
检查状态);
}
受保护的无效页面加载(对象发送方、事件参数e)
{
int刷新时间=5000;
Timer1.间隔=刷新时间;
如果(!IsPostBack)
PopulateCheckboxes();
}
受保护的无效计时器1_刻度(对象发送方,事件参数e)
{
PopulateCheckboxes();
}
私有void PopulateCheckboxes()
{
字符串[]comps=新字符串[]{“默认”、“销售”、“直接”};
字符串html=“”;
对于(int i=0;i”;
activitydiv.InnerHtml=html;
}
}
您正在添加简单的html控件,这些控件不象asp.net控件那样在viewstate的帮助下维护状态。但是,您不使用checkboxlist控件的具体原因是什么?看起来这正是您想要的,不是吗?很明显,您每次在页面加载事件中都会呈现相同的复选框,因此每次都会看到相同的控件系列是很正常的!使用ViewState模拟asp.net控件的行为。@Schadensbegrenzer首先,我在我的复选框中添加了runat server
,以便