keep value selected in dropdownlist

Discussion in 'ASP .Net' started by DC Gringo, Mar 3, 2004.

  1. DC Gringo

    DC Gringo Guest

    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
     
    DC Gringo, Mar 3, 2004
    #1
    1. Advertising

  2. 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
    >
    >
    >
    >
    >
     
    Brian K. Williams, Mar 3, 2004
    #2
    1. Advertising

  3. DC Gringo

    DC Gringo Guest

    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
    > >
    > >
    > >
    > >
    > >

    >
    >
     
    DC Gringo, Mar 3, 2004
    #3
  4. 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
    > > >
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Brian K. Williams, Mar 3, 2004
    #4
  5. DC Gringo

    DC Gringo Guest

    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
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    DC Gringo, Mar 3, 2004
    #5
  6. 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
    > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Brian K. Williams, Mar 3, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iain
    Replies:
    3
    Views:
    968
  2. Nathan Sokalski
    Replies:
    0
    Views:
    650
    Nathan Sokalski
    Oct 5, 2008
  3. Francois Soucy

    Cannot keep selected value in control?

    Francois Soucy, Dec 30, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    113
    John Saunders
    Dec 30, 2004
  4. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    317
    Nathan Sokalski
    Oct 5, 2008
  5. Patrick

    Drop Down refresh keep selected value

    Patrick, Sep 3, 2004, in forum: Javascript
    Replies:
    2
    Views:
    166
    Shawn Milo
    Sep 3, 2004
Loading...

Share This Page