2012-04-03 30 views
3

我想在更新窗體視圖之前運行if; 如果是,那麼...「消息」&取消更新查詢 如果沒有繼續更新查詢。關於更新的ASP.NET窗體視圖IF

我試過這個,但我得到一個「obeject實例沒有設置爲空實例......」在第一行的if?和物品更新不管

Private Sub FormView2_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView2.ItemUpdating 
    Dim status As TextBox = FormView1.FindControl("ApprovalStatusTextBox") 


    If status.Text = "approved" Or "denied" Then 
     e.Cancel = True 
     lblupdaterequest.Text = "you cannot update this request as it has already been responded to" 
    Else 
     HolidayDetailsdatasource.Update() 
    End If 

任何人都知道更好的是實現這樣的東西?

確切的錯誤:

System.NullReferenceException was unhandled by user code 
    Message=Object reference not set to an instance of an object. 
    Source=WebApplication1 
    StackTrace: 
    at WebApplication1.HolidayApprovalDetails.DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e) in line 32 
    at System.Web.UI.WebControls.DetailsView.OnItemUpdating(DetailsViewUpdateEventArgs e) 
    at System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg, Boolean causesValidation) 
    at System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) 
    at System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) 
    at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) 
    at System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) 
    at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) 
    at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) 
    at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) 
    at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
+0

你能發佈真實而乾淨的錯誤信息嗎?也許複製和粘貼?這聽起來像你的狀態文本框不存在或爲空?你可以調試嗎? – Remy 2012-04-03 21:13:37

+0

這個文本框在哪裏,我的意思是在哪個模板(fe ['EditItemTemplate'](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.edititemtemplate.aspx)? 'FormView'有三個不同的['FormViewModes'](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formviewmode.aspx)。除此之外,我建議使用[ 'FormViewUpdateEventArgs.NewValues'](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formviewupdateeventargs.newvalues.aspx)而不是 – 2012-04-03 22:01:34

+0

是的。它的edititemtemplate – user1055487 2012-04-03 22:12:55

回答

2

我至少可以看到您檢索狀態文本框的從「FormView1」的值,而你的子ItemUpdating是指「FormView2 」。

改變之後,嘗試如果前添加一個消息框,以確保你得到了你想要的正確值:

MsgBox(status.Text) 

希望,它可以幫助您解決問題。

+0

好景點!謝謝 – user1055487 2012-04-04 18:29:09

1
If status IsNot Nothing AndAlso (status.Text = "approved" OrElse status.Text = "denied") Then 
+0

它不會再崩潰了,但它是st不管「狀態」是什麼更新,我錯過了什麼? – user1055487 2012-04-03 21:33:27