2016-08-31 8 views
0

テーブル、キャンセルボタン、保存ボタンがあるフォームがあります。表の最後の列は編集可能です。保存ボタンは、最後の列に編集内容を保存します。私が抱えている問題は、SaveButton_ServerClickメソッドでテーブルを0行にして保存したときです。保存時に0行を返すaspテーブル

HTML:

<%@ Page validateRequest="false" Language="C#" AutoEventWireup="true" CodeBehind="xxxxxx.aspx.cs" Inherits="xxxxx.xxx" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="Form1" runat="server"> 
     <asp:Table id="SettingsTable" runat="server" EnableViewState="true"></asp:Table> 
     <asp:Button id="CancelButton" runat="server" Text="Cancel"></asp:Button> 
     <asp:Button id="SaveButton" runat="server" Text="Save"></asp:Button> 
    </form> 
</body> 
</html> 

のC#:

 const string CONFIG_SET_ID = "ConfigurationSetId"; 
     const string CONFIG_SET_NAME = "ConfigurationSetName"; 
     const string CONFIG_SET_DESC = "ConfigurationSetDescription"; 
     const string APP_NAME = "AppName"; 
     const string APP_ID = "AppId"; 
     const string SET_CAT = "SettingCategory"; 
     const string SET_DESC = "SettingDescription"; 
     const string SET_CAT_ID = "SettingCategoryId"; 
     const string TITLE = "title"; 

     const int CONFIG_SET_CELL = 0; 
     const int APP_NAME_CELL = 1; 
     const int SET_CAT_CELL = 2; 
     const int SETTINGKEY_CELL = 3; 
     const int SETTINGVALUE_CELL = 4; 

     static Dictionary<string, ConfigurationDictionary> _cfgDics = new Dictionary<string, ConfigurationDictionary>(); 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      AppCfg.AppName = "xxxxx"; 
      AppCfg.Initialize(); 

      if (!Page.IsPostBack) 
       LoadSettings(); 

      SaveButton.Click += SaveButton_ServerClick; 
      CancelButton.Click += CancelButton_ServerClick; 
     } 

     /// ADDING THIS FIXED THE POSTBACK PROBLEM. 
     void CancelButton_ServerClick(object sender, EventArgs e) 
     { 
      LoadSettings(); 
     } 

     void SaveButton_ServerClick(object sender, EventArgs e) 
     { 
      foreach (TableRow row in SettingsTable.Rows) 
      { 
       if (row.Cells[SETTINGVALUE_CELL].Controls[0].GetType().Equals(typeof(TextBox))) 
       { 
        string appId = row.Cells[APP_NAME_CELL].Attributes[APP_ID]; 
        string settingKey = row.Cells[SETTINGKEY_CELL].Text; 
        string settingValue = ((TextBox)row.Cells[SETTINGVALUE_CELL].Controls[0]).Text; 

        if (_cfgDics.ContainsKey(appId) && _cfgDics[appId][settingKey].Value != settingValue) 
         _cfgDics[appId][settingKey] = settingValue; 
       } 
      } 


      System.Threading.Thread.Sleep(1000); 
      LoadSettings(); 
     } 

     void CancelButton_ServerClick(object sender, EventArgs e) 
     { 
      LoadSettings(); 
     } 


     private void LoadSettings() 
     { 
      //foreach (ConfigurationDictionary dic in _cfgDics.Values) 
      // dic.Dispose(); 

      //_cfgDics.Clear(); 
      SettingsTable.Rows.Clear(); 

      // Build Settings table 
      // Build Header row 
      TableRow headerRow = new TableRow(); 
      headerRow.Cells.Add(new TableCell() { Text = "Config Set" }); 
      headerRow.Cells.Add(new TableCell() { Text = "Application" }); 
      headerRow.Cells.Add(new TableCell() { Text = "Category" }); 
      headerRow.Cells.Add(new TableCell() { Text = "Setting Key" }); 
      headerRow.Cells.Add(new TableCell() { Text = "Setting Value" }); 
      SettingsTable.Rows.Add(headerRow); 

      IniFileEditor xxxIni = new IniFileEditor(); 
      string dbConnString = xxxIni.ReadValue(ConfigurationDictionary.SECKEY, ConfigurationDictionary.SETTING_DB_KEY); 

      if (!string.IsNullOrEmpty(dbConnString)) 
      { 
       // Build each setting 
       using (DatabaseAccess dba = new DatabaseAccess(dbConnString)) 
       using (SqlCommand cmd = new SqlCommand("Select * from AllSettings")) 
       using (SqlDataReader reader = dba.GetSqlReader(cmd)) 
       { 
        while (reader.Read()) 
        { 
         TableRow row = new TableRow(); 
         string appId = string.Empty; 

         row.Cells.Add(new TableCell() { Text = reader[CONFIG_SET_NAME].ToString() }); 
         row.Cells[CONFIG_SET_CELL].Attributes[CONFIG_SET_ID] = reader[CONFIG_SET_ID].ToString(); 
         row.Cells[CONFIG_SET_CELL].Attributes[TITLE] = reader[CONFIG_SET_DESC].ToString(); 

         row.Cells.Add(new TableCell() { Text = reader[APP_NAME].ToString() }); 
         appId = reader[APP_ID].ToString(); 
         row.Cells[APP_NAME_CELL].Attributes[APP_ID] = appId; 
         row.Cells[APP_NAME_CELL].Attributes[TITLE] = string.Format("AppId:\t{0} \r\nMachine:\t{1} \r\nIPAddress:\t{2}", 
                     reader[APP_ID], 
                     reader["MachineName"], 
                     reader["MachineAddress"]); 

         row.Cells.Add(new TableCell() { Text = reader[SET_CAT].ToString() }); 
         row.Cells[SET_CAT_CELL].Attributes[SET_CAT_ID] = reader[SET_CAT_ID].ToString(); 

         row.Cells.Add(new TableCell() { Text = reader["SettingKey"].ToString() }); 
         row.Cells[SETTINGKEY_CELL].Attributes[TITLE] = reader[SET_DESC].ToString(); 

         TextBox valueTextbox = new TextBox(); 
         valueTextbox.Text = reader["SettingValue"].ToString(); 
         row.Cells.Add(new TableCell()); 
         row.Cells[SETTINGVALUE_CELL].Controls.Add(valueTextbox); 

         if (!_cfgDics.ContainsKey(appId)) 
          _cfgDics.Add(appId, new ConfigurationDictionary(appId)); 

         SettingsTable.Rows.Add(row); 
        } 
       } 
      } 
     } 
+0

LoadSettingsメソッドがIsPostBackで呼び出されないため、SessionまたはViewStateから取得しないとSettingsTableが空になります – Steve

+0

セッション状態の保存を追加しましたが、Web UIで変更した値はポストされませんバック。私はこのすべての間違ったデザインについて考えていますか? – JBurlison

+0

別の答えを使用した後、Seano666は正しいです。 – JBurlison

答えて

1

はPREINITイベントで毎回動的にコントロールを作成してみてください、それはViewStateは今までにポストバックに適用されます唯一の方法です。

protected void Page_PreInit(object sender, EventArgs e) 
     { 
      LoadSettings(); 
     } 
0

ポストバック時に、データが失われました。したがって、テーブルのデータをsessionstateまたはviewstateに保存してください。

関連する問題