?
Solved

ASP.NET how do I set one dropdown list to visible based on value of another dropdown list

Posted on 2009-02-11
14
Medium Priority
?
347 Views
Last Modified: 2012-08-13
I have two dropdown lists - I want one to open based on a value selected in another list.
protected void TYPE_RECORD_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (TYPE_RECORD.SelectedValue == "Pt Deferred")
        {
            Deferred.Visible= "true";
           
        }
 
 <table border="0" style="font-size: x-small" width="80%">
                    <tr class="fmtable">
                       
                        <td>Type Record:
                            <asp:DropDownList ID="TYPE_RECORD" AutoPostBack="True" runat="server" DataSourceID="SqlDataSource2" AppendDataBoundItems="true"
        DataTextField="TYPE" DataValueField="TYPE_ID" OnSelectedIndexChanged="TYPE_RECORD_SelectedIndexChanged">
        <asp:ListItem Value=" "></asp:ListItem>
 
    </asp:DropDownList> 
                        </td>
                        <td>Defer Status:
                        <asp:DropDownList ID="Deferred"  AutoPostBack="True"  Visible="false" runat="server" DataSourceID="SqlDataSource3" AppendDataBoundItems="true"
        DataTextField="TYPE_ID" DataValueField="TYPE_ID">
        <asp:ListItem Value=" "></asp:ListItem>
 
    </asp:DropDownList> 
    }

Open in new window

0
Comment
Question by:donkerr
  • 7
  • 5
  • 2
14 Comments
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23611446
put the html code in aspx and code in cs

ASPX
====
 
 <table border="0" style="font-size: x-small" width="80%">
                    <tr class="fmtable">
                       
                        <td>Type Record:
                            <asp:DropDownList ID="TYPE_RECORD" AutoPostBack="True" runat="server" DataSourceID="SqlDataSource2" AppendDataBoundItems="true"
        DataTextField="TYPE" DataValueField="TYPE_ID" OnSelectedIndexChanged="TYPE_RECORD_SelectedIndexChanged">
        <asp:ListItem Value=" "></asp:ListItem>
 
    </asp:DropDownList> 
                        </td>
                        <td>Defer Status:
                        <asp:DropDownList ID="Deferred"  AutoPostBack="True"  Visible="false" runat="server" DataSourceID="SqlDataSource3" AppendDataBoundItems="true"
        DataTextField="TYPE_ID" DataValueField="TYPE_ID">
        <asp:ListItem Value=" "></asp:ListItem>
 
    </asp:DropDownList> 
 
 
CS file
========
 
 
protected void TYPE_RECORD_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (TYPE_RECORD.SelectedValue == "Pt Deferred")
        {
            Deferred.Visible= "true";
           
        }
    }

Open in new window

0
 
LVL 3

Expert Comment

by:JacobBushong
ID: 23611452
insert the following code in the code-behind for the original dropdownlist. (dropdownlist1_selectedindexchanged)

if dropdownlist1.selectedvalue = whateverValue then
dropdownlist2.visible = true
end if

Turn autopostback to true for ddl1
0
 

Author Comment

by:donkerr
ID: 23611512
1.  I left the html tags off because there is more to the aspx than just the two dropdownlist and the C# code is in the cs page.

2.  I am using C#, but if I look at the answer you gave me in VB it is really the same as what I have.  Auto post back is already set.

Page opens and I can select the value Pt Deferred and it posts back to the page, but the other dropdownlist does not appear magically or otherwise.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:JacobBushong
ID: 23611596
if dropdownlist1.selectedvalue = wateverValue
{
dropdownlist2.visible= true
}

That should work.  How are you making the ddl invisible in the first place?
0
 

Author Comment

by:donkerr
ID: 23611612
Actually, after screwing with it, now I get an error msg that says I cannot implicity convert type string to bool
0
 
LVL 15

Assisted Solution

by:Praveen Venu
Praveen Venu earned 1000 total points
ID: 23611624
try this

  if (TYPE_RECORD.SelectedValue == "Pt Deferred")
            Deferred.Visible= true;
  else
            Deferred.Visible= false;

Open in new window

0
 
LVL 3

Assisted Solution

by:JacobBushong
JacobBushong earned 1000 total points
ID: 23611633
if dropdownlist1.selectedvalue = 'whateverValue'
{
dropdownlist2.visible= true
}

Change whateverValue to 'whateverValue'
You may be getting that if your value behind the ddl is a string and you don't have quotes around it in your code-behind.
0
 

Author Comment

by:donkerr
ID: 23611674
praveenvenu,
no errors with that, but the second dropdownlist is not visible after I sellect Pt Deferred.  
I assume the ddl can be referred to by its ID, which in the case of the second dropdown is Deferred.



0
 

Author Comment

by:donkerr
ID: 23611720
Jacob,
dropping the second(or first) = caused an error of cannot implicity convert a string to a bool
0
 

Author Comment

by:donkerr
ID: 23611745
Jacob,

Didn't notice you had changed to single quotes, when I do that I get an error of too many characters in character literal
0
 
LVL 3

Expert Comment

by:JacobBushong
ID: 23611789
Sorry, I meant to have double quotes.  Too much SQL coding, I guess....haha
0
 

Author Comment

by:donkerr
ID: 23611819
With double quotes, I get the error regarding converting string to bool .  I assume that has something to do with setting the Deferred.visible = "true"
0
 
LVL 3

Expert Comment

by:JacobBushong
ID: 23611862
oh, you don't need quotes around true.  True is a bool value (at least it is in vb).  
0
 

Accepted Solution

by:
donkerr earned 0 total points
ID: 23611933
Jacob,
The code is now basically what praveenvenu sent, but that puts me back at the beginning - form opens, can select Pt Deferred from first dropdown, it posts back, but second dropdown does not appear.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses

830 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