我有一张带有桌子,取消按钮和保存按钮的表格。表格中的最后一列是可编辑的。保存按钮将编辑内容保存到最后一列。我遇到的问题是当我保存时,表在SaveButton_ServerClick方法中回发为0行。
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 :(得分:1)
每次在PreInit事件中尝试创建动态控件,这是ViewState在Post-Back上应用的唯一方式。
protected void Page_PreInit(object sender, EventArgs e)
{
LoadSettings();
}
答案 1 :(得分:0)
回发后,表格数据丢失了。所以请将表数据保存在sessionstate或viewstate中。