Solved

Cannot check when DropDownList is empty

Posted on 2009-05-16
6
252 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:fwstealer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24403114
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
0
 

Author Comment

by:fwstealer
ID: 24403161
same - it is always true
0
 

Author Comment

by:fwstealer
ID: 24403165
pageload is where i began then move to the SqlDataSource1_DataBinding
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 11

Accepted Solution

by:
srikanthreddyn143 earned 500 total points
ID: 24403181
Try keeping this  in dropdown list databound event
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24403887
Try using your if...else code in the Selected event of the SqlDataSource rather than DataBinding.
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24403888
You may also move your code into Page_PreRender event.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question