Hi,
Trouble in retaining values of dropdownlist, textboxes, and other controls when dropdownlist selectedindexchanged event is triggered, the controls are inside a user
control and this user control inside a parent user control with an update panel. Can you guys help me hwo to retain the values. I have set EnableViewState to true. Where
is correct page event to store entered and selected values before the values on controls are re-intialized. Please provide some codes and please eb specific unto which is best to use (Session variables, hiddenfields or others). Thanks in advanced.
den2005
Can you please actually provide code you are using and getting the issue with. Otherwise here are some links:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
Hi jodywbcb,
I am working on 5 dropdownlist controls (these codes are created by a co-worker) and they are some how interdependent, first dropdown and second dropdown and third dropdown determines the data for fourth and fifth dropdown, now second dropdown determines the data for third dropdown. The thing is if the first dropdown is set to item 2, and second dropdown is set to to item2 and data selected on third dropdown remains set after postback.
Here at some data I am working on...So what mistake I have made..
protected override void OnLoad(EventArgs e){...this.UpdateContent();base.OnLoad(e);if (multiView.GetActiveView().ID =="viewInputForm"){if (Session["id"] !=null)ShowAssessmentDetails(Session["id"].ToString());DisplayProcessedIconImage();}}//All Dropdown SelectedIndexChanged Eventprotected void DropDown_SelectedIndexChanged(object sender, EventArgs e){if (sender ==this.ddlEmployee){...}else if (sender ==this.ddlCategory){FillSubCategoryList();FillCompetencyList();}else if (sender ==this.ddlSubCategory){if (ddlSubCategory.SelectedIndex != 0){int subcat = Int32.Parse(ddlSubCategory.SelectedValue);ddlCategory.SelectedValue = oAssessment.GetCategoryID(subcat);FillSubCategoryList();ddlSubCategory.SelectedValue = subcat.ToString();if (subcat.ToString() !=string.Empty && subcat.ToString() !="0")ViewState["ddlSubCategory"] = subcat.ToString();}FillCompetencyList();}else if (sender ==this.ddlCompetencyCode){UpdateCompetencyList(1);DoCourseCodeMatching();}else if (sender ==this.ddlCompetency){UpdateCompetencyList(2);DoCourseCodeMatching();}else if (sender ==this.ddlCourseCode){ClearSuggestedTrainingData();//FillTrainingData();DoCourseCodeMatching();}else if (sender ==this.ddlYear){...}else if (sender ==this.ddlCompetencyModel){FillCategoryList();FillSubCategoryList();FillCompetencyList();}...}private void ShowAssessmentDetails(string idVal){oAssessment.ID = Convert.ToInt32(idVal);if (oAssessment.Get()){InitializeControls();...FillCompetencyModelList();//First Dropdown...FillEmployeeData();try { ddlCompetencyModel.SelectedValue = oAssessment.CompetencyModelID.ToString(); }catch { ddlCompetencyModel.SelectedIndex = 0; }FillCategoryList();//Second DropDownFillSubCategoryList();//Third DropdownFillCompetencyList();//Fourth DropdownFillCourseCode();ddlCompetency.SelectedValue = oAssessment.CompetencyID.ToString();UpdateCompetencyList(2);try { ddlCourseCode.SelectedValue = oAssessment.CatalogueCourseID.ToString(); }catch { ddlCourseCode.SelectedIndex = 0; }...FillSupervisorApproval(cbTrainingRequest.Checked);FillLocalApproval(cbTrainingRequest.Checked);ddlCSApproval.SelectedValue = oAssessment.CompetencySupervisorApprovalID.ToString();ddlCLApproval.SelectedValue = oAssessment.CompetencyLocalApprovalID.ToString();...try { ddlSuggLocalTrainingType.SelectedValue = oAssessment.SuggestedLocalTrainingType.ToString(); }catch { ddlSuggLocalTrainingType.SelectedIndex = 0; }tbLocalTrainingSuggest.Text = oAssessment.SuggestedLocalTraining;try{ddlSuggTrainingType.SelectedValue = oAssessment.SuggestedTrainingType.ToString();tbSuggestedTraining.Text = oAssessment.SuggestedTraining;}catch{ddlSuggTrainingType.SelectedIndex = 0;tbSuggestedTraining.Text = String.Empty;}...}}private void InitializeControls(){...FillCompetencyModelList();...FillCategoryList();FillSubCategoryList();FillCompetencyList();FillSupervisorApproval(false);FillLocalApproval(false);...FillCourseCode();...}private void FillCompetencyModelList(){try{using (DataAccessLayer objDAL =new DataAccessLayer("CompetencyModel_GetAllByYear")){objDAL.AddParameter("Year", ddlYear.SelectedValue);objDAL.ExecuteReader();ddlCompetencyModel.Items.Clear();ddlCompetencyModel.Items.Add(new ListItem("-- select model --","0"));while (objDAL.DataReader.Read()){ddlCompetencyModel.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencyModelID"])));}}}catch{ddlCompetencyModel.Items.Clear();ddlCompetencyModel.Items.Add(new ListItem("-- select model --","0"));}}private void FillCompetencyList(){if (ddlCompetencyModel.SelectedIndex != 0){try{ddlCompetencyCode.Items.Clear();ddlCompetency.Items.Clear();ddlCompetencyCode.Items.Add(new ListItem("-- select code --","0"));ddlCompetency.Items.Add(new ListItem("-- select competency --","0"));if (ddlCompetencyModel.SelectedIndex == 0){if ((ddlSubCategory.SelectedValue !=null) && (ddlSubCategory.SelectedValue !="0")){using (DataAccessLayer objDAL =new DataAccessLayer("Competency_GetBySubCategoryIDAndYear")){SqlParameter sqlparam =new SqlParameter("@.subcat", SqlDbType.Int);sqlparam.Value = ddlSubCategory.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.AddParameter("year", ddlYear.SelectedValue);objDAL.ExecuteReader();while (objDAL.DataReader.Read()){ddlCompetencyCode.Items.Add(new ListItem(Convert.ToString(objDAL.DataReader["CompetencyCode"]), Convert.ToString(objDAL.DataReader["CompetencyID"])));ddlCompetency.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencyID"])));}}}else if ((ddlCategory.SelectedValue !=null) && (ddlCategory.SelectedValue !="0")){using (DataAccessLayer objDAL =new DataAccessLayer("Competency_GetByCategoryIDAndYear")){SqlParameter sqlparam =new SqlParameter("@.catid", SqlDbType.Int);sqlparam.Value = ddlCategory.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.AddParameter("year", ddlYear.SelectedValue);objDAL.ExecuteReader();while (objDAL.DataReader.Read()){ddlCompetencyCode.Items.Add(new ListItem(Convert.ToString(objDAL.DataReader["CompetencyCode"]), Convert.ToString(objDAL.DataReader["CompetencyID"])));ddlCompetency.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencyID"])));}}}else{using (DataAccessLayer objDAL =new DataAccessLayer("Competency_GetAllByYear")){objDAL.AddParameter("year", ddlYear.SelectedValue);objDAL.ExecuteReader();while (objDAL.DataReader.Read()){ddlCompetencyCode.Items.Add(new ListItem(Convert.ToString(objDAL.DataReader["CompetencyCode"]), Convert.ToString(objDAL.DataReader["CompetencyID"])));ddlCompetency.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencyID"])));}}}}else if (ddlCompetencyModel.SelectedIndex > 0){if ((ddlSubCategory.SelectedValue !=null) && (ddlSubCategory.SelectedValue !="0")){using (DataAccessLayer objDAL =new DataAccessLayer("Competency_GetBySubCatIDModelYear")){SqlParameter sqlparam =new SqlParameter("@.subcat", SqlDbType.Int);sqlparam.Value = ddlSubCategory.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.AddParameter("year", ddlYear.SelectedValue);sqlparam =new SqlParameter("@.compmodelid", SqlDbType.Int);sqlparam.Value = ddlCompetencyModel.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.ExecuteReader();while (objDAL.DataReader.Read()){ddlCompetencyCode.Items.Add(new ListItem(Convert.ToString(objDAL.DataReader["CompetencyCode"]), Convert.ToString(objDAL.DataReader["CompetencyID"])));ddlCompetency.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencyID"])));}}}else if ((ddlCategory.SelectedValue !=null) && (ddlCategory.SelectedValue !="0")){using (DataAccessLayer objDAL =new DataAccessLayer("Competency_GetByCatIDModelYear")){SqlParameter sqlparam =new SqlParameter("@.catid", SqlDbType.Int);sqlparam.Value = ddlCategory.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.AddParameter("year", ddlYear.SelectedValue);sqlparam =new SqlParameter("@.compmodelid", SqlDbType.Int);sqlparam.Value = ddlCompetencyModel.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.ExecuteReader();while (objDAL.DataReader.Read()){ddlCompetencyCode.Items.Add(new ListItem(Convert.ToString(objDAL.DataReader["CompetencyCode"]), Convert.ToString(objDAL.DataReader["CompetencyID"])));ddlCompetency.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencyID"])));}}}else{using (DataAccessLayer objDAL =new DataAccessLayer("Competency_GetAllByModelYear")){objDAL.AddParameter("year", ddlYear.SelectedValue);SqlParameter sqlparam =new SqlParameter("@.compmodelid", SqlDbType.Int);sqlparam.Value = ddlCompetencyModel.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.ExecuteReader();while (objDAL.DataReader.Read()){ddlCompetencyCode.Items.Add(new ListItem(Convert.ToString(objDAL.DataReader["CompetencyCode"]), Convert.ToString(objDAL.DataReader["CompetencyID"])));ddlCompetency.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencyID"])));}}}}}catch{ddlCompetencyCode.Items.Clear();ddlCompetency.Items.Clear();ddlCompetencyCode.Items.Add(new ListItem("-- select code --","0"));ddlCompetency.Items.Add(new ListItem("-- select competency --","0"));}}else{ddlCompetencyCode.Items.Clear();ddlCompetency.Items.Clear();ddlCompetencyCode.Items.Add(new ListItem("-- select code --","0"));ddlCompetency.Items.Add(new ListItem("-- select competency --","0"));}}private void FillSubCategoryList(){try{if ((ddlCategory.SelectedValue !=null) && (ddlCategory.SelectedValue !="0")){using (DataAccessLayer objDAL =new DataAccessLayer("CompetencySubCategory_GetByCategoryID")){SqlParameter sqlparam =new SqlParameter("@.CatID", SqlDbType.Int);sqlparam.Value = ddlCategory.SelectedValue;objDAL.AddParameter(sqlparam);objDAL.ExecuteReader();ddlSubCategory.Items.Clear();ddlSubCategory.Items.Add(new ListItem("-- select subcategory --","0"));if (ddlCompetencyModel.SelectedIndex != 0){while (objDAL.DataReader.Read()){ddlSubCategory.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencySubCategoryID"])));}}}}else{using (DataAccessLayer objDAL =new DataAccessLayer("CompetencySubCategory_GetAll")){objDAL.ExecuteReader();ddlSubCategory.Items.Clear();ddlSubCategory.Items.Add(new ListItem("-- select subcategory --","0"));if (ddlCompetencyModel.SelectedIndex != 0){while (objDAL.DataReader.Read()){ddlSubCategory.Items.Add(new ListItem(MultiLanguageField.GetFieldValue(Convert.ToString(objDAL.DataReader["Name"])), Convert.ToString(objDAL.DataReader["CompetencySubCategoryID"])));}}}}// Even with/without these codes below still the sameif (ViewState["ddlSubCategory"] !=null && ddlSubCategory.Items.Count > 1){ddlSubCategory.SelectedValue = ViewState["ddlSubCategory"].ToString();ddlSubCategory.SelectedIndex = ddlSubCategory.Items.IndexOf(ddlSubCategory.Items.FindByValue(ViewState["ddlSubCategory"].ToString()));}}catch{ddlSubCategory.Items.Clear();}}
The first thing that comes to mind is your InitializeControls() method...
I see alot of "..." so not sure what that is comenting out...and probably irrelevant
However:
I would do this:
private void InitializeControls()
{
...
If (!Page.IsPostback)
{
FillCompetencyModelList();
...
FillCategoryList();
FillSubCategoryList();
FillCompetencyList();
FillSupervisorApproval(false);
FillLocalApproval(false);
...
FillCourseCode();
}
...
}
It appears you are always re-initializing your data which is not necessary...and may be always setting all of your databound controls to the initial page load values...
However your viewstate issue - you are never actually setting any viewstate values so:
// Even with/without these codes below still the same
if (ViewState["ddlSubCategory"] !=null && ddlSubCategory.Items.Count > 1)
{
ddlSubCategory.SelectedValue = ViewState["ddlSubCategory"].ToString();
ddlSubCategory.SelectedIndex = ddlSubCategory.Items.IndexOf(ddlSubCategory.Items.FindByValue(ViewState["ddlSubCategory"].ToString()));
}
1. You do not need to set the ddlSubCategory.Selected Value and SelectedIndex both at the same time...
2. Do not use the Viewstate of the control to try and re-select the controls select item - it may work but the whole point of the Events when the ddl is selectedindex changed is to already give you that value (in otherwords it is already selected)...in other words that code is not warranted or needed and nor will it ever actually do anything...
now if you were selecting a value in the ddlSubCategory based on say ddlSubCategory_Other then you would do this:
ddlSubCategory.SelectedValue = ddlSubCategory_Other.SelectedValue (if they matched values wise otherwise you would have to loop through the items and determine what should be selected..based on whatever matching criteria you have.)
Suggestions:
Do not reference viewstate for your selected index or values and definately do not try to use both SelectedIndex and SelectedValue...
Code Sample:
For instance here is a simple DDL that populates a string with a mode name:
modeName = dropFilterBy.SelectedValue.ToString();
{do something with it}
Now if I want to keep the modeName in viewstate this is what I would do...
publicstring modeName
{
get
{
return (string)ViewState["modeName"] !=null ?
(
string)ViewState["modeName"] :"Unknown";
}
set { ViewState["modeName"] =value; }
}
That allows me to use the ModeName in code to reference values and not call the dropFilterBy.SelectedValue.ToString(); for proccessing in other areas of the code where I may need the value...
Again the infinitesloop link I responded with earlier - he has two great series on viewstate and understading controls - easy to read and will shed even more info on how to properly use viewstate....and how the page life cycle all works...
jodywbcb:
The first thing that comes to mind is your InitializeControls() method...
I see alot of "..." so not sure what that is comenting out...and probably irrelevant
I think for this part of problem the values of 5 dropdownlist control they are irrelevant, because they are for other sections of the user control.
jodywbcb:
However:
I would do this:
private void InitializeControls()
{
...
If (!Page.IsPostback)
{
FillCompetencyModelList();
...
FillCategoryList();
FillSubCategoryList();
FillCompetencyList();
FillSupervisorApproval(false);
FillLocalApproval(false);
...
FillCourseCode();
}
...
}
It does not work, the data is still not retain, no change in situation
jodywbcb:
It appears you are always re-initializing your data which is not necessary...and may be always setting all of your databound controls to the initial page load values...
Hmmm...the child user control is inside an update panel control(Atlas control) of the parent user control, if I do not repopulate the data then after postback there will be no data.
jodywbcb:
However your viewstate issue - you are never actually setting any viewstate values so:
// Even with/without these codes below still the same
if (ViewState["ddlSubCategory"] !=null && ddlSubCategory.Items.Count > 1)
{
ddlSubCategory.SelectedValue = ViewState["ddlSubCategory"].ToString();
ddlSubCategory.SelectedIndex = ddlSubCategory.Items.IndexOf(ddlSubCategory.Items.FindByValue(ViewState["ddlSubCategory"].ToString()));
}
1. You do not need to set the ddlSubCategory.Selected Value and SelectedIndex both at the same time...
2. Do not use the Viewstate of the control to try and re-select the controls select item - it may work but the whole point of the Events when the ddl is selectedindex changed is to already give you that value (in otherwords it is already selected)...in other words that code is not warranted or needed and nor will it ever actually do anything...
now if you were selecting a value in the ddlSubCategory based on say ddlSubCategory_Other then you would do this:
ddlSubCategory.SelectedValue = ddlSubCategory_Other.SelectedValue (if they matched values wise otherwise you would have to loop through the items and determine what should be selected..based on whatever matching criteria you have.)
Suggestions:
Do not reference viewstate for your selected index or values and definately do not try to use both SelectedIndex and SelectedValue...
Don't follow you here, I just trying both lines of code to solve the problem..no difference.
jodywbcb:
Code Sample:
For instance here is a simple DDL that populates a string with a mode name:
modeName = dropFilterBy.SelectedValue.ToString();
{do something with it}
Now if I want to keep the modeName in viewstate this is what I would do...
publicstring modeName
{
get
{
return (string)ViewState["modeName"] !=null ?
(
string)ViewState["modeName"] :"Unknown";
}
set { ViewState["modeName"] =value; }
}
That allows me to use the ModeName in code to reference values and not call the dropFilterBy.SelectedValue.ToString(); for proccessing in other areas of the code where I may need the value...
Again the infinitesloop link I responded with earlier - he has two great series on viewstate and understading controls - easy to read and will shed even more info on how to properly use viewstate....and how the page life cycle all works...
Don't exactly follow you on this, are you telling not to use viewstate of control but use a ViewState object? Any other ideas?
This part of page appears when a user clicks a button either add/edit "item" link inside a Gridview.
Thanks for the reply..
Dennis
Forgot to add..
CompetencyModel
Category
SubCategory
2 more data
When Category is set to Item2, Item 1 is "-- select a category --", when I change the selected value on SubCategory, the value is retain after postback, but when Category is set to any other values or other dropdown values changes, the values is not being retain..
Please advise for ideas.
Correction previous issues, it seems when Dropdown 1 is set to a default value not item1 and so as Dropdown 2, values are being retain after postback, otherwise no values are retain... So, how do I retain values?