locked
DropDownList retain selected value when navigating between pages RRS feed

  • Question

  • User-1797368610 posted

    I have DropDownLists on my page to select "Year" and when i am selecting the drop down value its displaying information into gridview.  I am binding data in Page_Load event when (Not Page.IsPostBack). Users will select Year from the DropDownLists and then will click on view or Edit link from the gridview go to a view/edit page. If they then select to 'Go Back' to the initial page from View/Edit page, the DropDownLists year always reset to the top of their alphabetical list or top value.

    Is there any way this can be amended to remember the last values the user entered?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
            If Not IsPostBack() Then
                fillddlyear()
                filldatagriddetail("0", "0")            
            End If       
    
        End Sub
    
    
     Private Sub fillddlyear()
            Dim query As String
            Dim lDataSet As System.Data.DataSet
            Try
                query = " select distinct to_char(maint_end_date,'yyyy') year " & _
               " from maintenance_schedule  " & _
               " order by year "
                lDataSet = PassDataSet("DBTS", query)
                DdL_maintyear.DataSource = lDataSet
                DdL_maintyear.DataTextField = "year"
                DdL_maintyear.DataValueField = "year"
                DdL_maintyear.DataBind()
            Catch ex As Exception
                'Lblexception.Text = ex.Message
            End Try
        End Sub
        Private Sub filldatagriddetail(ByVal memberkey As String, ByVal transcnt As Integer)
            Dim query, projno, bld_id As String
            Dim lDataSet As System.Data.DataSet
            Dim mytable As System.Data.DataTable
            Dim totamt As Decimal = 0
            Dim fnd As Boolean = False
            Dim searchchar As String
            searchchar = ":"   ' Search for ":".
            Try
                
                query = "select rownum num,item_description, decode(vendor_id,null,' ',vendor_id) vendor_id, " & _
                " decode(status,null,' ',status) status, estimated_amount, renewal_type, " & _
                " to_char(maint_end_date,'dd-MON-yyyy') mntdate , to_char(status_date,'dd-MON-yyyy') status_date  " & _
                " from maintenance_schedule " & _
                " where to_char(maint_end_date,'YYYY') = '" & DdL_maintyear.SelectedValue & "'" & _
                " order by item_description "
    
                lDataSet = PassDataSet("DBTS", query)
                Session("projds") = lDataSet
                mytable = lDataSet.Tables("ResultTable")
                Dim dv As DataView = mytable.DefaultView
    
                'if the user has elected to sort the gridview
                If Not String.IsNullOrEmpty(Me.SortBy("GVdetail")) Then
                    'get the sort expression and apply it to our dataView
                    Dim sortExpr As String = Me.SortBy("GVdetail") & " " & IIf(Me.SortDirection("GVdetail") = WebControls.SortDirection.Ascending, "ASC", "DESC").ToString()
                    dv.Sort = sortExpr
                End If
    
                Me.GVdetail.DataSource = dv
                Me.GVdetail.DataBind()
                'Lblexception.Text = " "
    
            Catch ex As Exception
                If Trim(Mid(ex.Message, 1, 9)) = "ORA-01840" Then
                    'Lblexception.Text = "** Please enter a valid date. "
                Else
    
    
                End If
            End Try
    
    
        End Sub

    Friday, October 19, 2018 2:47 PM

Answers

  • User61956409 posted

    Hi Rakib1,

    You can try to store the SelectedIndex of DropDownList "DdL_maintyear" in a session variable, and dynamically set the selected index for the DropDownList based on the value of that session variable.

    Protected Sub DdL_maintyear_SelectedIndexChanged(sender As Object, e As EventArgs)
        Session("selected_item") = DdL_maintyear.SelectedIndex
    
        'your code logic here
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            fillddlyear()
            filldatagriddetail()
    
            If Session("selected_item") IsNot Nothing Then
                DdL_maintyear.SelectedIndex = CType(Session("selected_item"), Int32)
            End If
        End If
    End Sub

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2018 5:55 AM