如何加载复选框值并在回发时将其保留在 Web 窗体 asp.net



我有一堆复选框和一个文本框,此时我已经硬编码了。根据业务规则,复选框选择需要保存,而且,在回发或页面刷新时,选择需要保留,就我而言,我可以将值保存到数据库,但无法在回发时再次加载它们。有人可以给我一个例子来处理这个问题。我认为我需要编写一个加载方法来执行此操作。有人可以给我举个例子吗?

法典:

<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
<div class="col-md-4">
<div class="form-group">
<div class="check">
<asp:CheckBox runat="server" ID="chkRS" Text="RS" />
</div>
<br />
<asp:CheckBox runat="server" ID="chkSC" Text="SSC" />
<br />
<asp:CheckBox runat="server" ID="SCR" Text="SCR" />
<br />
<asp:Label runat="server" AssociatedControlID="txtPrint">Print Button Class</asp:Label>
<asp:TextBox runat="server" ID="txtPrint" CssClass="form-control"></asp:TextBox>
</div>
</div>
</div>
</div>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
InitializeSurveys();
if (IsPostBack) return;
_projectContext.SelectedSurvey = null;

}

private void LoadSurvey(Survey survey)
{
ViewState["surveyId"] = survey.SurveyID;
txtTitle.Text = survey.Name;
chkActive.Checked = survey.Active;          
//ddlSurveyType.SelectedValue = survey.Type;
//TODO: There is no need for this setting
ddlRespondentType.SelectedValue = survey.RespondentType.ToString();
lstLanguages.Visible = false;
lblMultipleText.Visible = false;
lblLanguages.Visible = true;
ddlLanguages.Enabled = false;
lblLanguages.Text = "";
survey.SurveyLanguages
.Select(s => s.Language.LanguageName)
.ToList().ForEach(sl => lblLanguages.Text += "<br>" + sl);
ddlLanguages.SelectedValue = survey.DefaultLanguageID.ToString();
txtKeywords.Text = survey.Keywords;
ddlGroup.SelectedValue = survey.GroupId == null ? "-1" : survey.GroupId.ToString();
lblUniqueCode.Text = $@"Code<br><div style=""border: solid 1px #ccc; background-color: #eee;padding: 7px;"">{survey.Code}</div>";
}

protected void lnkSave_Click(object sender, EventArgs e)
{
var id = ViewState["surveyId"] != null
? long.Parse(ViewState["surveyId"].ToString())
: 0;
var survey = ViewState["surveyId"] != null
? _surveyRepo.GetSurveys().Include(s => s.ReportingGroups).Include(s => s.SurveySettings).Single(s => s.SurveyID == id)
: new Survey();
var ss = new List<Ss>();        
foreach (var item in g.ss)
{
var setting = s.ss.FirstOrDefault(s => s.Key == item.Key);
WebControl ctrl = GetControl(item.Key.ToString());
if (ctrl == null) continue;
if (setting == null)
{
setting = new SurveySetting()
{
SurveyId = id,
Key = item.Key
};
}
if (ctrl is CheckBox)
{
setting.Value = ((CheckBox)(ctrl)).Checked.ToString();
}
else
{
setting.Value = ((TextBox)(ctrl)).Text;
}
surveySettings.Add(setting);
}
foreach (var temp in surveySettings)
{
if (!survey.SurveySettings.Any(ss => ss.Key == temp.Key))
{
_surveyRepo.AddSurveySetting(temp);
}
}

_sRepo.Save();

ClearForm();
InitializeSurveys();
LoadDropDown();
LoadSurveys();
upAddUpdate.Update();
upMain.Update();
CloseModalOnUpdate("close_on_edit", "#NewSurveyModal");
}

区分回发和非回发。如果不这样做,则根据页面的设置方式,您最终可能会在每次页面加载时将字段初始化为其初始值,而不管实际的回发状态如何。

using System;
namespace PersistingCheckboxes_45379633
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
/*
* This is a postback, therefore I want to populate the page according to what's in the database
*/
txtbx_1.Text = "postback";
chkbx_item1.Checked = WhatDoesTheDBSay(1);
chkbx_item2.Checked = WhatDoesTheDBSay(2);
}
else
{
/*
* This is not a postback, so we initialize the fields
*/
txtbx_1.Text = "not a postback";
chkbx_item1.Checked = false;
}
}
private bool WhatDoesTheDBSay(int fieldID)
{
if (fieldID == 1)
{
return true;
}
else
{
return false;
}
}

}
}

跟进我关于将回发状态传递给初始化方法的评论。

using System;
namespace PersistingCheckboxes_45379633
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//initialize taking Postback into account
InitializeSurveys(true);
}
else
{
//initialize without postback
InitializeSurveys(false);
}
}
private void InitializeSurveys(bool IsItAPostBack)
{
if (IsItAPostBack)
{
/*
* This is a postback, therefore I want to populate the page according to what's in the database
*/
txtbx_1.Text = "postback";
chkbx_item1.Checked = WhatDoesTheDBSay(1);
chkbx_item2.Checked = WhatDoesTheDBSay(2);
}
else
{
/*
* This is not a postback, so we initialize the fields
*/
txtbx_1.Text = "not a postback";
chkbx_item1.Checked = false;
}
}
private bool WhatDoesTheDBSay(int fieldID)
{
if (fieldID == 1)
{
return true;
}
else
{
return false;
}
}

}
}

最新更新