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

时间:2015-03-12 12:15:11

标签: asp.net vb.net drop-down-menu session-state

非常感谢以下任何帮助...

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

用户将从DropDownLists中选择值并导航到其他页面。如果他们然后选择“返回”到初始页面,则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

2 个答案:

答案 0 :(得分:1)

将单个会话对象中的所有三个值保存为某个字符,例如会话[&#39; ddlvalues&#39;] =&#34; ddlval1#ddlval2#ddlval3&#34; 当你回到初始页面检查会话[&#34; ddlvalues&#34;]拆分字符串时使用您使用的特殊字符并在下拉列表中选择值

答案 1 :(得分:1)

我通过使用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