0

I have two dropdownlists inside an ASP.Net AJAX Update Panel. One dropdownlist populates the second through a postback when a user selects a value from the first, and this all works well. However, the problem I have is that I am unable to grab the selectedvalue from the second dropdownlist, by this I mean it always selects the first item rather than the actual selected value.

I have looked at moving the populating the second dropdownlist to If Not IsPostBack Then on the page load, but this then stops the populating of that dropdownlist when the AJAX Update Panel does a partial postback.

So, there is away out of this?

Thanks

Andy5
  • 2,319
  • 11
  • 45
  • 91
  • 1
    can you please post some code? – codingbiz Oct 26 '12 at 22:53
  • Which part of the code do you want to see? On the page_load? – Andy5 Oct 26 '12 at 22:56
  • Are you trying to get the value out on Page Load? If so, you probably won't because the viewstate hasn't loaded yet. – Noel Oct 26 '12 at 22:56
  • Sorry, Noel - not clear what you are trying to ask me? – Andy5 Oct 26 '12 at 22:57
  • where you are getting the selected value, your page_load,...? – codingbiz Oct 26 '12 at 22:59
  • I won't bother with the ASP.Net page mark as it an AJAX update panel containing two dropdownlists with the first dropdownlist with autopostback = "true". On Page_Load I have the following If IsPostBack Then ddlSite.Datasource = GetSites() ddlSite.Databind() ddlPlant.DataSource() = GetPlant(ddlSite.SelectedValue) ddlPlant.Databind() End If – Andy5 Oct 26 '12 at 23:03
  • The actual design pattern I am using here is a bit like MVC. The selected value from the second dropdownlist is when I am about to use it on another user control on a second step of a wizard control, where it is bound to that user control through my controller class, Noel – Andy5 Oct 26 '12 at 23:07

2 Answers2

0

If you are changing the value of the second dropdown based on the value selected on the first, you should use the SelectedIndexChanged event of the first dropdown to set the value.

Your code (from comments):

On Page_Load I have the following

If (IsPostBack)
{ 
    ddlSite.Datasource = GetSites();
    ddlSite.Databind();
    ddlPlant.DataSource() = GetPlant(ddlSite.SelectedValue);
    ddlPlant.Databind();
}

On page load the ddlSite.SelectedValue is always zero.

You need to bind the second drop down in the ddlSite_SelectedIndexChanged event.

Page Lifecycle question answered

Community
  • 1
  • 1
Noel
  • 600
  • 16
  • 37
  • No that is not what I am doing here. The populating of the whole dropdownlist is based on a selectedvalue of the first. This is not the issue. When I select a value from the second dropdownlist I am getting the first item in the list when I go to use the value else where in my app. – Andy5 Oct 26 '12 at 23:13
  • Please review my comment above. I am not changing the value on the second dropdownlist. I can't get my selected value from my second dropdownlist as it is only picking the first item in its list of values – Andy5 Oct 26 '12 at 23:21
  • where is the SelectedIndexChanged event handler? – codingbiz Oct 26 '12 at 23:21
  • Right, he isn't using it, other than to get a postback. – Noel Oct 26 '12 at 23:22
  • Why do I need to use SelectedIndexChanged event on the first dropdownlist? It is the second dropdownlist I am interested here. – Andy5 Oct 26 '12 at 23:26
  • I guess if your only postback is going to be the change of first dropdown, that is the case, but when you change the second drop down that also causes a postback and resets the first dropdown and the second. – Noel Oct 26 '12 at 23:28
  • Both dropdownlists have values, I can get the value from the first dropdownlist, but I am unable to grab the value from the second even though there are values visible in the list. – Andy5 Oct 26 '12 at 23:30
  • No postback is conditional in the AJAX Update Panel and is only fired when a value is selected from the first dropdownlist. I am expecting the values in the second dropdownlist to change when a user selects a new value from the first - that is expected behaviour – Andy5 Oct 26 '12 at 23:32
0

here is an example if it helps you..its cascading dropdown with datasource. http://www.aspdotnet-suresh.com/2011/01/introduction-here-i-will-explain-how-to.html

user704988
  • 436
  • 1
  • 9
  • 24