2012-01-13 150 views
1

我想要做的是從page_load中獲取intGuestID1以便在bth_add區域中使用 因爲我試圖從已點擊的位置獲取ID另一種形式frmAddFollowUp,所以我試圖從page_load請求它,因爲當我從添加按鈕請求時,它只會給我數字0而不是以前的形式的ID。我如何獲得從一個子對象到另一個子

Partial Class frmAddFollowUp 
Inherits System.Web.UI.Page 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    Dim objCDBFeedback As New CDBFeedback 
    Dim objCDBDepartment As New CDBDepartment 

    Dim intGuestID1 As Integer 
    Dim arrList As New ArrayList 





    If Page.IsPostBack = False Then 
     intGuestID1 = Request.QueryString("id") 

     arrList = objCDBDepartment.getAllDepartmentDropDownList 
     lstDepartment.DataSource = arrList 
     lstDepartment.DataTextField = "DepartmentName" 
     lstDepartment.DataValueField = "Department" 
     lstDepartment.DataBind() 



    End If 

End Sub 

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click 
    Dim CheckBoolean As Boolean = True 
    Dim objCDBFeedback As New CDBFeedback 
    Dim objCFeedback As New CFeedback 

    If txtStaffName.Text = "" Then 
     lblValidateStaffName.Text = "*Please enter Staff Name." 
     CheckBoolean = False 
    Else 

     lblValidateStaffName.Text = "" 
    End If 

    If txtFollowUpSummary.Text = "" Then 
     lblValidateFollowUpSummary.Text = "*Please enter Follow up summary." 
     CheckBoolean = False 
    ElseIf txtFollowUpSummary.Text.Contains("'") Then 
     txtFollowUpSummary.Text = txtFollowUpSummary.Text.Replace("'", "''") 
    Else 
     lblValidateFollowUpSummary.Text = "" 
    End If 

    If txtAmount2.Text = "" Then 
     lblValidateAmount2.Text = "*Please enter the Amount or put in NIL if there is no amount." 
     CheckBoolean = False 
    Else 
     lblValidateAmount2.Text = "" 
    End If 

    If CheckBoolean = False Then 

     If txtStaffName.Text.Contains("''") Then 
      txtStaffName.Text = txtStaffName.Text.Replace("''", "'") 
     End If 
     If txtFollowUpSummary.Text.Contains("''") Then 
      txtFollowUpSummary.Text = txtFollowUpSummary.Text.Replace("''", "'") 
     End If 

    Else 
     Dim intNumOfRecordsAffected As Integer 
     objCFeedback.GuestId = intGuestID1 
     objCFeedback.Feedback = txtFollowUpSummary.Text 
     objCFeedback.Department = lstDepartment.SelectedItem.Value 
     objCFeedback.StaffName = txtStaffName.Text 

     objCFeedback.Amount = txtAmount2.Text 

     intNumOfRecordsAffected = objCDBFeedback.addNewFollowUp(objCFeedback) 
     Response.Redirect("frmIncident.aspx") 
    End If 

End Sub 

回答

0

可在任一指定ByRef參數類型或使用Function返回對象的參考。在您的代碼片段中,您可以在類級別(字段)聲明變量,以便您可以在不同的事件處理程序/子級中使用它們。

0

使intGuestID1成爲私有成員變量。這將允許你在這兩個函數中訪問這個變量。

此代碼是否正在編譯?我沒有看到在btnAdd_Click中聲明intGuestID1,這應該是拋出一個錯誤。

要聲明一個私有成員變量添加上面的Page_Load函數如下,例如:現在

Partial Class frmAddFollowUp 
Inherits System.Web.UI.Page 

Private _intGuestID1 as Integer ' The _ prefix is just a naming convention that is used for class member variables, if you don't like it, you can remove it. 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

,在你的Page_Load方法,刪除行昏暗intGuestID1爲整數,因爲你不再需要聲明這個。最後,在任何你看到intGuestID1的地方,將它重命名爲_intGuestID1

此外,一些可能重構此方法的方法是創建一個只讀屬性,它封裝了從QueryString獲取_intGuestID1的事實。請看下面的例子:

Private ReadOnly Property GuestID1() as Int32 
Get 
    Return Request.QueryString("id") 
End Get 
End Property 

其中一個這樣做是使用它,如之前執行的值的任何檢查的好處,是它存儲在的Request.QueryString(實數「ID」 ),還是允許用戶訪問由此ID代表的實體,或者您可以想到的任何其他內容。當你這樣做時,你需要將所有對_intGuestID1的引用改爲GuestID1。

+0

我如何讓它成爲私人成員變量?對不起,在這個新的..至於intGuestID1不在btnAdd_click中聲明..因爲我在想我是否可以在page_load中聲明它,並將其帶入到btnAdd_click,但我不知道代碼採取 – waterbottle 2012-01-13 03:38:29

+0

沒有需要對不起,我們都從某個地方開始。給我一下,我會更新我的答案,如何做到這一點 – 2012-01-13 03:41:08

相關問題