0

I have two asp dropdown lists,the second one will generate options when the first dropdown list has a selected value. Both dropdown list are getting data from database. However, when I try to insert these two values into my table,the first dropdown list item will insert correctly,the second dropdown list always inserts the first item into my table.

After some testing, I found out that the second dropdown list is not selected and the first item was inserted into my table because its the default value.

BTW, my first dropdown list is autopostback while the second one is not.

I am using Microsoft Web Developer 2010 express and I am using Visual Basic.

first dropdown list:

<asp:DropDownList ID="stockcodeddl" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="stockcode" 
                    DataValueField="stockcode" Width="174px" Height="19px">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:oakwell %>" 
                    SelectCommand="SELECT [stockcode] FROM [orders] GROUP BY [stockcode]">
                </asp:SqlDataSource>

second dropdown list:

<asp:DropDownList ID="componentddl" runat="server" DataSourceID="ComponentsByStockCode" DataTextField="component" 
                    DataValueField="stockcode" Width="174px" Height="19px" 
                    Enable="true" onchange="getComponent()" >
                </asp:DropDownList>
                <asp:TextBox ID="comtxt" runat="server"></asp:TextBox>
                <asp:SqlDataSource ID="ComponentsByStockCode" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:oakwell %>" 
                    SelectCommand="SELECT component, stockcode FROM orders WHERE ([stockcode] = @stockcode) GROUP BY stockcode, component">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="stockcodeddl" Name="stockcode" PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>
wusen
  • 25
  • 6

2 Answers2

1

Please check that your dropdown binding data from the database was within the IsPostback on Page init. If not then before button event fires it will rebind the dropdown on page init function.

Selva
  • 527
  • 4
  • 14
  • hello, could you elaborate more on Page init ? i dont really understand it , or do you have external links for examples ? thanks :) – wusen Sep 25 '13 at 11:40
  • Please take a look on the below links that how the IsPostBack property works on page_init or page_load fn. http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 http://stackoverflow.com/questions/3620883/implementation-of-ispostback-in-page-load – Selva Sep 26 '13 at 05:46
  • ok thanks bro in my case i dont think i need to use ispostback function as the first dropdownlist will be changed more than 1 time – wusen Sep 26 '13 at 06:25
0

Try

dropdown2.selectedvalue 

or

dropdown2.selecteditem
Sasidharan
  • 3,676
  • 3
  • 19
  • 37
  • eh the reason for my first dropdownlist to be postback , because i want the second dropdownlist generate datas differently base the selection of the first dropdownlist. – wusen Sep 26 '13 at 01:38
  • actually i have tried the second ddl to be postback, but that is a problem that ,after select a option and the page load is refreshed , and my selected item gone ,the default item will be selected again – wusen Sep 26 '13 at 01:39
  • i have solve the problem using dropdown2.selectedvalue, and change the DataValueField="component" – wusen Sep 26 '13 at 03:30