我想在视图状态
中维护用户控件值我尝试了文本框,下拉值正在维护视图状态
但是BasicDatePicker值没有维护在视图状态我得到空值
代码:
的.aspx
<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="addnewtext" runat="server" Text="Add" onclick="addnewtext_Click" width="76px" />
<asp:Button ID="btnSaveVisa" Text="Save" runat="server" OnClick="btnSaveVisa_Click" />
.aspx.cs
private const string VIEWSTATEKEY = "ContactCount";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Set the number of default controls
ViewState[VIEWSTATEKEY] = ViewState[VIEWSTATEKEY] == null ? 1 : ViewState[VIEWSTATEKEY];
//Load the contact control based on Vewstate key
LoadContactControls();
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
LoadContactControls();
}
private void LoadContactControls()
{
for (int i = 0; i < int.Parse(ViewState[VIEWSTATEKEY].ToString()); i++)
{
rpt1.Controls.Add(LoadControl("AddVisaControl.ascx"));
}
}
protected void addnewtext_Click(object sender, EventArgs e)
{
LoadContactControls();
ViewState[VIEWSTATEKEY] = int.Parse(ViewState[VIEWSTATEKEY].ToString()) + 1;
}
private void saveData()
{
foreach (var control in rpt1.Controls)
{
var usercontrol = control as AddVisaControl;
string s = usercontrol.TextVisaNumber;
string date = usercontrol.ExpiryDate.ToShortDateString();
}
}
protected void Save_Click(object sender, EventArgs e)
{
saveData();
}
}
的.ascx
这里我添加了BasicDatePicker的程序集
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %>
<%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %>
<div id="divreg" runat="server">
<table id="tbl" runat="server">
<tr>
<td>
<asp:Label ID="lbl2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td> Visa Number:</td>
<td><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td> Country Name:</td>
<td><asp:DropDownList ID="dropCountry" Width="165px" runat="server"> </asp:DropDownList></td>
</tr>
<tr>
<td> Type of Visa:</td>
<td><asp:DropDownList ID="dropVisa" Width="165px" runat="server"></asp:DropDownList> </td>
<td> Type of Entry:</td>
<td><asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Expiry Date</td>
<td><BDP:BasicDatePicker ID="basicdate" runat="server"></BDP:BasicDatePicker></td>
<td>
<asp:Button ID="btnRemove" Text="Remove" runat="server" OnClick="btnRemove_Click" />
</td>
</tr>
</table>
</div>
.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
txtUser.Text = Request.Form[txtUser.UniqueID];
dropCountry.SelectedValue = Request.Form[dropCountry.UniqueID];
dropVisa.SelectedValue = Request.Form[dropVisa.UniqueID];
dropEntry.SelectedValue = Request.Form[dropEntry.UniqueID];
//Here I'm getting null value
basicdate.SelectedValue = Request.Form[basicdate.UniqueID];
}
public string TextVisaNumber
{
get { return txtUser.Text; }
set { txtUser.Text = value; }
}
public DateTime ExpiryDate
{
get
{
return basicdate.SelectedDate;
}
set
{
basicdate.SelectedDate = value;
}
}
在下面的图片中,当我给出文本框,下拉列表和日期选择器然后我单击添加按钮控件添加文本框,下拉值有,但是datepicker值消失
有什么想法吗?提前致谢
答案 0 :(得分:0)
您可以将值存储在隐藏控件中并将值恢复到回发的相应控件中,如果涉及到jquery,我猜它是,它经常会在回发时忘记它的值,无论viewstate是否启用等等。
看看at this blog post使用隐藏字段值设置数据贴纸值。
以下博客文章的代码:
<table>
<tr>
<td>
<asp:CheckBox ID="chkEnableMonthlyNotifications" runat ="server" CssClass="cell-Text" Text="Enable monthly notifications." />
<br/>
</td>
</tr>
<tr>
<td>
<label for="txtMonthStartDate">Specify start date for monthly notification</label>
<br />
<br />
<asp:TextBox ID="txtMonthStartDate" runat="server" Enabled="true"></asp:TextBox>
<asp:HiddenField ID="hdnMonthStartDate" runat="server" />
</td>
</tr>
<tr>
<td>
<label for="txtMonthEndDate">Specify end date for monthly notification, or leave blank if no end date.</label>
<br />
<br />
<asp:TextBox ID="txtMonthEndDate" runat="server" Enabled="true" > </asp:TextBox>
<asp:HiddenField ID="hdnMonthEndDate" runat="server" />
</td>
</tr>
</table>
使用备用字段初始化Datepicker的jQuery
$(document).ready(function ()
{
$("[id$=_txtMonthStartDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnMonthStartDate]' });
$("[id$=_txtMonthEndDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnMonthEndDate]' });
$("[id$=_txtQuarterStartDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnQuarterStartDate]' });
$("[id$=_txtQuarterEndDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnQuarterEndDate]' });
$("[id$=_txtHalfYearStartDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnHalfYearStartDate]' });
$("[id$=_txtHalfYearEndDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnHalfYearEndDate]' });
$("[id$=_txtYearStartDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnYearStartDate]' });
$("[id$=_txtYearEndDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnYearEndDate]' });
}
jQuery Code在document.ready
上再次设置Datepicker日期$(document).ready(function ()
{
//Initializing the datepicker
$("[id$=_txtMonthStartDate]").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '/icon_cal.png', altField: '[id$=_hdnMonthStartDate]' });
//Code to set the datepicker's date again after post back
if(!($("[id$=_txtMonthStartDate]").attr("Value") == undefined))
{
if($("[id$=_txtMonthStartDate]").attr("Value").length > 0)
{
$("[id$=_txtMonthStartDate]").datepicker("setDate",new Date($("[id$=_txtMonthStartDate]").val($("[id$=_txtMonthStartDate]").attr("Value"))));
}
}
}
答案 1 :(得分:0)
所以,这段代码与你的代码有点不同,但我知道它有效。我在这里工作了几个星期。我有一个预渲染,创建视图状态,并加载视图状态。每当我的一个值发生变化时,我就将它保存到我设置的私有属性中。
//Global ViewState Variables
private DataTable networksInfo;
private string HHmm;
private string Window;
private string ZoomFactorTop;
private string ZoomFactorBottom;
protected Boolean autoRefresh;
/// <summary>
/// This page prerender allows me to save viewstate objects before anything bad
/// happens. It's going to save any data that needs to persist across postbacks,
/// since the page refreshes with an autorefresh. Whenever updated, the ViewState
/// will also need to be updated.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Page_PreRender(object sender, EventArgs e)
{
// Save the ViewStateObjects before the page is rendered.
ViewState.Add("networksListVS", networksInfo);
ViewState.Add("HHmmVS", HHmm);
ViewState.Add("WindowVS", Window);
ViewState.Add("ZoomFactorTopVS", ZoomFactorTop);
ViewState.Add("ZoomFactorBottomVS", ZoomFactorBottom);
ViewState.Add("AutoRefresh", autoRefresh);
}
protected void Create_ViewState(DateTime now)
{
//Populate Dropdown handles the initial value for networksInfo, but none of the others are
//Set ViewState Vars' initial values
if (ViewState["HHmmVS"] == null)
{
int HH = now.Hour;
int mm = now.Minute;
Change_Time(HH, mm);
}
if (ViewState["WindowVS"] == null)
{
//The default time window to view is 6 hours
Window = "6";
}
if (ViewState["ZoomFactorTopVS"] == null)
{
//The default zoom factor is Window + 0
ZoomFactorTop = "0";
}
if (ViewState["ZoomFactorBottomVS"] == null)
{
//The default zoom factor is Window + 0
ZoomFactorBottom = "0";
}
if(ViewState["AutoRefresh"] == null)
{
autoRefresh = true;
}
}
protected void Load_ViewState()
{
//Before updating the charts, we know that this is a postback and that the viewstate shouldn't
//be null, but check anyways, just to be sure
if (ViewState["networksListVS"] != null)
{
networksInfo = (DataTable)ViewState["networksListVS"];
}
if (ViewState["HHmmVS"] != null)
{
HHmm = (String)ViewState["HHmmVS"];
}
if (ViewState["WindowVS"] != null)
{
Window = (String)ViewState["WindowVS"];
}
if (ViewState["ZoomFactorTopVS"] != null)
{
ZoomFactorTop = (String)ViewState["ZoomFactorTopVS"];
}
if (ViewState["ZoomFactorBottomVS"] != null)
{
ZoomFactorBottom = (String)ViewState["ZoomFactorBottomVS"];
}
if (ViewState["AutoRefresh"] != null)
{
autoRefresh = (Boolean)ViewState["AutoRefresh"];
}
}
我的一个变量实际上是一个日历对象。我使用它的方式是,如果用户选择较旧的时间,他们不希望页面刷新并且自动刷新关闭。
如果您不关心这一点,最简单的做法就是保存表示日期时间对象的状态。
现在,从我读过的东西,你需要一个预渲染(虽然这可以在页面加载中完成),一个创建视图状态(也可以在那里完成),以及一个加载视图状态。我看到的是你可能想要外部变量,而不是获取/设置,保存这些值的东西,因为当我保存我的viewstate时,它看起来更像是这样:
ZoomFactorTop = "0";
ZoomFactorBottom = "0";
//Save the changes just made to the objects the ViewState needs access to
SaveViewState();
希望有所帮助!