JBurlison JBurlison - 3 months ago 8
ASP.NET (C#) Question

asp table returning 0 rows on save

I have a form with a table, a cancel button and a save button. The last column in the table is editable. The save button saves the edits to the last column. The problem I am having is when I save, the table posts back as having 0 rows in the SaveButton_ServerClick method.

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

Answer

Try creating the dynamic controls every time in the PreInit event, that is the only way ViewState will ever get applied on Post-Back.

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