Showing posts with label dropdownlist1selectedvalue. Show all posts
Showing posts with label dropdownlist1selectedvalue. 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

DropDownList1.SelectedValue allways the same on postbacks

Hi,

I'm new to Atlas/Ajax, so please forgive if this is a simple question.

I'm trying to create a simple solution, where I databinds a DropDownList and when the SelectedValue changes, a GridView must reflect the new data.

My problem is, when making the Ajax callback, the DropDownList.SelectedValue is always the same no matter what item is selected ind the DropDownList. Somehow the server does not know the new value.

Here's my code...

<formid="form1"runat="server">
<atlas:ScriptManagerID="ScriptManager1"EnablePartialRendering="true"runat="server"/>
<asp:DropDownListID="DropDownList1"runat="server"AutoPostBack="True"/>
<div>
<atlas:UpdatePanelID="aup"runat="server">
<ContentTemplate>
<asp:GridViewID="GridView1"runat="server"/>
</ContentTemplate>
<Triggers>
<atlas:ControlValueTriggerControlID="DropDownList1"PropertyName="SelectedValue"/>
</Triggers>
</atlas:UpdatePanel>
</div>
</form>

Here's my code behind...

Imports

System.Data

Partial

Class _Default
Inherits System.Web.UI.PageProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load
Call GetDropDownItems()
EndSubProtectedSub DropDownList1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles DropDownList1.SelectedIndexChanged
Call GetData()
EndSubPrivateSub GetDropDownItems()
Dim dtAs DataTable = .. get my DropDownList items as datatable
Me.DropDownList1.DataSource = dt
Me.DropDownList1.DataTextField ="text"
Me.DropDownList1.DataValueField ="ID"
Me.DropDownList1.DataBind()
EndSubPrivateSub GetData()
Dim dtAs DataTable = ... get data depending on theDropDownList1.SelectedValue <-- this is always the same!!
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
EndSub

End

Class

Hope you can help me out here.

Thanks!

M O J O

Sorry ... I found out the problem.

I just had to do this instead...

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load
If Not Page.IsPostBack Then
Call GetDropDownItems()
End If
EndSub

Thanks anyway!!!

M O J O


Hi: Mojo

I had same problem, thank you for the hint.

Do you think why it happened, why Postback is important. Although it works, but I need to know why?

Will be great that you can give my some idea.

Thanks.

James


The reason is,

Whenever a DropDownList is databound, the SelectedValue is always cleared out. To prevent the users selection from getting cleared out, you would only databind the DDL on the first hit to the page (when it's not a PostBack). Then on subsequent PostBacks, you would not databind your DDL. The ListItems in the DDL will get restored to the control from ViewState and the users selection (as read from the forms Post data) will get set on the DDL control.


thanks, mbanavige, now i c

James