如何将对象从一个子对象转到另一个子对象

时间:2012-01-13 03:12:50

标签: vb.net

我想要做的是从page_load获取intGuestID1以在bth_add区域中使用 因为当我从另一个表单点击到frmAddFollowUp时我试图获取ID所以我试图从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

2 个答案:

答案 0 :(得分:0)

您可以指定ByRef参数类型,也可以使用返回对象引用的Function。在你的代码片段中,你可以在类级别(字段)声明变量,这样你就可以在不同的事件处理程序/ sub中使用它们。

答案 1 :(得分: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方法中,删除Dim intGuestID1 As Integer行,因为您不再需要声明它。最后,在您看到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。