Showing posts with label dropdownlist. Show all posts
Showing posts with label dropdownlist. Show all posts

Monday, March 26, 2012

trigger for control that is dynamically created on databind

I have a dropdownlist that is created when the gridview is databound. This is because the dropdownlist is contained in the headertemplate of a templatefield column. So I have tried to add the trigger dynamically right after the gridview is databound, but I keep getting an error saying that the controlID wasn't found...I don't know what is wrong. I can find the control inside the the gridview with no problem using the findControl fuction.


TimeStampGridView.DataBind();
DropDownList StatusFilterDLL=(DropDownList)TimeStampGridView.HeaderRow.Cells[2].FindControl("StatusFilter");//finds the Control
StatusFilterDLL.SelectedIndex = StatusFilterDLL.Items.IndexOf(StatusFilterDLL.Items.FindByText(StatusFilter));//Works fine
AsyncPostBackTrigger trigger =new AsyncPostBackTrigger();
trigger.ControlID ="StatusFilter";
trigger.EventName ="SelectedIndexChanged";
GridUpdatePanel.Triggers.Add(trigger);//error:A control with ID 'StatusFilter' could not be found for the trigger in UpdatePanel 'GridUpdatePanel'


Anyone know what I'm doing wrong here?

Thanks for the help in advance!

I removed all the code for the dynamic trigger and set the dropdownlist's autopostback= true so now when the user changes the selectedindex the gridview is updated

Wednesday, March 21, 2012

Trouble Retaining values entered or selected for controls inside an update panel

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?

Trying to hide controls on UpdatePanel

I have an UpdatePanel on which there sits a dropdownlist of providers and two labels that show the providers AU and Activity #. When the selectedIndex of the dropdown is zero, I want to hide the labels. I have tried both asp:panel controls with height=0 set in the code behind SelectedIndexChanged event and html div with style.visibility = 'hidden' in a javascript onchange event for the dropdown, and neither does anything. No errors, it just doesn't do anything. Here is my page code.

<asp:UpdatePanel id="UpdatePanel1" runat="server">
<contenttemplate>
<asp:DropDownList id="ddlProviders" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlProviders_SelectedIndexChanged"></asp:DropDownList>
<BR />
<DIV style="WIDTH: 100%; HEIGHT: 100%" id="divProviderDetail" noWrap>
<asp:Label id="Label1" runat="server" Text="AU#: " __designer:wfdid="w20"></asp:Label>
<asp:Label id="lblProviderAU" runat="server" __designer:dtid="844424930132022" __designer:wfdid="w17"></asp:Label> <BR />
<asp:Label id="Label2" runat="server" Text="Activity #: " __designer:wfdid="w21"></asp:Label>
<asp:Label id="lblActivityNumber" runat="server" __designer:dtid="844424930132024" __designer:wfdid="w18"></asp:Label>
</DIV>
<BR />
</contenttemplate>
</asp:UpdatePanel>

Any ideas? Thanks.

Protected Sub ddlProviders_SelectedIndexChanged(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles ddlProviders.SelectedIndexChanged
If (ddlProviders.SelectedIndex = 0)Then
lblProviderAU.Visible =False
lblActivityNumber.Visible =False
Else
lblProviderAU.Visible =True
lblActivityNumber.Visible =True
End If
End Sub

Or you could use a panel and set Visible = False for that.


whats the code you using at ddlProviders_SelectedIndexChanged

coz the aspx looks fine here


When I used a panel, I set height=0 so as to not leave a big blank spot. That didn't do anything, height was still the same.


If you don't want anything to move, then just change their text to "" rather than changing their height or visibility.


Setting an element's display CSS property to 'none' is a good way to hide elements on the client side. It has the advantage of not removing them from the DOM or rendered HTML (as is the case with those other methods), so you can still manipulate them on the client side later if need be.


document.getElementById('lblAU').class = 'none';

gave me an error when VS scanned the .js file at start. What is the correct syntax?


document.getElementById('lblAU').style.display = 'none';

or if you have a css class

.hide {display: none;}

document.getElementById('lblAU').className = 'hide';