Link to home
Start Free TrialLog in
Avatar of fwstealer
fwstealerFlag for United States of America

asked on

Cannot check when DropDownList is empty

I have a ddl that is being populated when the page loads. What I need to do is set the status of a button to enable of false if the ddl has 0 items. Any suggestions?

<asp:DropDownList ID="ddlP" runat="server" DataSourceID="SqlDataSource1" 
                                                            DataTextField="FullName" DataValueField="PId">
                                                        </asp:DropDownList>
                                                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                                            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                                            
                                                        SelectCommand="SELECT blah... " 
                                                        ondatabinding="SqlDataSource1_DataBinding">
                                                            <SelectParameters>
                                                                <asp:ProfileParameter Name="UserId" PropertyName="SCId" 
                                                                    Type="String" />
                                                            </SelectParameters>
                                                        </asp:SqlDataSource>                                                    
 
 
    protected void SqlDataSource1_DataBinding(object sender, EventArgs e)
    {
        //if (e.AffectedRows == 0) //no good
        if (ddlPatients.Items.Count == 0)
        {
            btnAdd.Enabled = false; //ignored
        }
        else
        {
            btnAdd.Enabled = true; //always the case regardless
        }
    }

Open in new window

Avatar of srikanthreddyn143
srikanthreddyn143

if (ddlPatients.Items.Count == 0)
        {
            btnAdd.Enabled = false; //ignored
        }
        else
        {
            btnAdd.Enabled = true; //always the case regardless
        }


keep this part in page load and try it
Avatar of fwstealer

ASKER

same - it is always true
pageload is where i began then move to the SqlDataSource1_DataBinding
ASKER CERTIFIED SOLUTION
Avatar of srikanthreddyn143
srikanthreddyn143

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try using your if...else code in the Selected event of the SqlDataSource rather than DataBinding.
You may also move your code into Page_PreRender event.