Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > keep value selected in dropdownlist

Reply
Thread Tools

keep value selected in dropdownlist

 
 
DC Gringo
Guest
Posts: n/a
 
      03-03-2004
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





 
Reply With Quote
 
 
 
 
Brian K. Williams
Guest
Posts: n/a
 
      03-03-2004
EnableViewState = true

Regards,
Brian K. Williams

"DC Gringo" <> wrote in message
news:...
> 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
>
>
>
>
>



 
Reply With Quote
 
 
 
 
DC Gringo
Guest
Posts: n/a
 
      03-03-2004
I added EnableViewState="true" and nothing changed...

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

_____
DC G

"Brian K. Williams" <> wrote in message
news:uX$...
> EnableViewState = true
>
> Regards,
> Brian K. Williams
>
> "DC Gringo" <> wrote in message
> news:...
> > 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
> >
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Brian K. Williams
Guest
Posts: n/a
 
      03-03-2004
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

"DC Gringo" <> wrote in message
news:...
> I added EnableViewState="true" and nothing changed...
>
> <asp:dropdownlist Font-Size="8" id="ddlCommunities" EnableViewState="true"
> runat="server"
> Width="100"></asp:dropdownlist>
>
> _____
> DC G
>
> "Brian K. Williams" <> wrote in message
> news:uX$...
> > EnableViewState = true
> >
> > Regards,
> > Brian K. Williams
> >
> > "DC Gringo" <> wrote in message
> > news:...
> > > 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
> > >
> > >
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
DC Gringo
Guest
Posts: n/a
 
      03-03-2004
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

"Brian K. Williams" <> wrote in message
news:%...
> 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
>
> "DC Gringo" <> wrote in message
> news:...
> > I added EnableViewState="true" and nothing changed...
> >
> > <asp:dropdownlist Font-Size="8" id="ddlCommunities"

EnableViewState="true"
> > runat="server"
> > Width="100"></asp:dropdownlist>
> >
> > _____
> > DC G
> >
> > "Brian K. Williams" <> wrote in message
> > news:uX$...
> > > EnableViewState = true
> > >
> > > Regards,
> > > Brian K. Williams
> > >
> > > "DC Gringo" <> wrote in message
> > > news:...
> > > > 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
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Brian K. Williams
Guest
Posts: n/a
 
      03-03-2004
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

"DC Gringo" <> wrote in message
news:...
> 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
>
> "Brian K. Williams" <> wrote in message
> news:%...
> > 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
> >
> > "DC Gringo" <> wrote in message
> > news:...
> > > I added EnableViewState="true" and nothing changed...
> > >
> > > <asp:dropdownlist Font-Size="8" id="ddlCommunities"

> EnableViewState="true"
> > > runat="server"
> > > Width="100"></asp:dropdownlist>
> > >
> > > _____
> > > DC G
> > >
> > > "Brian K. Williams" <> wrote in message
> > > news:uX$...
> > > > EnableViewState = true
> > > >
> > > > Regards,
> > > > Brian K. Williams
> > > >
> > > > "DC Gringo" <> wrote in message
> > > > news:...
> > > > > 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
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net Web Controls 0 10-05-2008 10:29 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net 0 10-05-2008 10:29 PM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
Cannot keep selected value in control? Francois Soucy ASP .Net Building Controls 1 12-30-2004 03:54 AM
Drop Down refresh keep selected value Patrick Javascript 2 09-03-2004 11:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57