在页面之间导航时如何使 DropDownList 保留选定的值

How can I make DropDownList retain selected value when navigating between pages

如能提供以下任何帮助,我们将不胜感激...

我的页面上有三个 DropDownList,我在 Page_Load 事件中绑定数据(不是 Page.IsPostBack)。

用户将从 DropDownLists select 值导航到不同的页面。如果他们然后 select 到 'Go Back' 到初始页面,DropDownLists 总是重置到他们的字母列表的顶部。

有什么方法可以修改它以记住用户最后输入的值吗?

我希望这些信息足够了。

提前致谢。

.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Amend Checklist</h2>
<br /><br />
    <table>
        <tr>
            <td>
                Team
            </td>
            <td>
                <asp:DropDownList ID="drpTeam" runat="server" AutoPostBack="true">
                </asp:DropDownList>
            </td>
            <td>
                Category
            </td>
            <td>
                <asp:DropDownList ID="drpCategory" runat="server" AutoPostBack="true">
                </asp:DropDownList>
            </td>
            <td>
                Management Company
            </td>
            <td>
                <asp:DropDownList ID="drpManCo" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
    </table>

.aspx.vb

Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Try
        Me.Header.Title = "OCP - Checklist Admin"

        'security
        If Not (secHelper.HasRole(SecurityMatchOption.AtLeast, SecurityRole.Manager)) Then
          Server.Transfer("~/dialogs/accessdenied.aspx")
        End If

        ' querystring passed through from the btnAdd_Click Response.Redirect to display success message - WT214900 - 16.01.15 - GJust
        If Request.QueryString("alert") = "Checklistadded" Then
          lblSuccess.Text = "Checklist Created Successfully"
        End If

        If (Not Page.IsPostBack) Then
          secHelper.BindTeamDropDown(drpTeam, SecurityMatchOption.AtLeast, SecurityRole.Manager)
          GetAllCategoriesForAllTeams()
          PopulateCategoryDropDown(drpCategory, CType(drpTeam.SelectedValue, Integer))
          PopulateManCoDropdown(drpManCo)
          BindChecklistRepeater()

          ' set up any default button handlers
          btnBack.PostBackUrl = "~/Default.aspx"
        End If
        'BindChecklistRepeater()
      Catch ex As Exception
        ExceptionHelper.HandleException(ex)
      End Try

    End Sub

    Private Sub GetAllCategoriesForAllTeams()
      Using db As New LINQDataContext(Model.helpers.ConnectionHelper.ConnectionString())
        For Each team In secHelper.GetTeamsForUser()
          Dim currentTeam As Team = team
          Dim category As List(Of Category) = db.Categories.Where(CType(Function(x) CType(x.team_id = currentTeam.id, Boolean), Func(Of Category, Boolean))).ToList()
          _allCategories.AddRange(category)
          Session(Constants.SESSION_ALLCATEGORIES) = _allCategories
        Next
      End Using
    End Sub

    Private Sub PopulateCategoryDropDown(ByRef drp As System.Web.UI.WebControls.DropDownList, Optional ByVal teamId As Integer? = Nothing)
      If teamId Is Nothing Then
        drp.DataSource = CType(Session(Constants.SESSION_ALLCATEGORIES), List(Of Category)).Distinct().OrderBy(Function(c) c.name).ToList()
      Else
        drp.DataSource = CType(Session(Constants.SESSION_ALLCATEGORIES), List(Of Category)).Distinct().Where(Function(c) CType(c.team_id = teamId, Boolean)).OrderBy(Function(c) c.name).ToList()
      End If
      drp.DataTextField = "name"
      drp.DataValueField = "id"
      drp.DataBind()
    End Sub

    Private Sub PopulateManCoDropdown(ByRef drp As DropDownList)
      Using db As New LINQDataContext(Model.helpers.ConnectionHelper.ConnectionString())
        drp.DataSource = db.ManCos().OrderBy(Function(mc) mc.name).ToList()
        drp.DataTextField = "name"
        drp.DataValueField = "id"
        drp.DataBind()
        drp.Items.Insert(0, New ListItem("<No Management Company>", String.Empty))
      End Using
    End Sub

    Private Sub drpTeam_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpTeam.SelectedIndexChanged
      PopulateCategoryDropDown(drpCategory, CType(drpTeam.SelectedValue, Integer?))
      BindChecklistRepeater()
      GetAllCategoriesForAllTeams()
    End Sub

    Private Sub drpCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpCategory.SelectedIndexChanged
      BindChecklistRepeater()
    End Sub

    Protected Sub drpTeamRepeater_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
      Dim drpTm As DropDownList = CType(sender, DropDownList)
      PopulateCategoryDropDown(CType(drpTm.NamingContainer.FindControl("drpCategory"), DropDownList), CType(drpTm.SelectedValue, Integer?))
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
      Using db As New LINQDataContext(Model.helpers.ConnectionHelper.ConnectionString())
        Dim newCheckList As New Model.Checklist
        newCheckList.name = txtChecklist.Text
        newCheckList.team_id = CInt(drpTeam.SelectedItem.Value)
        newCheckList.category_id = CInt(drpCategory.SelectedItem.Value)
        newCheckList.status = Constants.CHECKLIST_STATUS_CREATED
        If (drpManCo.SelectedItem.Value = String.Empty) Then
          newCheckList.manco_id = Nothing
        Else
          newCheckList.manco_id = CInt(drpManCo.SelectedItem.Value)
        End If
        db.Checklists.InsertOnSubmit(newCheckList)
        db.SubmitChanges()
        BindChecklistRepeater()
        'Response.Redirect("~/admin/Tasks.aspx?chkid=" + newCheckList.id.ToString())

        ' Redirect user to the same page with query string. This prevents form resubmission if/when the user refreshes browser [F5] - WT214889 - 14.01.15 - GJust
        Response.Redirect("~/admin/ChecklistAdmin.aspx?alert=Checklistadded")
      End Using
    End Sub

    Private Sub BindChecklistRepeater()
      If (Not drpTeam.SelectedItem Is Nothing AndAlso Not drpCategory.SelectedItem Is Nothing) Then
        Using db As New LINQDataContext(Model.helpers.ConnectionHelper.ConnectionString())
          Dim checklists As List(Of Model.Checklist) = _
              db.Checklists.Where(Function(x) x.team_id = CInt(drpTeam.SelectedItem.Value) AndAlso x.category_id = CInt(drpCategory.SelectedItem.Value)). _
              OrderBy(Function(c) c.name).ToList()    ' Fix issue 69 - added OrderBy

          Dim teamId = CType(drpTeam.SelectedValue, Integer)

          ' Released Checklists
          ChecklistAdminRepeaterReleased.Bind(teamId, Constants.ChecklistStatus.RELEASED, checklists.Where(Function(c) c.status = Constants.CHECKLIST_STATUS_RELEASED).ToList())

          ' Created Checklists
          ChecklistAdminRepeaterCreated.Bind(teamId, Constants.ChecklistStatus.CREATED, checklists.Where(Function(c) c.status = Constants.CHECKLIST_STATUS_CREATED).ToList())

          ' Archived Checklists
          ChecklistAdminRepeaterArchived.Bind(teamId, Constants.ChecklistStatus.ARCHIVED, checklists.Where(Function(c) c.status = Constants.CHECKLIST_STATUS_ARCHIVED).ToList())

          lblMessage.Text = String.Empty
          pnlEdit.Visible = True

          If (checklists.Count <= 0) Then
            lblMessage.Text = "No checklist found"
            pnlEdit.Visible = False
          End If
        End Using
      End If
    End Sub

在单个会话对象中维护所有三个值,这些值由一些字符分隔,例如会话['ddlvalues']="ddlval1#ddlval2#ddlval3" 当您返回初始页面时,检查会话 ["ddlvalues"] 使用您使用的特殊字符和 select 下拉列表中的值拆分字符串

我通过使用 Session 存储我的选择解决了这个问题,并在页面加载时重新评估。

   Session("Team") = drpTeam.SelectedValue
   Session("Category") = drpCategory.SelectedValue

页面加载

If (Not Page.IsPostBack) And Session("Team") IsNot Nothing Then
            drpTeam.SelectedValue = Session("Team").ToString()
            drpCategory.SelectedValue = Session("Category").ToString()
            secHelper.BindTeamDropDown(drpTeam, SecurityMatchOption.AtLeast, SecurityRole.Manager)
            GetAllCategoriesForAllTeams()
            PopulateCategoryDropDown(drpCategory, CType(drpTeam.SelectedValue, Integer))
            PopulateManCoDropdown(drpManCo)
            BindChecklistRepeater()
        End If