Valhalla Legends Forums Archive | .NET Platform | asp:DropdownList

AuthorMessageTime
Imperceptus
I have a drop downlist on a asp.net form that i populate with list items during the load. It looks like
[code]<asp:DropDownList ID="_RiskTolerance" runat="server"
                    Width="150px"></asp:DropDownList>
[/code]
The CodeBehind I use to add items looks like
[code]
    Private Sub FillCombobox(ByVal ComboControl As DropDownList, ByVal dsCombo As DataSet)
        Dim myItem As ListItem
        For Each row As DataRow In dsCombo.Tables(0).Rows
            myItem = New ListItem(row(1), row(0))
            ComboControl.Items.Add(myItem)

        Next
    End Sub
[/code]

I have tried to figure out what the user is setting the value of a in a dropdownlist.  So far nothing returns what the user chooses.  Item.SelectedItem.value and Item.SelectedIndex both seem like the right choice but the dont return what I thought.

One example
[code]
<select name="ctl00$ContentPlaceHolder1$_RiskTolerance" id="ctl00_ContentPlaceHolder1__RiskTolerance" style="width: 150px;">
<option selected="selected" value="1">High Risk</option>
<option value="2">Average Risk</option>
<option value="3">Low Risk</option>
<option value="4">No Risk</option>

</select>
[/code]
Say I choose Option 4 in my browser and click a button on the page to start the processing of the information on the page.
If I reference _RiskTolerance.SelectedIndex.Value Or _RiskTolerance.SelectedItem.Value, neither of them return a value anywhere close to what I am looking for.

As far as I can figure I have to enable postback for the dropdown for it to fire the "On" events, and preferably I dont want to use postback for this.

Thoughts?
August 5, 2009, 9:35 PM
Myndfyr
What value are you expecting?
August 6, 2009, 4:19 AM
Imperceptus
well for that drop down box, had i choosen the last value I would rather get a 3 for index or a 4 for selected value.  but instead i get a 0 for index and a 1 for selected value. 

From what I am seeing it doesn't seem to fire any of the "on" events until postback, which sucks.  Any idea's as whether the Ajax drop downs from ajaxtoolkit have the same problem?
August 6, 2009, 12:54 PM
Myndfyr
[quote author=Imperceptus link=topic=18033.msg183176#msg183176 date=1249563241]
well for that drop down box, had i choosen the last value I would rather get a 3 for index or a 4 for selected value.  but instead i get a 0 for index and a 1 for selected value. 

From what I am seeing it doesn't seem to fire any of the "on" events until postback, which sucks.  Any idea's as whether the Ajax drop downs from ajaxtoolkit have the same problem?
[/quote]When are you populating your combo box?  For instance:
[code]
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        FillComboBox(_RiskTolerance, myData);
}
[/code]

You can cause a postback to be raised by setting the drop down list's AutoPostBack property to true.  However, all server-side controls only have their events triggered when a postback is triggered.
August 6, 2009, 2:15 PM
Imperceptus
Postback was something i was trying to avoid, but I think its the only way atm.  Thanks for the info about server-side controls though, thats good to know.
August 6, 2009, 3:29 PM
Myndfyr
If you're using 3.5 and can reference System.Web.Extensions (alternatively you can use it in 2.0 as long as you have ASP.NET AJAX 1.0 Extensions), you can avoid a complete postback with a couple additions:

[code]
<asp:ScriptManager runat="server" EnablePartialRendering="true" ID="smgr" />
[/code]

[code]
<asp:UpdatePanel ID="upd" runat="server">
    <ContentTemplate>
        <asp:DropDownList AutoPostBack="true" .... />
        <%-- Other controls that should be modified by the postback should be placed here --%>
    </ContentTemplate>
</asp:UpdatePanel>
[/code]

I personally don't like using UpdatePanel but it can help when something needs to be fast.  See "UpdatePanel Considerations" (near the end) of this article - I wrote it (it's ghostwritten, the guy who paid didn't tell me it would be).  Those are my thoughts.  But like I said, it can be helpful.
August 6, 2009, 8:36 PM
Imperceptus
Excellent, will do.
August 7, 2009, 3:08 PM

Search