dropdown list selectedindex changed

forsters
forsters used Ask the Experts™
on
Hi All

I have a dropdown list which is databound to a sqltable giving 3 options and I want to use the selectedindexchanged command to determine the remainder of the form:

so if the user selects item 1 I want them to see one drop down list,

if they select item two I want them to see something else

etc

is this asking too much?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
StephanLead Software Engineer

Commented:
You can use the SelectedIndexChanged event from the dropdownlist (remember to enable AutoPostBack on the dropdownlist).. And in the event you can do what you like; e.g. generate one, hide/show etc...
try this,
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="dropdown.aspx.vb" Inherits="dropdown" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <script>
      
        function showtextbox1() {
            if (document.getElementById("sponsorshiplevel").value == "1") {
                document.getElementById("txtValue").style.display = "block";
                document.getElementById("test").style.display = "none";
            }
            else {
                document.getElementById("test").style.display = "block";
                document.getElementById("txtValue").style.display = "none";
            }

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:DropDownList id="sponsorshiplevel" runat="server" onchange="return showtextbox1()">
    <asp:ListItem>Select Value</asp:ListItem>
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    </asp:DropDownList>
    <input type="text" id="txtValue" name="input_B" size="10" value="0" visible="true" style="background-color:Yellow;display:none"/> 
    <asp:Label ID="test" runat="server" Text="hi different for 2nd value" style="display:none"></asp:Label>
    
    
    </div>
    </form>
</body>
</html>

Open in new window

Author

Commented:
Thanks, I tried, but there are errors:
I get a validtaion error that onchange is not a valid ASP.NET attribute for dropdownbox.
I also get a runtime error in the function -  the first line of the if statement says object required.

I should mention that I'm moving across to ASP.NET 4.0 C# & Visual Studio, so I'm learning from books which have a habit of leading you on and then starting a new chapter at the crucial moment.

 I have working examples to use selectedindexchanged to change the array of the next drop down so I suppose I could take that approach but what I really want to do is be able to say if item one is selected show the following (which in my mind will be a mixture of dropdownlists and textboxes - the rest of my form) if item two is selected however I probably only need to show one drop down and a textbox, so can I put these items into  'views' or something like that and say if item one is selected show view one else if item two seleted show view two.

 I'm thinking more along the AJAX lines if possible...I just can't find a good example of how to set this up which makes me think its either too complex or i'm so far off-target that i'm looking in the worng place.

Can anyone give me some pointers...  
Commented:
You can do this server side.  You'll need to connect an event to the control then put some code in your code behind.

<asp:dropdown id="ddlMyList" runat="server" OnSelectedIndexChanged="ddlMyList_OnSelectedIndexChanged" AutoPostback="true" />


In code behind:

protected void ddlMyList_OnSelectedIndexChanged(object sender, EventArgs e)
{
      string myValue = ddlMyList.SelectedValue;
      string myText = ddlMyList.SelectedText;

      //do lots of stuff here

}

If you put your dropdown and other controls inside an update panel your postback will be as smooth as silk.

Author

Commented:
Update Panel - thank you that was the control I knew I'd seen somewhere, thats what I need to look at.

Ok thanks gmb64 your example is equivalent to the examples I've been reading about.

 I presumably just need to dig a little deeper into the C# stuff to get a bit more of a handle on the //do lots of stuff here part, which is fair enough.

Can you reccomend any learning resources?



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial