Save updated object in Generic List
I am trying hard to update a business object inside a List<>.
I am using a checkbox list
- When a checkbox list item is clicked
- I check if it exists, if yes I mark it as Dirty (means the user unchecks an item)
If not, I add a new item to the list (means the user clicks a new item) and store it in view state
How do I update the Generic List with the Dirty Object ? On form update, do I foreach and make separate lists of dirty and new objects to send to DB layer or would you recommend any o开发者_如何学Pythonther way ?
Here is my code, sorry about the bad logic I am just a starter =(
protected void cblExclusions_SelectedIndexChanged(object sender, EventArgs e)
{
if (cblExclusions.SelectedIndex != -1)
{
ftExclusions myExclusion=new ftExclusions(); // Business object
ftExclusionsList myExclusionList=new ftExclusionsList(); // Business Obj. List
int excId = Convert.ToInt32(cblExclusions.SelectedValue.ToString()); // value of checkbox list item which is clicked
ftExclusionsList tempList = (ftExclusionsList)ViewState["ftExclusionList"];
ftExclusions isExist =tempList.Find(delegate(ftExclusions tmpExclusion)
{
return (tmpExclusion.excluId == excId);
});
if (isExist != null)
{
isExist.isDirtyExclusion(); // Mark as dirty
tempList. // stuck here I don't grasp how to save this back to the list
}
else
{
myExclusion = new ftExclusions();
myExclusion.excluId = excId;
myExclusion.fTrtID = Convert.ToInt32(lblTreatyNo.Text);
myExclusion.ftExcluId = -1; //new record
myExclusion.isNewExclusion(); // Mark as new
tempList.Add(myExclusion);
}
ViewState["ftExclusionList"] = tempList;
}
}
You do no need to save it back to the list. Assuming ftExclusions
is class and not struct, it is always passed by reference. Meaning that both tempList
and isExist
contain reference to the same object. And any changes on the object will be visible from both places.
精彩评论