【问题标题】:C# DropDownlist default value -1 but displaying incorrectlyC# DropDownlist 默认值 -1 但显示不正确
【发布时间】:2015-12-22 19:55:38
【问题描述】:

我遇到了一个非常令人沮丧的问题,希望每个人都可以提供帮助。

我有两组下拉列表;第一个是从数据库调用中填充的,它只是一个唯一的客户端列表。第二个是服务者列表;它应该被第一个下拉列表的结果过滤。

请注意,主要问题似乎是“DropdownListClient.SelectedIndex.ToString()”的第一个下拉列表结果显示值为 -1。然而,当网页启动时,下拉列表只有两个选项。

Value List Description 0 Green Tree 1 Chase

那么为什么Dropdownlist的DEFAULT选择值等于-1呢?我似乎找不到任何可以将默认设置为下拉列表中显示的任何值。

我后面的代码是这样的..

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = User.Identity.Name;
        if (!Page.IsPostBack)
        {
Documenation_ClientProject.SelectParameters.Add("Personlookup",User.Identity.Name);
Documenation_ClientProject.SelectCommand = "SELECT distinct Clientname FROM [FileReviewProject] where personid=@Personlookup";
Label2.Text = DropDownListClient.SelectedIndex.ToString();
/*shows a -1 but should show a 0 (since the displayed result in the first    drop down list is the 0 value of green Tree*/
Documenation_ServicerProject.SelectParameters.Add("ServDef",    DropDownListClient.Text);
Documenation_ServicerProject.SelectCommand = "SELECT distinct LoanServicer    FROM [FileReviewProject] where clientname=@ServDef";

        }                         
    }

我的 asp.net 页面看起来像......

<asp:SqlDataSource ID="Documenation_ClientProject" runat="server"
 ConnectionString="<%$ ConnectionStrings:DEVSQLCATConnection%>"> 
</asp:SqlDataSource>

<asp:SqlDataSource ID="Documenation_ServicerProject" runat="server"
 ConnectionString="<%$ ConnectionStrings:DEVSQLCATConnection%>"> 
</asp:SqlDataSource>


<asp:DropDownList ID="DropDownListClient" runat="server" DataSourceID="Documenation_ClientProject" DataTextField="ClientName" DataValueField="ClientName" AutoPostBack="True" OnSelectedIndexChanged="DropDownListClient_SelectedIndexChanged">
</asp:DropDownList>

<asp:DropDownList ID="DropDownServicer" runat="server" DataSourceID="Documenation_ServicerProject" DataTextField="LoanServicer" DataValueField="LoanServicer">
  </asp:DropDownList>

【问题讨论】:

  • 通常,.NET 中下拉菜单/组合框的默认值是无选择 (= -1)。例如:对于元素 1、2、3,它不显示 1,而是显示一个空格(转换为索引的 -1)。 “更正”这很容易:只需在应用程序启动时选择所需的索引。例如:DropdownListClient.SelectedIndex = 0;.

标签: c# drop-down-menu


【解决方案1】:

尚未选择任何内容,所以 DropdownListClient.SelectedIndex 当然是 -1,在设置 Label2.Text 之前将索引设置为 0 DropdownListClient.SelectedIndex = 0 应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    相关资源
    最近更新 更多