【问题标题】:how to load the checkbox values and persist them on postback in asp.net webforms如何加载复选框值并将它们保留在 asp.net webforms 的回发中
【发布时间】:2017-07-29 03:47:21
【问题描述】:

我有一堆复选框和一个文本框,此时我已经硬编码。根据业务规则,复选框选择需要保存,并且在回发或页面刷新时选择需要保留,在我的情况下,我可以将值保存到数据库中,但是无法再次加载它们回帖。有人可以给我一个例子来处理这个问题。我认为我需要编写一个加载方法来做到这一点。谁能给我一个例子。

代码:

<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");
        }

【问题讨论】:

  • 在高层次上,您可能希望区分页面的postback 和initial load。在初始加载时,您需要执行当前正在执行的操作,但在回发时,您需要以不同方式初始化您的内容,以保留和/或覆盖用户输入的数据。
  • 为什么要保持页面刷新时的值?如果一个全新的用户第一次访问该网站,你会怎么做?
  • 请告诉我们背后的代码。
  • 感谢更新代码。乍一看,我会说InitializeSurveys(); 中的某些内容将您的复选框设置为特定的检查状态。并且由于InitializeSurveys();发生在if (IsPostBack) return;之前,那么检查postBack没有效果......它没有效果,因为我们检查了Postback 在初始化非回发中的字段 方式。您可以将回发状态传递给InitializeSurveys(postBackState);,然后直接在该方法中相应地初始化您的字段。
  • 能否请您发布整个 ASPX 页面?

标签: javascript c# asp.net checkbox


【解决方案1】:

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

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;
            }
        }


    }
}

【讨论】:

  • 我已经尝试过您的代码,但它不适用于我的场景
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多