David

David

  • NA
  • 3
  • 0

Trouble with 'FindByValue' ..

Jan 8 2008 5:40 PM
Hi my first post and sorry its a question ...

I have a databound DropDownList which i need c# code to check the selected value is there or not.

ASP Code:
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
                                        SelectCommand="SELECT [CountryId], [CountryName] FROM [Countries] WHERE ([Active] = @Active2) ORDER BY [CountryName]">
                                            <SelectParameters>
                                            <asp:Parameter DefaultValue="true" Name="Active2" Type="Boolean" />
                                        </SelectParameters>
                                    </asp:SqlDataSource>
<asp:DropDownList ID="countriesDropDownList" runat="server" DataSourceID="SqlDataSource1"
                                        DataTextField="CountryName" DataValueField="CountryId" AppendDataBoundItems="True" OnDataBinding="countriesDropDownList_SelectedIndexChanged">
                                        <asp:ListItem Value="-1" Selected="True">Please Select</asp:ListItem>
                                    </asp:DropDownList>

C# Code:
string BillCountryId = reader["BillCountryId"].ToString();

                    TextBox1.Text = BillCountryId;

                    ListItem li = countriesDropDownList.Items.FindByValue(BillCountryId);
                    if (li != null)
                     countriesDropDownList.SelectedValue = BillCountryId;

Problem is, its not finding any values which have been DataBound, only the (-1) of the 'Please Select' value.  The C# Code is in the Page_Load void ..

Please help! :)

Answers (2)