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: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"
If Not (secHelper.HasRole(SecurityMatchOption.AtLeast, SecurityRole.Manager)) Then
Server.Transfer("~/dialogs/accessdenied.aspx")
End If
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()
btnBack.PostBackUrl = "~/Default.aspx"
End If
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/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()
Dim teamId = CType(drpTeam.SelectedValue, Integer)
ChecklistAdminRepeaterReleased.Bind(teamId, Constants.ChecklistStatus.RELEASED, checklists.Where(Function(c) c.status = Constants.CHECKLIST_STATUS_RELEASED).ToList())
ChecklistAdminRepeaterCreated.Bind(teamId, Constants.ChecklistStatus.CREATED, checklists.Where(Function(c) c.status = Constants.CHECKLIST_STATUS_CREATED).ToList())
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