2013-01-15 27 views
0

我正在開發一個應用程序,它使用ListView來顯示問題,它是來自DataBase不同表中的選項。
我已將PageSize="1"分配給DataPager,因此在選擇用戶單擊顯示其他問題的下一個按鈕後,一次只能顯示一個問題,但如果用戶單擊上一個按鈕,ListView未顯示上一個選定的選項? 我不知道如何保留之前選擇的選項,因爲我必須在DataBase的最後提交它們。如何在下一個按鈕的列表視圖中保存值?

+0

我猜你需要的是保存頁面後背上之間的控件的值,而不保存到數據庫。這是對的嗎? –

+0

請添加相關代碼 – Blachshma

回答

0

您可以在Session中的Hashtable中保留控件的值。以下是爲ListView中的每個項目呈現2 TextBox控件和CheckBox的示例頁面。該狀態在尋呼事件之間保留。

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; 
     } 
    } 
} 
0

要保留這些scenerios中的值,請使用Session對象。你可以創建一個值的集合 - 問題ID,答案ID等,並將其存儲在會話(HashTable是一個不錯的選擇)。當用戶退回時,從Session中獲取該問題Id的值。每當用戶移動到下一個問題時刷新會話對象。但請記住處理以下情況: - 用戶移回,然後再移回,問題Id已在收集中。

相關問題