Need help with dynamically loaded user control losing changes...usercontrol

R

Rosemary

I have been struggling with this for weeks. Each time I thought I had
it figured out, it has reared its ugly head again! :) Any help is
SO appreciated since my project deadline is Friday!!!!

************What I am trying to do:
I have a bunch of user controls that are loaded and unloaded one at a
time. Before one control is loaded, a SaveControl method is called to
save any changes the user made in the previous control.

************What is happening:
I am dynamically loading a user control in a place holder like this:
Control ctl = new Control();
ctl = LoadControl("AssessmentDemographics.ascx");

The control loads fine. A user is able to change values of controls
in the user control. Once they have made their changes, they click on
the next control they want to load. However, when the SaveControl
method in the usercontrol is triggered the first time, the changes are
saved. But then any other changes made are lost!

I have stepped through the code and watched the new values entered by
the user disappear. For example, a patient has been entered with the
first name of "John". The user changes the name to "Jack", but when
the SaveControl method is called and the first name property is being
set to the text box's text, it says the text box text still says John.

I am not sure what I am doing right the first time and doing wrong
every other time! I don't know what I am missing. I will post some
code to give a better idea of what I am doing. Hopefully it isn't as
confusing as I am feeling right now. :)

TIA,

Rosemary

//*****************CODE TO FOLLOW******************************

//---------------------------MAIN
PAGE------------------------------------------
//I have link buttons that a user can click to change which form they
are working on.
//Each time a link is clicked, the current form is saved, and the new
one loaded

protected void lnkDemographics_Click(object sender, EventArgs e)
{
//Utility is just a class I created to help me convert values
without having null errors, etc....
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

SaveCurrentControl();
//Form Order is simply an enum letting me save which Form ID is
currently loaded.
//I save that form id to a cookie.

SaveCurrentControlID(utility.GetInt32(AGP.Common.FormOrder.AssessmentDemographics));
LoadCurrentControl();
}

protected void lnkAssessmentMain_Click(object sender, EventArgs e)
{
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

SaveCurrentControl();

SaveCurrentControlID(utility.GetInt32(AGP.Common.FormOrder.AssessmentMain));
LoadCurrentControl();
}

private void LoadCurrentControl()
{
int controlID = GetCurrentControlID();

//If controlID = 0 then no control needs to be loaded, so exit the
method
if (controlID == 0)
{
return;
}

//Load the requested control
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();
Control ctl = new Control();
placeHolder.Controls.Clear();

if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentDemographics))
{
this.lnkDemographics.BackColor = System.Drawing.Color.AliceBlue;

ctl = LoadControl("AssessmentDemographics.ascx");
}
else if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentMain))
{
this.lnkAssessmentMain.BackColor =
System.Drawing.Color.AliceBlue;
ctl = LoadControl("AssessmentMain.ascx");
}

placeHolder.Controls.Add(ctl);
}

private Boolean SaveCurrentControl()
{
//If the control to save is empty, then this is a first load and
no control needs saving yet
int controlID = GetCurrentControlID();

if (controlID == 0)
{
return false;
}

AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentDemographics))
{
UserControls_AssessmentDemographics currentControl =
(UserControls_AssessmentDemographics)placeHolder.Controls[0];
currentControl.SaveControl();
}
else if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentMain))
{
UserControls_AssessmentMain currentControl =
(UserControls_AssessmentMain)placeHolder.Controls[0];
currentControl.SaveControl();
}
return true;
}

private void SaveCurrentControlID(int controlID)
{
Session["CurrentControlID"] = controlID;
}

private void ClearCurrentControlID()
{
Session["CurrentControlID"] = null;
}

private int GetCurrentControlID()
{
if (Session["CurrentControlID"] == null)
{
return 0;
}
else
{
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();
return utility.GetInt32(Session["CurrentControlID"]);
}
}

//-------------------------DEMOGRAPHICS USER
CONTROL----------------------------
//The second user control is very similar to this one,
//just uses a different class to update different properties.

protected void Page_Load(object sender, System.EventArgs e)
{
OpenAssessment();
}

private void OpenAssessment()
{
Patient patientToEdit = GetSelectedPatient();
Contract contract = GetSelectedContract();

AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

this.txtFirstName.Text =
utility.GetString(patientToEdit.FirstName);
}

public void SaveControl()
{
Patient patient = GetSelectedPatient();

Utility utility = new Utility();

patient.FirstName = utility.GetString(txtFirstName.Text);

//Save the patient id for later retrieval
SavePatientID(new Patients().AddPatient(patient));
}
 
B

bruce barker

you have a couple issues.

1) on postback, you are not loading the control in oninit, so it can not get
its postback data

2) you use auto generated names, so the name generated on postback may not
match the name from the render. (this happens when a control was added to the
placeholder, then removed, then a new added).

-- bruce (sqlwork.com)


Rosemary said:
I have been struggling with this for weeks. Each time I thought I had
it figured out, it has reared its ugly head again! :) Any help is
SO appreciated since my project deadline is Friday!!!!

************What I am trying to do:
I have a bunch of user controls that are loaded and unloaded one at a
time. Before one control is loaded, a SaveControl method is called to
save any changes the user made in the previous control.

************What is happening:
I am dynamically loading a user control in a place holder like this:
Control ctl = new Control();
ctl = LoadControl("AssessmentDemographics.ascx");

The control loads fine. A user is able to change values of controls
in the user control. Once they have made their changes, they click on
the next control they want to load. However, when the SaveControl
method in the usercontrol is triggered the first time, the changes are
saved. But then any other changes made are lost!

I have stepped through the code and watched the new values entered by
the user disappear. For example, a patient has been entered with the
first name of "John". The user changes the name to "Jack", but when
the SaveControl method is called and the first name property is being
set to the text box's text, it says the text box text still says John.

I am not sure what I am doing right the first time and doing wrong
every other time! I don't know what I am missing. I will post some
code to give a better idea of what I am doing. Hopefully it isn't as
confusing as I am feeling right now. :)

TIA,

Rosemary

//*****************CODE TO FOLLOW******************************

//---------------------------MAIN
PAGE------------------------------------------
//I have link buttons that a user can click to change which form they
are working on.
//Each time a link is clicked, the current form is saved, and the new
one loaded

protected void lnkDemographics_Click(object sender, EventArgs e)
{
//Utility is just a class I created to help me convert values
without having null errors, etc....
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

SaveCurrentControl();
//Form Order is simply an enum letting me save which Form ID is
currently loaded.
//I save that form id to a cookie.

SaveCurrentControlID(utility.GetInt32(AGP.Common.FormOrder.AssessmentDemographics));
LoadCurrentControl();
}

protected void lnkAssessmentMain_Click(object sender, EventArgs e)
{
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

SaveCurrentControl();

SaveCurrentControlID(utility.GetInt32(AGP.Common.FormOrder.AssessmentMain));
LoadCurrentControl();
}

private void LoadCurrentControl()
{
int controlID = GetCurrentControlID();

//If controlID = 0 then no control needs to be loaded, so exit the
method
if (controlID == 0)
{
return;
}

//Load the requested control
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();
Control ctl = new Control();
placeHolder.Controls.Clear();

if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentDemographics))
{
this.lnkDemographics.BackColor = System.Drawing.Color.AliceBlue;

ctl = LoadControl("AssessmentDemographics.ascx");
}
else if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentMain))
{
this.lnkAssessmentMain.BackColor =
System.Drawing.Color.AliceBlue;
ctl = LoadControl("AssessmentMain.ascx");
}

placeHolder.Controls.Add(ctl);
}

private Boolean SaveCurrentControl()
{
//If the control to save is empty, then this is a first load and
no control needs saving yet
int controlID = GetCurrentControlID();

if (controlID == 0)
{
return false;
}

AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentDemographics))
{
UserControls_AssessmentDemographics currentControl =
(UserControls_AssessmentDemographics)placeHolder.Controls[0];
currentControl.SaveControl();
}
else if (controlID ==
utility.GetInt32(AGP.Common.FormOrder.AssessmentMain))
{
UserControls_AssessmentMain currentControl =
(UserControls_AssessmentMain)placeHolder.Controls[0];
currentControl.SaveControl();
}
return true;
}

private void SaveCurrentControlID(int controlID)
{
Session["CurrentControlID"] = controlID;
}

private void ClearCurrentControlID()
{
Session["CurrentControlID"] = null;
}

private int GetCurrentControlID()
{
if (Session["CurrentControlID"] == null)
{
return 0;
}
else
{
AGP.Utilities.Utility utility = new AGP.Utilities.Utility();
return utility.GetInt32(Session["CurrentControlID"]);
}
}

//-------------------------DEMOGRAPHICS USER
CONTROL----------------------------
//The second user control is very similar to this one,
//just uses a different class to update different properties.

protected void Page_Load(object sender, System.EventArgs e)
{
OpenAssessment();
}

private void OpenAssessment()
{
Patient patientToEdit = GetSelectedPatient();
Contract contract = GetSelectedContract();

AGP.Utilities.Utility utility = new AGP.Utilities.Utility();

this.txtFirstName.Text =
utility.GetString(patientToEdit.FirstName);
}

public void SaveControl()
{
Patient patient = GetSelectedPatient();

Utility utility = new Utility();

patient.FirstName = utility.GetString(txtFirstName.Text);

//Save the patient id for later retrieval
SavePatientID(new Patients().AddPatient(patient));
}
 
R

Rosemary

1) I am not sure what you mean by this. In the oninit of the
usercontrol am I suppose to reload the control? What would that look
like?

2) Again, I am clueless. :) As far as I know, I have given unique
names to each Usercontrol and all their controls. What part would be
autogenerated?

Thank you for your reply - can you help clarify. Links would also be
helpful if that would be easier.

Sincerely,

Rosemary
 
R

Rosemary

After researching (and finding some other of your posts) I have tried
the following to no avail - I get the same results:

I pulled the OpenAssessment() method from the Page_Load and did the
following:

override protected void OnInit(EventArgs e)
{
OpenAssessment();
}

I know I am probably missing something very basic here, but I don't
what. Again, any direction would be greatly appreciated.
 
B

bruce barker

1) your page's OnInit routine should call LoadCurrentControl(), so that the
control exists to receive postback data on a submit.

2) when you load the control, and add to the placeholder, you don't assign
it an id, but use an auto generated one.

-- bruce (sqlwork.com)
 
R

Rosemary

Wow! Thank you, thank you, thank you! (and also to my geek husband
who helped me figure this out too!)

Ok, what I did was...

In the loading of the control I added to this piece of code:
ctl = LoadControl("AssessmentDemographics.ascx");

the following line of:
ctl.ID = "AD";

(as you suggested)

Then in saving the control, I changed the previous code of:
UserControls_AssessmentDemographics currentControl =
(UserControls_AssessmentDemographics)placeHolder.Controls[0];

to

UserControls_AssessmentDemographics currentControl =
(UserControls_AssessmentDemographics)placeHolder.FindControl("AD");

(as you suggested in another post I found)


Thanks again so much - I am very much relieved!!!!

Rosemary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top