Showing posts with label ddl. Show all posts
Showing posts with label ddl. Show all posts

Saturday, March 24, 2012

DropDownList2.DataValueField = DropDownList1.SelectedValue

Hello, I have a webpage with 2 DDL controls. DDL1.SelectedValue = DDL2.DataValueField. Everthing works fine if I have DDL1 AutoPostBack checked. I would like to eliminate the postbacks. Below is my code. I do not know why it doesn't work.

Thanks! Steve

ProtectedSub DropDownList1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles DropDownList1.SelectedIndexChanged

Dim strDataTextFieldAsString

DropDownList2.DataValueField = DropDownList1.SelectedValue

strDataTextField = DropDownList1.SelectedValue

strDataTextField = strDataTextField &"text"

DropDownList2.DataTextField = strDataTextField

EndSub

<body>

<formid="form1"runat="server">

<div>

<asp:ScriptManagerID="ScriptManager1"runat="server">

</asp:ScriptManager>

<asp:UpdatePanelID="UpdatePanel1"runat="server">

<ContentTemplate>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"SelectCommand="SELECT * FROM [Futures]"></asp:SqlDataSource>

<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"SelectCommand="SELECT * FROM [Months]"></asp:SqlDataSource>

<asp:DropDownListID="DropDownList1"runat="server"Style="z-index: 100; left: 342px; position: absolute; top: 86px"DataSourceID="SqlDataSource1"DataTextField="Text"DataValueField="Value"></asp:DropDownList>

<asp:DropDownListID="DropDownList2"runat="server"Style="z-index: 102; left: 616px; position: absolute; top: 86px"DataSourceID="SqlDataSource2"></asp:DropDownList>

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTriggerControlID="DropDownList1"EventName="SelectedIndexChanged"/>

</Triggers>

</asp:UpdatePanel>

</div>

</form>

</body>

SelectedIndexChanged is a server-side event which occurs only after a postback. If you inserted a breakpoint, you'd find that the program never gets there.

DisturbedBuddha:

SelectedIndexChanged is a server-side event which occurs only after a postback. If you inserted a breakpoint, you'd find that the program never gets there.

So what do I do to fix that?

Thanks Steve


Set AutoPostBack="true" for the dropdownlist and just place the dropdownlist in its own updatepanel to hide the visible refresh.


Hello, I already have my dropdownlist1 in an updatepanel with autopostback="true" and the whole page refreshes. If autopostback="false" nothing happens.

Please look at my code on top again.

Thanks for the help

Steve

DropDownList SelectedIndexChanged not firing

I have an issue with a DDL where my SelectedIndexChanged only gets fired when the second list item is selected (there are only 2 items in the list). The first item in the list is the default item and is set to selected. Also the list items are not bound via a datasource, they are already populated.

The DDL is wired up to the UpdatePanel as a Trigger. The DDL is also located outside the UpdatePanel - I tried having the DDL inside the UpdatePanel as well but the same issue comes up.

The Update does occur because I verified that the Page_Load fires when I switch between the two DDL items.

Any ideas?

The trick to this is to insert another record as the default selection of the DDL. I always insert a ListItem at run-time right after I bind the records to the DDL that tells the user to make a selection, for example 'Select State'. This solves two problems, it helps guide the user (let's face they need all the help they can get) and will force the selectedindexchanged event to fire like you want.

ddlState.Items.Insert(0,New ListItem("Select a State", 0))

Here is a Blog entry I made on the topic:

http://professionalaspnet.com/archive/2007/12/22/Cures-for-the-Common-Ailments-_2D00_-DropDownList.aspx


I was hoping that wasn't the fix but I guess I'll just have to work with it.

Thanks!


Have you set the property named "ChildrenAsTriggers" of the UpdatePanel to true?

dropdownlist in updatepanel1 to update updatepanel2

I have 2 ddl in an updatepanel1 and when one is selected, panel refreshes to load ddl #2 based on ddl1 choice. Works great, love it. Now can ddl2 change cause updatepanel2 to refresh?doh! nevermind... set update to conditional and call updatepanel.update from codebehind.