Reputation: 3562
within an asp.net page I have a predefined drop down menu
<asp:DropDownList CssClass="queryterm dropdownSpacer" ID="ddResultNum" runat="server">
<asp:ListItem Text="10 Results " Value="10" />
<asp:ListItem Text="20 Results" Value="20" />
<asp:ListItem Text="30 Results" Value="30" />
<asp:ListItem Text="40 Results" Value="40" />
<asp:ListItem Text="50 Results" Value="50" />
<asp:ListItem Text="100 Results" Value="100" />
</asp:DropDownList>
In the code behind I am trying to set the selected value based upon a query to the database.
protected void SetResultsDropDown()
{
string selectCommand = String.Format("SELECT ResultsReturned FROM StoredFilters WHERE SecurityUserID = {0}", securityUserId);
string resultValue = DatabaseUtilities.QueryDatabase(selectCommand, ConfigurationManager.AppSettings["informconnectstring"].ToString(), "ResultsReturned");
//based upon the users id the db stores the set number from the drop down the default is 10 however if the number changes when the page loads I want the user to see what their current result set being returned is in the drop down vs. it defaulting to the first option
ddResultNum.ClearSelection();
switch (resultValue)
{
case "10":
ddResultNum.Items[0].Selected = true;
break;
case "20":
ddResultNum.Items[1].Selected = true;
break;
case "30":
ddResultNum.Items[2].Selected = true;
break;
case "40":
ddResultNum.Items[3].Selected =true;
break;
case "50":
ddResultNum.Items[4].Selected = true;
break;
case "100":
ddResultNum.Items[5].Selected = true;
break;
}
}
I have tried calling this method using onInit as well as during the page load
protected override void OnInit( EventArgs e)
{
base.OnInit(e);
// Extract an Inform User ID from the Token.
IClaimsIdentity claimsIdentity = ((IClaimsPrincipal)(Thread.CurrentPrincipal)).Identities[0];
securityUserId = Convert.ToInt64(claimsIdentity.Claims.Where(c =>
c.ClaimType == AuthenticationConstants.INFORM_USER_ID_CLAIM_TYPE).First().Value);
SetResultsDropDown();
}
While the switch steps through and updates object I never see the text within the drop down menu update. It always stays set to the first item of 10 results.
I have tried wrapping the control in an udpatepanel and then calling an update on the panel to see if I could force the refresh on render and page load.
I have also tried instead of using Items[0].Selected
I have tried to set the selected index as:
protected void SetResultsDropDown()
{
string selectCommand = String.Format("SELECT ResultsReturned FROM StoredFilters WHERE SecurityUserID = {0}", securityUserId);
string resultValue = DatabaseUtilities.QueryDatabase(selectCommand, ConfigurationManager.AppSettings["informconnectstring"].ToString(), "ResultsReturned");
ddResultNum.ClearSelection();
ddResultNum.SelectedIndex = ddResultNum.Items.FindByValue(resultValue);
}
and still no luck. I'd appreciate any help on figuring out why this drop down menu fails to update and represent / display the correct text for what is stored in the database.
-Cheers
Upvotes: 1
Views: 4698
Reputation: 21365
Why don't you use the DropDownList.SelectedValue
property???
For example:
ddResultNum.SelectedValue = resultValue;
Upvotes: 1
Reputation: 6249
You need to set the SelectedIndex
property on the DDL itself. Like so (remember, the index is zero-based):
switch (resultValue)
{
case "10":
ddResultNum.SelectedIndex = 0;
break;
case "20":
ddResultNum.SelectedIndex = 1;
break;
case "30":
ddResultNum.SelectedIndex = 2;
break;
case "40":
ddResultNum.SelectedIndex = 3;
break;
case "50":
ddResultNum.SelectedIndex = 4;
break;
case "100":
ddResultNum.SelectedIndex = 5;
break;
}
Upvotes: 0