Click here to Skip to main content
15,891,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Any help with the following would be much appreciated...

I have three DropDownLists on my page and I am binding data in Page_Load event when (Not Page.IsPostBack).

Users will select values from the DropDownLists and navigate to a different page. If they then select to 'Go Back' to the initial page, the DropDownLists always reset to the top of their alphabetical list.

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

I hope this is enough information.

Thanks in advance.

.aspx

ASP.NET
<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

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
Posted

1 solution

XML
<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>
 
Share this answer
 
Comments
Member 11510203 9-Mar-15 10:46am    
Apologies but is this a solution? If so what exactly have you changed?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900