fwstealer
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
}
}
ASKER
same - it is always true
ASKER
pageload is where i began then move to the SqlDataSource1_DataBinding
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
{
btnAdd.Enabled = false; //ignored
}
else
{
btnAdd.Enabled = true; //always the case regardless
}
keep this part in page load and try it