Solved

Cannot check when DropDownList is empty

Posted on 2009-05-16
6
246 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:srikanthreddyn143
Comment Utility
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
Comment Utility
same - it is always true
0
 

Author Comment

by:fwstealer
Comment Utility
pageload is where i began then move to the SqlDataSource1_DataBinding
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 11

Accepted Solution

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

Expert Comment

by:Muhammad Ousama Ghazali
Comment Utility
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
Comment Utility
You may also move your code into Page_PreRender event.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now