2017-07-28 28 views
0

我有一堆複選框和一個文本框,在這一點上我已經硬編碼了。根據業務規則,複選框選擇需要保存,並且在回發或頁面刷新時,選擇需要保留,在我的情況下,我可以將值保存到數據庫,但無法再次加載它們發回。有人能爲我提供一個例子來處理這個問題。我認爲我需要編寫一個加載方法來執行此操作。有人可以給我一個例子。如何加載複選框的值並在asp.net中將其保留在回發中webforms

代碼:

<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 = [email protected]"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

爲什麼它會保持頁面上的值刷新?如果一個全新的用戶第一次來到這個網站會怎麼樣,那麼你會怎麼做? – CodingYoshi

+0

請告訴我們背後的代碼。 – Win

回答

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


    } 
} 
+0

我在編輯中發佈了相關的代碼。 – user3147594

+0

我試過了你的代碼,但是它並沒有用於我的場景 – user3147594

相關問題