keep value selected in dropdownlist

D

DC Gringo

I have a dropdownlist that, upon form submission, I'd like to maintain the
selected value when I get my result...how do I do that?

<asp:dropdownlist Font-Size="8" id="ddlCommunities" runat="server"
Width="100"></asp:dropdownlist>


Sub RunReport_OnClick(sender As Object, e As System.EventArgs)

_sqlStmt &= " AND tblSurvey1.clnGUID = '" &
ddlCommunities.SelectedItem.Value & "'"
BindData()

End Sub


---HERE'S MY PAGE_LOAD AND BindData()

Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Sub BindData()

Dim conString As String = "server=server;database=db;uid=user;pwd=pwd;"

Dim myDataSet5 As New DataSet
Dim myDataAdapter5 As New SqlDataAdapter(_sqlStmt5, conString)
myDataAdapter5.Fill(myDataSet5, "CommunitiesT2")
ddlCommunities.DataSource = myDataSet5.Tables("CommunitiesT2")
ddlCommunities.DataMember = "CommunitiesT2"
ddlCommunities.DataTextField = "clnName"
ddlCommunities.DataValueField = "clnGUID"

ddlCommunities.DataBind()
ddlCommunities.Items.Insert(0,New ListItem("--ALL","0"))

End Sub
 
D

DC Gringo

I added EnableViewState="true" and nothing changed...

<asp:dropdownlist Font-Size="8" id="ddlCommunities" EnableViewState="true"
runat="server"
Width="100"></asp:dropdownlist>

_____
DC G
 
B

Brian K. Williams

Make sure that you are populating your dropdown list within
!IsPostBack.

Like:
private void Page_Load(object sender, System.EventArgs e)

{

if(!IsPostBack)

{

code to populate Dropdown list...

}

}


Also, if you are not the only person developing on the server, check that
ViewState has not been disabled in the Machine.Config or Web.Config files.

Regards,
Brian K. Williams
 
D

DC Gringo

Brian,

I'm binding data in both cases, I believe...here's my code...

THANKS!
---------


Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Sub BindData()
Dim conString As String = "server=server;database=db;uid=user;pwd=pwd;"
Dim myDataSet1 As New DataSet
Dim myDataAdapter1 As New SqlDataAdapter(_sqlStmt, conString)
myDataAdapter1.Fill(myDataSet1, "CommunitiesT1")
DataGrid2.DataSource = myDataSet1.Tables("CommunitiesT1")

Dim myDataSet2 As New DataSet
Dim myDataAdapter2 As New SqlDataAdapter(_sqlStmt2, conString)
myDataAdapter2.Fill(myDataSet2, "ProvincesT")
Provinces.Datasource = myDataSet2.Tables("ProvincesT")
Provinces.DataMember = "ProvincesT"
Provinces.DataTextField = "clnName"
Provinces.DataValueField = "clnGUID"

Dim myDataSet3 As New DataSet
Dim myDataAdapter3 As New SqlDataAdapter(_sqlStmt3, conString)
myDataAdapter3.Fill(myDataSet3, "DistrictsT")
Districts.DataSource = myDataSet3.Tables("DistrictsT")
Districts.DataMember = "DistrictsT"
Districts.DataTextField = "clnName"
Districts.DataValueField = "clnGUID"

Dim myDataSet4 As New DataSet
Dim myDataAdapter4 As New SqlDataAdapter(_sqlStmt4, conString)
myDataAdapter4.Fill(myDataSet4, "SubDistrictsT")
SubDistricts.DataSource = myDataSet4.Tables("SubDistrictsT")
SubDistricts.DataMember = "SubDistrictsT"
SubDistricts.DataTextField = "clnName"
SubDistricts.DataValueField = "clnGUID"

Dim myDataSet5 As New DataSet
Dim myDataAdapter5 As New SqlDataAdapter(_sqlStmt5, conString)
myDataAdapter5.Fill(myDataSet5, "CommunitiesT2")
ddlCommunities.DataSource = myDataSet5.Tables("CommunitiesT2")
ddlCommunities.DataMember = "CommunitiesT2"
ddlCommunities.DataTextField = "clnName"
ddlCommunities.DataValueField = "clnGUID"

DataGrid2.DataBind()

Provinces.DataBind()
Provinces.Items.Insert(0,New ListItem("--ALL","0"))

Districts.DataBind()
Districts.Items.Insert(0,New ListItem("--ALL","0"))

SubDistricts.DataBind()
SubDistricts.Items.Insert(0,New ListItem("--ALL","0"))

ddlCommunities.DataBind()
ddlCommunities.Items.Insert(0,New ListItem("--ALL","0"))

End Sub

Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
_sqlStmt = _sqlStmt & " ORDER BY " & E.SortExpression
BindData()
End Sub

Sub PageIndexChanged_OnClick(Source As Object, E As
DataGridPageChangedEventArgs)
DataGrid2.CurrentPageIndex = E.NewPageIndex
BindData()
End Sub


Sub RunReport_OnClick(sender As Object, e As System.EventArgs)
_sqlStmt &= " AND tblSurvey1.clnGUID = '" &
ddlCommunities.SelectedItem.Value & "'"

End if

BindData()
End Sub
 
B

Brian K. Williams

If you are not updating values in your dropdowns on submit, move them into
another method that only gets called in Not Page.IsPostBack

If you are updating or adding values to the dropdown selections, then you
might want to try saving the selectedIndex and onLoad re-select the last
selected value.

-Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top