?
Solved

How to work with dropdown control from a usercontrol

Posted on 2012-09-04
4
Medium Priority
?
807 Views
Last Modified: 2012-09-05
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.
0
Comment
Question by:Easwaran Paramasivam
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 38367212
Are you working exclusively with server code or with Javascript too?
0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 38367230
Please tell me both.
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 2000 total points
ID: 38367309
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
 
LVL 16

Author Closing Comment

by:Easwaran Paramasivam
ID: 38367426
Thanks.
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 I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
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