如何在页面之间导航时使DropDownList保留选定的值。 [英] How can I make DropDownList retain selected value when navigating between pages.

查看:66
本文介绍了如何在页面之间导航时使DropDownList保留选定的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



用户将从DropDownLists中选择值并导航到其他页面。如果他们然后选择返回到初始页面,则DropDownLists总是重置为其按字母顺序排列的列表顶部。



是否有 这可以修改以记住用户输入的最后一个值吗?



我希望这是足够的信息。



提前致谢。



.aspx



 <   asp:Content     ID   =  Content2    ContentPlaceHolderID   =  MainContent    runat   =  server >  
< h2 < span class =code-keyword>>
修改清单< / h2 >
< br / > < span class =code-keyword>< br / >
< table >
< tr >
&l t; td >
团队
< / td >
< td >
< span class =code-keyword>< asp:DropDownList ID = drpTeam runat = server AutoPostBack = < span class =code-keyword> true & gt;
< / asp:DropDownList >
< / td >
< td >
类别
< / td >
< td >
< asp:DropDownList ID = drpCa tegory runat = server AutoPostBack = true >
< / asp:DropDownList >
< / td >
< td >
管理公司
< / td >
< td >
< span class =code-keyword>< asp:DropDownList ID = drpManCo runat = server >
< / asp:DropDownList >
< / td >
< / tr >
< / table >







.aspx.vb



 受保护的 重载  Sub  Page_Load( ByVal  sender  As   Object  ByVal  e  As  System.EventArgs)句柄 。加载
尝试
< span class =code-keyword> Me .Header.Title = OCP - Checklist Admin

' security
如果 (secHelper.HasRole(SecurityMatchOption.AtLeast,SecurityRole.Manager))那么
Server.Transfer( 〜/ dialogs / accessdenied.aspx
结束 如果

' 从btnAdd_Click Response.Redirect传递的查询字符串显示成功消息 - WT214900 - 16.01.15 - GJust
If Request.QueryString( alert)= Checklistadded 然后
lblSuccess.Text = 核对表已成功创建
结束 如果

如果 Page.IsPostBack)然后
secHelper.BindTeamDropDown(drpTeam,SecurityMatchOption) .AtLeast,SecurityRole.Manager)
GetAllCategoriesForAllTeams()
PopulateCategoryDropDown(drpCategory, CType (drpTeam.SelectedValue,整数))
PopulateManCoDropdown(drpManCo)
BindChecklistRepeater()

' 设置任何默认按钮处理程序
btnBack.PostBackUrl = 〜/ Default.aspx
结束 如果
' BindChecklistRepeater()
Catch ex As Exception
ExceptionHelper.HandleException(ex)
End 尝试

结束 Sub

私有 Sub GetAllCategoriesForAllTeams()
使用 db 作为 LINQDataContext(Model.helpers.ConnectionHelper.ConnectionString())
对于 每个团队 secHelper.GetTeamsForUser( )
Dim currentTeam As Team = team
Dim 类别作为列表( 类别) = db.Categories.Where( CType Function (x) CType (x.team_id = currentTeam.id,布尔),Func( 类别, Boolean )))。ToList()
_allCategories.AddRange(category)
Session(Constants.SESSION_ALLCATEGORIES)= _ allCategories
< span class =code-keyword>下一步
结束 使用
结束 Sub

私人 Sub PopulateCategoryDropDown( ByRef drp As System.Web.UI.WebControls.DropDownList,可选 ByVal teamId < span class =code-keyword> As 整数? = Nothing
如果 teamId Nothing 然后
drp.DataSource = CType (会话(Constants.SESSION_ALLCATEGORIES),列表( 类别))。Distinct()。OrderBy(函数(c)c.name)。ToList()
Else
drp.DataSource = CType (会话(Constants.SESSION_ALLCATEGORIES),列表( 类别))。Distinct()。Where(功能(c) CType (c.team_id = teamId,布尔)) .OrderBy( Function (c)c.name)。ToList()
End 如果
drp.DataTextField = name
drp.DataValueField = < span class =code-string> id
drp.DataBind()
结束 Sub

私有 Sub PopulateManCoDropdown( ByRef drp As DropDownList)
使用 db 作为 LINQDataContext(模型。 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 ListItem( < No Management Company> String .Empty))
End 使用
结束 Sub

私有 Sub drpTeam_SelectedIndexChanged( ByVal 发​​件人作为 对象 ByVal e As System.EventArgs)句柄 drpTeam.SelectedIndexChanged
PopulateCategoryDropDown(drpCategory, CType (drpTeam.SelectedValue,Integer?))
BindChecklistRepeater()
GetAllCategoriesForAllTeams()
结束 Sub

私有 Sub drpCategory_SelectedIndexChanged( ByVal sender As Object ByVal e As System.EventArgs) Handles drpCategory.SelectedIndexChanged
BindChecklistRepeater()
结束 Sub

受保护的 Sub drpTeamRepeater_SelectedIndexChanged( ByVal sender 作为 对象 ByVal e As EventArgs)
Dim drpTm As DropDownList = CType (sender,DropDownList )
PopulateCategoryDropDown( CType (drpTm.NamingContainer.FindControl( drpCategory),DropDownList), CType (drpTm.SelectedValue,Integer?))
结束 Sub

受保护 Sub btnAdd_Click( ByVal sender As 对象 ByVal e As EventArgs)句柄 btnAdd.Click
使用 db 作为 LINQDataContext(Model.helpers.ConnectionHelper.ConnectionString())
Dim newCheckList 作为 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 )然后
newCheckList.manco_id = 没什么
Else
newCheckList.manco_id = CInt (drpManCo.SelectedItem.Value)
结束 如果
db.Checklists.InsertOnSubmit(newCheckList)
db.SubmitChanges()
BindChecklistRepeater()
' Response.Redirect(〜/ admin / Tasks.aspx?chkid =+ newCheckList.id。 ToString())

' 使用查询字符串将用户重定向到同一页面。如果/当用户刷新浏览器时,这可以防止表单重新提交[F5] - WT214889 - 14.01.15 - GJust
Response.Redirect( < span class =code-string>〜/ admin / ChecklistAdmin.aspx?alert = Checklistadded)
结束 使用
结束 Sub

私有 Sub BindChecklistRepeater()
如果 drpTeam.SelectedItem 什么 AndAlso drpCategory.SelectedItem Nothing 然后
使用 db 作为 LINQDataContext(Model.helpers.ConnectionHelper.ConnectionString())
Dim 检查表作为列表( Model.Checklist)= _
db.Checklists.Where( Function (x)x.team_id = CInt (drpTeam.SelectedItem.Value) AndAlso x.category_id = CInt (drpCategory.SelectedItem 。值))。 _
OrderBy( Function (c)c.name)。ToList()' 修复问题69 - 添加了OrderBy

Dim teamId = CType (drpTeam.SelectedValue,整数

' 已发布的核对表
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_C REATED)。ToList())

' 归档核对清单
ChecklistAdminRepeaterArchived.Bind(teamId,Constants.ChecklistStatus.ARCHIVED,checklists.Where( Function (c)c.status = Constants.CHECKLIST_STATUS_ARCHIVED)。ToList())

lblMessage.Text = 字符串 .Empty
pnlEdit.Visible = True

如果(checklists.Count< = 0 然后
lblMessage.Text = 未找到清单
pnlEdit.Visible = False
结束 如果
结束 使用
结束 如果
结束 Sub

解决方案

 <   asp:Content     ID   =  Content2    ContentPlaceHolderID   =  MainContent    runat   = 服务器 >  
< h2 >
修改清单< / h2 >
< br < span class =code-attribute> / > < br / >
< 表格 >
< tr >
< td >
团队
< / td >
< td >
< asp:DropDownList < span class =code-attribute> ID = drpTeam runat = server AutoPostBack = true >
< / asp:DropDownList >
< / td >
< td >
类别
< / td >
< td >
< asp:DropDownList ID = drpCategory runat = server AutoPostBack = true >
< / asp:DropDownList >
< span class =code-keyword>< / td >
< td >
管理公司
< / td >
< td >
< asp:DropDownList ID = drpManCo runat = server >
< / asp:DropDownList >
< / td >
< / tr >
< / table >


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"

        '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

解决方案

<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>


这篇关于如何在页面之间导航时使DropDownList保留选定的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆