我有一堆复选框和一个文本框,我现在已经硬编码了。根据业务规则,复选框选择需要保存,并且在回发或页面刷新时,选择需要保持,在我的情况下,我能够将值保存到数据库,但是,无法再次加载它们回帖。有人能为我提供一个处理这个的例子。我认为我需要编写一个load方法来执行此操作。有人能为我提供一个例子。
代码:
<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");
}
答案 0 :(得分:0)
区分回发和非回发。如果不这样做,根据页面的设置方式,最终可能会在每次页面加载时将字段初始化为初始值,而不管实际的回发状态如何。
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;
}
}
}
}