Reputation: 9966
I am using ASP.NET Web Forms, and I have a problem with how to handle the state.
I am adding a new product, and the product has different variations. Each variation has several unique characteristics, for instance: Price, purchase price, variation_type, variation_value.
In the UI, I want to be able to add a list of variations. When I've added X variations and other info about the product, I want to submit the whole form and then start doing the adding-to-database part.
Explained with an image:
Ok, now with an image. A user adds a new name for the variation, clicks "Add" and then it's added to the current variations ListBox control. When clicking a variation in the "Current variations", i can fill out the information about the variation, and save it.
When I am done with all the variations, I can submit the form.
Now, my problem
If I use AutoPostBack of the Current variations and it makes a postback, everything is forgotten because of the state. Using standard ASP.NET web forms, I can't find a way to store these lists.
What I really need, would be the ListBox from Windows Forms which contains objects. But good luck with a stateless web ;-)
Solutions I could think off
At every click, changes are saved into a temporary session which is used when submitting the form
At every click, I could save stuff into the database temporarily
A kind of solution I would like to have
A great solution would be some jQuery magic, where I dynamically can create all the rows I need without a postback. Then when I am done, I could iterate all the controls, and store it in the database.
I guess this is a normal issue, and someone can send a link to something obvious, or maybe show me how easy it is to fix it! :-)
Oh yeah, and one last thing, don't send me to hell because I still have projects in Web Forms.
Upvotes: 1
Views: 1673
Reputation: 9861
This is most certainly a place where I feel that ViewState should be used. Ordinarily, I argue against using ViewState in Web Forms. However, you are in such a place where this 'stateful' operation is necessary.
With an ASPX that contains form elements like this:
New Variation:
<asp:TextBox runat="server" ID="newName"></asp:TextBox>
<br />
<asp:Button runat="server" ID="addName" Text="Add Variant" OnClick="addName_Click" />
<br />
<br />
Current Variations:<br />
<asp:ListBox runat="server" ID="listOfVariations" Width="200px"
AutoPostBack="true"
OnSelectedIndexChanged="listOfVariations_SelectedIndexChanged">
</asp:ListBox>
<br />
Feature 1:<br />
<asp:TextBox runat="server" ID="feature"></asp:TextBox>
<br />
<asp:Button runat="server" ID="submit" Text="Add Variation" />
You can write a code-behind then that moves the data into ViewState to be remembered between page loads:
protected void addName_Click(object sender, EventArgs e)
{
listOfVariations.Items.Add(newName.Text);
ViewState.Add("item_" + newName.Text, new VariantObject{ Name = newName.Text });
newName.Text = "";
}
[Serializable]
public class VariantObject
{
public string Name { get; set; }
public string Feature1 { get; set; }
}
protected void listOfVariations_SelectedIndexChanged(object sender, EventArgs e)
{
var v = ViewState["item_" + listOfVariations.SelectedValue] as VariantObject;
feature.Text = v.Feature1 ?? "";
}
Upvotes: 1