我有mail.aspx页面。哪些必须要撰写邮件?此外,用户可以从用户帐户中选择用户,抄送和密件抄送。当更新面板更新方法从文本框中调用所有以前的值时,它们也会随之变空。为了避免它们,我创建了一个方法,该方法采用以前的值并针对特定条件使用它们进行呈现。
这是我的方法:
private void SetViewStateData(bool sTitle, bool sFromuser, bool sTouser, bool sCc, bool sBcc, bool sBody)
{
if (sTitle.Equals(true))
{
ViewState["sTitle"] = Request.Form.Get("txt_ComName1");
}
else
{
ViewState["sTitle"] = string.Empty;
}
if (sFromuser.Equals(true))
{
ViewState["sFromuser"] = Request.Form.Get("txt_ComName2");
}
else
{
ViewState["sFromuser"] = string.Empty;
}
if (sTouser.Equals(true))
{
ViewState["sTouser"] = Request.Form.Get("txt_ComName3");
}
else
{
ViewState["sTouser"] = string.Empty;
}
if (sCc.Equals(true))
{
ViewState["sCc"] = Request.Form.Get("txt_ComName4");
}
else
{
ViewState["sCc"] = string.Empty;
}
if (sBcc.Equals(true))
{
ViewState["sBcc"] = Request.Form.Get("txt_ComName5");
}
else
{
ViewState["sBcc"] = string.Empty;
}
if (sBody.Equals(true))
{
ViewState["sBody"] = Request.Form.Get("CKEditor1");
}
else
{
ViewState["sBody"] = string.Empty;
}
txt_ComName1.Text = sTitle.Equals(false) ? string.Empty : ViewState["sTitle"].ToString();
txt_ComName2.Text = sFromuser.Equals(false) ? string.Empty : ViewState["sFromuser"].ToString();
txt_ComName3.Text = sTouser.Equals(false) ? string.Empty : ViewState["sTouser"].ToString();
txt_ComName4.Text = sCc.Equals(false) ? string.Empty : ViewState["sCc"].ToString();
txt_ComName5.Text = sBcc.Equals(false) ? string.Empty : ViewState["sBcc"].ToString();
CKEditor1.Text = sBody.Equals(false) ? string.Empty : ViewState["sBody"].ToString();
}
如果bool status为true或string.empty,则txt_ComName2.Text从视图状态值设置时,这将引发错误。但是,这在第一个文本框中可以正常工作。当第二秒,它给我一个错误,如:
Server Error in '/EASYMAIL_METRO' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 240: }
Line 241: txt_ComName1.Text = sTitle.Equals(false) ? string.Empty : ViewState["sTitle"].ToString();
Line 242: txt_ComName2.Text = sFromuser.Equals(false) ? string.Empty : ViewState["sFromuser"].ToString();
Line 243: txt_ComName3.Text = sTouser.Equals(false) ? string.Empty : ViewState["sTouser"].ToString();
Line 244: txt_ComName4.Text = sCc.Equals(false) ? string.Empty : ViewState["sCc"].ToString(); Source File: c:\shalin\EASYMAIL_METRO\mail.aspx.cs Line: 242
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
mail.SetViewStateData(Boolean sTitle, Boolean sFromuser, Boolean sTouser, Boolean sCc, Boolean sBcc, Boolean sBody) in c:\shalin\EASYMAIL_METRO\mail.aspx.cs:242
mail.btn_add_Click(Object sender, EventArgs e) in c:\shalin\EASYMAIL_METRO\mail.aspx.cs:1823
mail.Page_Load(Object sender, EventArgs e) in c:\shalin\EASYMAIL_METRO\mail.aspx.cs:71
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678
这是怎么回事...请帮助我,伙计们...