Saturday, March 24, 2012

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

No comments:

Post a Comment