Reputation: 33
Simple enough question, although perhaps a better title would be "I have no idea what I'm doing when it comes to ViewStates
, please help me". What I want to be able to do is click btnAddSkills
and have a new row appear at the bottom of the pre-existing table (and if it doesn't exist, have the table be created). Currently, the first row is added when btnAddSkills
is clicked, but when it's clicked a second time it over-writes the first row (and the selected value of a DropBox
inside one of the cells is lost.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class _Default : System.Web.UI.Page
{
private Table tblSkillsMaster;
protected void Page_Load(object sender, EventArgs e)
{
//my past experience tells me something will need to go in here
}
protected void btnAddSkill_Click(object sender, EventArgs e)
{
if (ViewState["masterSkills"] != null)
{
tblSkillsMaster = (Table)ViewState["masterSkills"];
}
else
{
plhSkillsTable.Controls.Clear(); //plh is a PlaceHolder
tblSkillsMaster = new Table();
plhSkillsTable.Controls.Add(tblSkillsMaster);
}
tblSkillsMaster.Rows.Add(AddRow()); //handled elsewhere, cut for brevity
ViewState["masterSkills"] = (Table)tblSkillsMaster;
}
}
}
Upvotes: 1
Views: 213
Reputation: 66
ViewState can hold only serializable objects, in this case System.Web.UI.WebControl.Table is not serilizable and hence you cannot store it in the viewstate. Instead you can use DataTable and GridView to achieve your objective.
Upvotes: 1
Reputation: 140813
From MSDN: Data Types You Can Store in View State You can store objects of the following types in view state:
Otherwise, you can create your own class and make it serializable with the Serializable attribute.
For your case, you cannot edit the Table class to add the attribute since you do not own it. What you can do is to create your own class that will use the information from the Table you want and to handle the back and forth between your class that can be used in the viewstate with the Table class.
Upvotes: 2
Reputation: 62260
You can try testing like this -
protected void btnAddSkill_Click(object sender, EventArgs e)
{
if (ViewState["masterSkills"] != null)
{
tblSkillsMaster = (Table) ViewState["masterSkills"];
}
else
{
plhSkillsTable.Controls.Clear(); //plh is a Placeholder
tblSkillsMaster = new Table();
plhSkillsTable.Controls.Add(tblSkillsMaster);
}
var row = new TableRow();
for (int cellNum = 0; cellNum < 3; cellNum++)
{
var cell = new TableCell {Text = cellNum.ToString()};
row.Cells.Add(cell);
}
tblSkillsMaster.Rows.Add(row);
// *** System.Web.UI.WebControls.Table is not serializable ***
// ViewState["masterSkills"] = tblSkillsMaster;
}
However, you cannot add System.Web.UI.WebControls.Table to ViewState, because it is not serializable.
You might want to use Class instead of Table.
Upvotes: 1
Reputation: 358
The only thing that I see in the code you posted is that you don't need to cast the table to put it into ViewState.
Try changing this:
ViewState["masterSkills"] = (Table)tblSkillsMaster;
To this:
ViewState["masterSkills"] = tblSkillsMaster;
You do have to cast when retrieving from ViewState.
Upvotes: 1