How to work with dropdown control from a usercontrol

I've an user control where as a dropdown control is placed there. I placed the usercontrol in my aspx page. How to get the dropdown value in ASPX page and how and where to handle the selection changed event of the dropdown?

Please guide me.
LVL 16
Easwaran ParamasivamAsked:
Who is Participating?
 
Luis PérezConnect With a Mentor Software Architect in .NetCommented:
Well, if you got a UserControl with a DropDownList inside, you need a way to populate the DropDownList selected value to the Usercontrol's container page. You can do this thru a property in the UserControl:

 public ListItem SelectedItem {
        get {
            return this.DropDownList1.SelectedItem;
        }
    }

Open in new window

So, in the container page, you can do this (for example, in page_load):

if (this.IsPostBack) {
    ListItem item = this.WebUserControl1.SelectedItem;
}

Open in new window


And into javascript, you can check at every moment the selected item of the dropdownlist:
<script type="text/javascript">
    function checkDropDown() {
        var selectedValue = document.getElementById('<%=DropDownList1.ClientID%>').value;
    }
</script>

Open in new window


Note that you must place that Javascript inside the UserControl.

And about how to handle the selection changed event, you can use the AutoPostback property of the DropDownList. If you set AutoPostback to true, every time the user changes the selection the page reloads, and you can check the selected value in Page_Load event (you can use this.IsPostBack to know if the page has reloaded or not). If you prefer to detect changes in javascript, you must add this code to the UserControl's Page_Load method:

this.DropDownList1.attributes.add('onchange','dropDownListChanged(this)');

Open in new window


And you must write this function to handle the change in javascript:

function dropDownListChanged(dropdown) {
    //you can access dropdown.value to know the selected value
}

Open in new window


Because the question you are posting, I guess you must be a bit newbie in ASP.net. I know this can be messy. Good luck. Hope that helps.
0
 
Luis PérezSoftware Architect in .NetCommented:
Are you working exclusively with server code or with Javascript too?
0
 
Easwaran ParamasivamAuthor Commented:
Please tell me both.
0
 
Easwaran ParamasivamAuthor Commented:
Thanks.
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.

All Courses

From novice to tech pro — start learning today.