这里我在类文件account.vb中声明了getdata
method()。点击GET
按钮作为输出后,它会将msgbox
显示为0 0 ....所以请帮助我:
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
'declaring the obj of class account
Dim obj As New account
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
'initializing the object obj on class accounts
obj = New account
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
'obj.setdata(txt_accno.Text, txt_name.Text, txt_bal.Text)
MsgBox("value are set")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Account.vb
Imports Microsoft.VisualBasic
Public Class account
Private accno As Integer
Private acc_name As String
Private bal As Integer
'public method to populate above three private variable
Public Sub setdata(ByVal a As Integer, ByVal b As String, ByVal c As Integer)
Me.accno = a
Me.acc_name = b
Me.bal = c
End Sub
Public Sub getdata()
MsgBox(Me.accno.ToString + vbNewLine + Me.acc_name + vbNewLine + Me.bal.ToString)
End Sub
End Class
答案 0 :(得分:0)
您正在尝试在页面中使用Obj。无论何时单击按钮,Obj可以重置为回发。您需要存储Obj的值。有很多方法可以存储它。 ASP.NET会话状态使您能够在用户在Web应用程序中导航ASP.NET页面时存储和检索用户的值。 HTTP是无状态协议。这意味着Web服务器将页面的每个HTTP请求视为独立请求。服务器不保留先前请求期间使用的变量值的知识。 ASP.NET会话状态在有限时间窗口内将来自同一浏览器的请求标识为会话,并提供了在该会话期间持久保存变量值的方法。默认情况下,为所有ASP.NET应用程序启用ASP.NET会话状态。
因此使用会话Obj对象保留,当您设置和获取数据时,即使回发发生,它仍保留在页面上
尝试以下代码
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
'declaring the obj of class account
Dim obj As New account
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
'initializing the object obj on class accounts
obj = New account
session("ClsObj") = obj
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj = session("ClsObj")
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
'obj.setdata(txt_accno.Text, txt_name.Text, txt_bal.Text)
MsgBox("value are set")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj = session("ClsObj")
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
*************************************************
Account.vb
Imports Microsoft.VisualBasic
Public Class account
Private accno As Integer
Private acc_name As String
Private bal As Integer
'public method to populate above three private variable
Public Sub setdata(ByVal a As Integer, ByVal b As String, ByVal c As Integer)
Me.accno = a
Me.acc_name = b
Me.bal = c
End Sub
Public Sub getdata()
MsgBox(Me.accno.ToString + vbNewLine + Me.acc_name + vbNewLine + Me.bal.ToString)
End Sub
End Class
答案 1 :(得分:0)
试试这个:
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
Dim obj As New account
obj = New account
Session["obj"] = obj
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
Try
Dim obj as New account
obj = CType(Session["obj"], account)
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
MsgBox("value are set")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
Try
Dim obj as New account
obj = CType(Session["obj"], account)
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub