• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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

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
donkerr
Asked:
donkerr
  • 7
  • 5
  • 2
3 Solutions
 
Praveen VenuTechnical Project ManagerCommented:
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
 
JacobBushongCommented:
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
 
donkerrAuthor Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
JacobBushongCommented:
if dropdownlist1.selectedvalue = wateverValue
{
dropdownlist2.visible= true
}

That should work.  How are you making the ddl invisible in the first place?
0
 
donkerrAuthor Commented:
Actually, after screwing with it, now I get an error msg that says I cannot implicity convert type string to bool
0
 
Praveen VenuTechnical Project ManagerCommented:
try this

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

Open in new window

0
 
JacobBushongCommented:
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
 
donkerrAuthor Commented:
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
 
donkerrAuthor Commented:
Jacob,
dropping the second(or first) = caused an error of cannot implicity convert a string to a bool
0
 
donkerrAuthor Commented:
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
 
JacobBushongCommented:
Sorry, I meant to have double quotes.  Too much SQL coding, I guess....haha
0
 
donkerrAuthor Commented:
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
 
JacobBushongCommented:
oh, you don't need quotes around true.  True is a bool value (at least it is in vb).  
0
 
donkerrAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now