简体   繁体   中英

C# DropDownlist default value -1 but displaying incorrectly

I am running into an extremely frustrating problem I'm hoping everyone can help with.

I have two sets of Dropdownlists; The first one is populated from a database call, and it is simply a unique list of clients. The second one is a list of servicers; and it should be FILTERED by the results of the first Dropdownlist.

Note the main problem seems to be that the first drop down list result of "DropdownListClient.SelectedIndex.ToString()" is showing a value of -1. however when teh web page is launched the drop down list only has two options.

Value List Description 0 Green Tree 1 Chase

So why is the DEFAULT selected value of the Dropdownlist equal to -1? I can't seem to find anything that would set the default to whatever value is displayed in the dropdown list.

my code behind looks like this..

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

        }                         
    }

And my asp.net page looks like...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM