C# 如何在视图状态下维护用户控件值?
我想在视图状态下维护用户控件值 我试过文本框,下拉值在视图状态下保持不变 但BasicDatePicker值未在视图状态下维护,我得到的是null值 代码: .aspx 在下图中,当我给出文本框、下拉列表和日期选择器时,我单击添加按钮控件添加文本框,下拉列表值在那里,但日期选择器值消失C# 如何在视图状态下维护用户控件值?,c#,asp.net,user-controls,viewstate,C#,Asp.net,User Controls,Viewstate,我想在视图状态下维护用户控件值 我试过文本框,下拉值在视图状态下保持不变 但BasicDatePicker值未在视图状态下维护,我得到的是null值 代码: .aspx 在下图中,当我给出文本框、下拉列表和日期选择器时,我单击添加按钮控件添加文本框,下拉列表值在那里,但日期选择器值消失 有什么想法吗?提前感谢您可以将该值存储在一个隐藏控件中,并在回发时将该值恢复到相应的控件中,如果涉及jquery(我猜是这样的),那么它在回发时通常会忘记它的值,而不管是否启用了viewstate等 查看如何使
有什么想法吗?提前感谢您可以将该值存储在一个隐藏控件中,并在回发时将该值恢复到相应的控件中,如果涉及jquery(我猜是这样的),那么它在回发时通常会忘记它的值,而不管是否启用了viewstate等 查看如何使用隐藏字段值将datapicker值设置为ready 以下是上述博客文章中的代码:
<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>
用于在document.ready上再次设置日期选择器日期的jQuery代码
$(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"))));
}
}
}
所以,这段代码会和你的有点不同,但我知道它是有效的。我已经在这里工作了好几个星期了。我已经完成了预渲染、创建viewstate和加载viewstate。每当我的某个值发生更改时,我都会将其保存到已设置的私有属性中
//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工作起来,它就简单多了。
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;
}
}
<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>
$(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]' });
}
$(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"))));
}
}
}
//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"];
}
}
ZoomFactorTop = "0";
ZoomFactorBottom = "0";
//Save the changes just made to the objects the ViewState needs access to
SaveViewState();