1

在 asp.net 页面中,我有一个预定义的下拉菜单

<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>

在后面的代码中,我试图根据对数据库的查询来设置选定的值。

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;
            }
        }

我尝试使用 onInit 以及在页面加载期间调用此方法

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();
        }

当开关逐步通过并更新对象时,我从未在下拉菜单更新中看到文本。它始终保持设置为 10 个结果的第一项。

我尝试将控件包装在 udpatepanel 中,然后在面板上调用更新以查看是否可以在渲染和页面加载时强制刷新。

我也尝试过而不是使用Items[0].Selected我尝试将选定的索引设置为:

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);
}

仍然没有运气。对于找出为什么此下拉菜单无法更新和表示/显示存储在数据库中的正确文本的任何帮助,我将不胜感激。

-干杯

4

2 回答 2

1

为什么不使用DropDownList.SelectedValue物业???

例如:

ddResultNum.SelectedValue = resultValue;
于 2012-07-31T23:02:59.030 回答
0

您需要SelectedIndex在 DDL 本身上设置该属性。像这样(记住,索引是从零开始的):

        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;
        }
于 2012-07-31T22:53:31.687 回答