Asked by:
Calling User Control multiple times - issues saving records in db table.

Question
-
<UCR:ucAddRemoveDevGoals runat="server" id="ucAddRemoveDevGoals1" GoalTypeId="1"/> single call works fine. I want to call multiple instances from the same aspx page. <UCR:ucAddRemoveDevGoals runat="server" id="ucAddRemoveDevGoals2" GoalTypeId="2"/><UCR:ucAddRemoveDevGoals runat="server" id="ucAddRemoveDevGoals3" GoalTypeId="3"/> only last uc call saved in db.
.CS
public partial class ucAddRemoveDevGoals : BaseUserControl {
public int GoalTypeId { get; set; }
#region "Development Goals"
private List<YearEndDevelopmentGoalEntity> DevelopmentGoals {
get {
var goals = ViewState["DevelopmentGoals"] as List<YearEndDevelopmentGoalEntity>;
if (goals == null ) {
goals = new List<YearEndDevelopmentGoalEntity>();
ViewState["DevelopmentGoals"] = goals;
}
if (goals.Count == 0) {
goals.Add(new YearEndDevelopmentGoalEntity() { GoalTypeId = GoalTypeId }); // initialize 1 row
ViewState["DevelopmentGoals"] = goals;
}
return goals;
}
set { ViewState["DevelopmentGoals"] = value; }
}
#endregion
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
DevelopmentGoals = CurrentReview.AdvSummReview.YearEndDevelopmentGoals.Where(p => p.GoalTypeId == GoalTypeId).ToList();
} else {
SaveGridValuestoViewState();
}
}
protected override void DisplayUserControl() {
base.DisplayUserControl();
// check access
if (base.ReviewAccessLevel == ReviewAccess.Access.ReadOnly) {
gridDevelopmentGoals.Enabled = false;
}
}
public override bool Save(bool isAutoSave)
{
bool save = false;
if (!ReviewHelpers.AreListsEqual(CurrentReview.AdvSummReview.YearEndDevelopmentGoals, DevelopmentGoals))
{
save = true;
}
foreach (var goal in DevelopmentGoals)
{
goal.GoalTypeId = GoalTypeId;
}
CurrentReview.AdvSummReview.YearEndDevelopmentGoals = DevelopmentGoals;
return save;
}
#region "Development Goals"
protected void gridDevelopmentGoals_ItemDataBound(object sender, GridItemEventArgs e) {
if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem) {
GridDataItem item = e.Item as GridDataItem;
var goal = e.Item.DataItem as YearEndDevelopmentGoalEntity;
TextBox txtWaytoDevelop = (TextBox)e.Item.FindControl("txtWaytoDevelop");
TextBox txtNeedToDo = (TextBox)e.Item.FindControl("txtNeedToDo");
TextBox txtNeedFromFirm = (TextBox)e.Item.FindControl("txtNeedFromFirm");
Button cmdDelete = (Button)e.Item.FindControl("cmdDelete");
txtWaytoDevelop.Text = goal.WayToDevelop;
txtNeedToDo.Text = goal.NeedToDo;
txtNeedFromFirm.Text = goal.NeedFromFirm;
cmdDelete.Attributes.Add("DataItemIndex", item.ItemIndex.ToString());
}
}
protected void SaveGridValuestoViewState() {
List<YearEndDevelopmentGoalEntity> TempGoals = new List<YearEndDevelopmentGoalEntity>();
foreach (GridDataItem item in gridDevelopmentGoals.Items) {
if ((item.ItemType == GridItemType.AlternatingItem) || (item.ItemType == GridItemType.Item)) {
TextBox txtWaytoDevelop = (TextBox)item.FindControl("txtWaytoDevelop");
TextBox txtNeedToDo = (TextBox)item.FindControl("txtNeedToDo");
TextBox txtNeedFromFirm = (TextBox)item.FindControl("txtNeedFromFirm");
YearEndDevelopmentGoalEntity entity = new YearEndDevelopmentGoalEntity();
entity.Review_Id = CurrentReview.Review_Id;
entity.NeedFromFirm = txtNeedFromFirm.Text;
entity.WayToDevelop = txtWaytoDevelop.Text;
entity.NeedToDo = txtNeedToDo.Text;
entity.GoalTypeId = GoalTypeId;
TempGoals.Add(entity);
}
}
DevelopmentGoals = TempGoals;
}
protected void gridDevelopmentGoals_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) {
gridDevelopmentGoals.DataSource = DevelopmentGoals;
}
protected void gridDevelopmentGoals_ItemCommand(object sender, GridCommandEventArgs e) {
if (e.CommandName == "Add") {
DevelopmentGoals.Add(new YearEndDevelopmentGoalEntity() {
Review_Id = CurrentReview.Review_Id,
GoalTypeId = GoalTypeId
});
}
if (e.CommandName == "Remove") {
DevelopmentGoals.RemoveAt(e.Item.ItemIndex);
}
gridDevelopmentGoals.Rebind();
}
#endregion
}
}
- Moved by Wendy ZangMicrosoft contingent staff Tuesday, April 24, 2018 11:48 AM
Monday, April 23, 2018 10:46 PM
All replies
-
Monday, April 23, 2018 10:47 PM
-
You should consider asking on the ASP.NET forums. Also, explain the current behavior, what is not going as planned beside simply indicating multiple inserts are a problem.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, April 24, 2018 2:02 AM -
Hi ShriG,
Thank you for posting here.
For your question is more related to ASP.NET, please post a new thread in ASP.NET forum for suitable support.
The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Tuesday, April 24, 2018 6:37 AM