我正在開發一個應用程序,它使用ListView
來顯示問題,它是來自DataBase
不同表中的選項。
我已將PageSize="1"
分配給DataPager
,因此在選擇用戶單擊顯示其他問題的下一個按鈕後,一次只能顯示一個問題,但如果用戶單擊上一個按鈕,ListView
未顯示上一個選定的選項? 我不知道如何保留之前選擇的選項,因爲我必須在DataBase
的最後提交它們。如何在下一個按鈕的列表視圖中保存值?
0
A
回答
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已在收集中。
相關問題
- 1. 如何從按鈕創建一個下拉列表視圖?
- 2. 如何在列表視圖(Android)中觸發一個按鈕?
- 3. 在Android上的列表視圖下添加一個按鈕
- 4. Android:如何避免在按下列表項時按下列表視圖中的按鈕的視覺
- 5. 如何在按下另一個視圖中的按鈕時調用視圖?
- 6. 如何在點擊按鈕後保存選定的列表值?
- 7. XML佈局,在一個列表視圖/文本視圖下的按鈕
- 8. 列表視圖中,無法按一下按鈕
- 9. 如何更改列表視圖上的圖像按鈕,當長按一下
- 10. 如何在一個列表視圖中保存一個項目的狀態android
- 11. 在SharedPreference中保存列表視圖中存在的所有值
- 12. 保存一個列表視圖菜單
- 13. 如何在按下時保存圖像按鈕的狀態?
- 14. 在視圖中按值保存排序
- 15. 刷新列表視圖點擊該列表視圖中的一個按鈕
- 16. 如何在樹形視圖中添加按鈕下一個打印按鈕?
- 17. 如何在列表視圖中添加兩個按鈕
- 18. 在列表視圖中插入字符串按下按鈕
- 19. Android:在列表視圖項中按下按鈕
- 20. 如何將每個視圖都保存在一個視圖中?
- 21. 兩個按鈕更新列表視圖中的一個項目
- 22. 如何保存值,當我點擊前一個按鈕在asp.net
- 23. 如何設置按鈕在一些條件下的列表視圖
- 24. 在列表視圖中點擊按鈕
- 25. 在列表視圖中顯示按鈕
- 26. 在列表視圖中的上下文感知按鈕
- 27. 如何在列表視圖中選擇一行只按下一個ImageView
- 28. 如何使用列表視圖中的按鈕刪除列表視圖項目
- 29. 將動態表格視圖中按鈕的狀態保存在一個UITableViewController中以在另一箇中查看
- 30. 如何保存從下拉列表中選擇的多個值
我猜你需要的是保存頁面後背上之間的控件的值,而不保存到數據庫。這是對的嗎? –
請添加相關代碼 – Blachshma