?
Solved

Get value from cascading dropdown list on postback

Posted on 2009-04-23
3
Medium Priority
?
4,157 Views
Last Modified: 2013-11-08
I am trying to obtain the value of a drop down list populated client side by a cascading drop down and cannot seem to get the selected vaue

I have seen examples where the cascading drop down is added to page_init to let the page know you want to maintain state but this is doing nothing.

I must say that the whole code is inside a user control if it makes any difference

All I want to do is get the selected value from the drop down??

I am using vb.net, vs 2008, .Net 3.5 SP1. I am using the ajax toolkit from asp.net for the controls and system.web.extensions 3.5
'The button that launces the code, this is in the code behind
Public Sub saveProfilePeriod_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles saveProfilePeriodBtn.Click
        Dim CP As New CarProfile
        CP.ContractPeriodID = makeListExtender.SelectedValue
 
        CP.Deposit = carDeposit.Text
        CP.Ongoing = carOngoing.Text
 
        DB.CarProfiles.InsertOnSubmit(CP)
        DB.SubmitChanges()
        newContractPeriodContainer.Update()
 
    End Sub
 
This is the update panel with the cascading drop down in where I can not get the posted value
<asp:UpdatePanel ID="newContractPeriodContainer" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
                    <ContentTemplate>
                    <table class="formTable">
                        <tr>
                            <th>Contract Period: </th>
                            <td><div id="contractDropContainer">
                                    <asp:DropDownList ID="ContractPeriodsDrop" runat="server" />
                                    <cc1:CascadingDropDown ID="contractPeriodExtender" runat="server" />
                            </td>
                            <td><a href="javascript: hideContainer('newContractPeriod')">Add new contract period</a></td>
                        </tr>
                        <tr id="newContractPeriod" style="display:none">
                            <td></td>
                            <td>
                                <asp:TextBox ID="contractPerdiodText" runat="server" class="formSmallTextBox" /> months&nbsp;
                                <asp:button CssClass="btnSave" ID="saveContractPeriod" runat="server" OnClick="saveContractPeriod_Click" Text=" " />
                            </td>
                            <td></td>
                        </tr>
                        </table>
                    
                <table class="formTable">
                    <tr>
                        <th>Profile: </th>
                        <td><div id="profileDropContainer">
                                <asp:DropDownList ID="profilesList" runat="server">
                                
                                </asp:DropDownList>
                                <cc1:CascadingDropDown ID="profileListExtender" ServicePath="~/AdminEventHandlers.asmx" 
                                    ServiceMethod="GetCarProfiles" ParentControlID="ContractPeriodsDrop" TargetControlID="profilesList" PromptText="Select Profile" 
                                    runat="server" Category="Profiles"></cc1:CascadingDropDown>
                            </div>
                        </td>
                        <td><a href="javascript: hideContainer('addProfileContainer')">Add new profile</a></td>
                    </tr>
                    <tr id="addProfileContainer" style="display:none">
                        <td></td>
                        <td>
                            <asp:TextBox ID="carDeposit" runat="server" class="formSmallTextBox" />+
                            <asp:TextBox ID="carOngoing" runat="server" class="formSmallTextBox" />
                            <asp:button CssClass="btnSave" ID="saveProfilePeriodBtn" runat="server" OnClick="saveProfilePeriod_Click" Text=" " />
                        </td>
                        <td></td>
                    </tr>
                </table>
                </ContentTemplate>   
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="saveContractPeriod" />
                        <asp:AsyncPostBackTrigger ControlID="saveProfilePeriodBtn" />
                    </Triggers>     
                </asp:UpdatePanel>
 
'This is the page init where the cascading drop down is loaded, this works from a webservice
 Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
 
            contractPeriodExtender.ServicePath = "~/AdminEventHandlers.asmx"
            contractPeriodExtender.TargetControlID = "ContractPeriodsDrop"
            contractPeriodExtender.PromptText = "Select Contract Period"
            contractPeriodExtender.Category = "ContractPeriods"
            contractPeriodExtender.ServiceMethod = "GetContractPeriods"
 
 
    End Sub

Open in new window

0
Comment
Question by:cg_medistox
3 Comments
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 2000 total points
ID: 24214595
Hi,

If you are populating any values of Dropdown list in client side, DropDownList1.SelectedIndex or DropDownList1.SelectedValue won't work. Because those are obtained from ViewState. The values added in clientside won't be there in the ViewState.

Instead you can try this way.

Instead of DropDownList1.SelectedValue you can the selected value by Request[DropDownList1.UniqueID].
0
 
LVL 10

Expert Comment

by:Espavo
ID: 24214647
I get the Values from my Cascading DD's like this:
Try : If mStateDropDownList.SelectedValue > 0 Then : iState = mStateDropDownList.SelectedValue : Session("StateInt") = iState : End If : Catch : End Try
BTW, make sure that if you are setting their values on page_load that you do it like this:
If Not IsPostBack Then
  If Not Session("StateInt") Is Nothing Then  mStateDropDownList_CascadingDropDown.SelectedValue = Session("StateInt")
End If
0
 
LVL 4

Author Comment

by:cg_medistox
ID: 24214655
I have been looking for that solution for near two days now, my hat goes off to you!

I cannot find documentation for that anywhere, if there is then its not very well documented
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 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