0

我正在开发一个ListView用于显示问题的应用程序,以及来自不同表格的选项DataBase
我已经指定PageSize="1"DataPager因此一次只能显示一个问题,在选择选项后,用户单击显示另一个问题的下一步按钮,但是如果用户单击上一个按钮,ListView则不显示上一个选择的选项?我不知道如何保留先前选择的选项,因为我必须DataBase最后提交它们。

4

2 回答 2

0

要保留此类场景中的值,请使用 Session 对象。您可以创建一组值,例如 - Question Id、Answer Id 等并将其存储在 Session 中(HashTable 是一个不错的选择)。当用户返回时,从 Session 中获取该问题 ID 的值。每当用户移动到下一个问题时刷新 Session 对象。但请记住处理以下情况 - 用户向后移动,然后再次下一个,问题 ID 已经在收集中。

于 2013-01-15T08:44:19.553 回答
0

您可以将控件的值保留HashtableSession. 下面是一个示例页面,TextBoxCheckBoxListView. 状态在分页事件之间被保留。

WebForm1.aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication13.WebForm1" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListView runat="server" ID="listView" EnableViewState="true" OnItemDataBound="listView_ItemDataBound" ClientIDMode="Inherit">

                <ItemTemplate>
                    <div class="Product">
                        <strong>
                            <asp:TextBox runat="server" ID="TextBoxId" OnTextChanged="TextBox_TextChanged"></asp:TextBox>
                            ::
                            <asp:TextBox runat="server" ID="TextBoxName" OnTextChanged="TextBox_TextChanged"></asp:TextBox>
                        </strong>
                        <br />
                        <asp:CheckBox runat="server" ID="CheckBoxChecked" AutoPostBack="true" OnCheckedChanged="CheckBoxChecked_CheckedChanged" />
                    </div>
                </ItemTemplate>
                <LayoutTemplate>
                    <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                </LayoutTemplate>
                <ItemSeparatorTemplate>
                    <hr />
                </ItemSeparatorTemplate>
            </asp:ListView>

            <asp:DataPager ID="DataPagerProducts" runat="server" PagedControlID="listView"
                PageSize="1" OnPreRender="DataPagerProducts_PreRender">
                <Fields>
                    <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
                    <asp:NumericPagerField />
                    <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
                </Fields>
            </asp:DataPager>
        </div>
    </form>
</body>
</html>

文件背后的代码 - WebForm1.aspx.cs

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication13
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        /// <summary>
        /// This object will preserve the values of the controls
        /// </summary>
        internal Hashtable DataObject
        {
            get
            {
                if (Session["DataObject"] == null)
                    Session["DataObject"] = new Hashtable();
                return (Hashtable)Session["DataObject"];
            }
            set
            {
                Session["DataObject"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            listView.DataSource = new[] { 
                    new{Id=1,Name="test 1",Checked=true},
                    new{Id=2,Name="test 2",Checked=true},
                    new{Id=3,Name="test 3",Checked=true}
                };
            listView.DataBind();
        }

        protected void DataPagerProducts_PreRender(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// Use the ItemDataBound event to set the values of the controls
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void listView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            SetValue("TextBoxId", "Id", e);
            SetValue("TextBoxName", "Name", e);
            SetValue("CheckBoxChecked", "Checked", e);
        }

        /// <summary>
        /// Sets the value of the control with the specified id by looking in the user session's DataObject hashtable
        /// </summary>
        /// <param name="id">The id of the searched control</param>
        /// <param name="dataProperty">The property name of the data item</param>
        /// <param name="e">The list view item event arguments</param>
        void SetValue(string id, string dataProperty, ListViewItemEventArgs e)
        {
            if (e.Item.DataItem == null)
                return;
            var webControl = e.Item.FindControl(id);
            switch (webControl.GetType().ToString())
            {
                //To do: handle other control types, such as System.Web.UI.WebControls.ComboBox etc.
                case "System.Web.UI.WebControls.TextBox":
                    var label = (TextBox)webControl;
                    if (DataObject[label.ClientID + e.Item.DataItemIndex.ToString()] == null)
                    {
                        DataObject[label.ClientID + e.Item.DataItemIndex.ToString()] = e.Item.DataItem.GetType().GetProperty(dataProperty).GetValue(e.Item.DataItem);
                    }
                    label.Text = String.Format("{0}", DataObject[label.ClientID + e.Item.DataItemIndex.ToString()]);
                    break;
                case "System.Web.UI.WebControls.CheckBox":
                    var checkBox = (CheckBox)webControl;
                    if (DataObject[checkBox.ClientID + e.Item.DataItemIndex.ToString()] == null)
                    {
                        DataObject[checkBox.ClientID + e.Item.DataItemIndex.ToString()] = e.Item.DataItem.GetType().GetProperty(dataProperty).GetValue(e.Item.DataItem);
                    }
                    checkBox.Checked = (bool)DataObject[checkBox.ClientID + e.Item.DataItemIndex.ToString()];
                    break;
                default:
                    break;
            }
        }

        protected void CheckBoxChecked_CheckedChanged(object sender, EventArgs e)
        {
            var checkBox = (CheckBox)sender;
            DataObject[checkBox.ClientID + (((ListViewDataItem)((Control)sender).Parent)).DataItemIndex] = checkBox.Checked;
        }

        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            var textBox = (TextBox)sender;
            DataObject[textBox.ClientID + (((ListViewDataItem)((Control)sender).Parent)).DataItemIndex] = textBox.Text;
        }
    }
}
于 2013-01-15T10:41:57.590 回答