ASP.Net Accessing Object Properties in Control Attribute - asp.net

I've two classes as these
Namespace Business
Public Class Core
Public Shared ReadOnly Property Settings As DBManager.Settings
Get
Return DBManager.Settings.GetSettings
End Get
End Property
End Class
End Namespace
Namespace DBManager
Public Class Settings
Private _Name As String
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _Title As String
Public Property Title As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Public Shared Function GetSettings() As Settings
Return New Settings With {.Name = "Website", .Title = "My Product Site"}
End Function
End Class
End Namespace
Now, I wish to create a DataBound Label control with a property name as DataProperty where I can pass the full path of the property name.
Namespace Application.Controls
Public Class ExtendedLabel
Inherits Label
Public Property DataProperty As String
Get
If ViewState("DataProperty") Is Nothing Then
Return String.Empty
Else
Return CStr(ViewState("DataProperty"))
End If
End Get
Set(ByVal value As String)
ViewState("DataProperty") = value
End Set
End Property
Private Sub ExtendedLabel_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(DataProperty) Then
Me.Text = GetReflectedValue()
End If
End Sub
Private Function GetReflectedValue() As String
//'Need suggestion here
End Function
End Class
End Namespace
usage will be something like this
<cc:ExtendedLabel id="elName" runat="server" DataProperty="Business.Core.Settings.Name" />
Kindly suggest a way to access this value using Reflection.
Just to clarify, I want to be able to access any property in any class of any namespace, static or instantiated. Therefore I cannot use a declarative format as given in
get value of a property by its path in asp.net
Get property Value by its stringy name

Related

How to convert returned asmx class in aspx

I need some pointers with understanding how to convert a returned asmx class from within aspx codebehind. I created a prototype asmx and aspx pages to test this functionality that once sucessfully working I'd like to extend to a project I'm working on.
Although I'm using the same class definition within the asmx and aspx vb codebehind, visual studio is noting a conversion incompatiability error "Error BC30311 Value of type 'websvc_returnvalues' cannot be converted to 'WebServiceConsume.websvc_returnvalues'". This error is denoted in visual studio on the following line in aspx.vb:
rtnvals = websvc.test()
I tried doing a simple type conversion but it has the same kind of error: Unable to cast object of type 'websvctest.websvc_returnvalues' to type 'websvc_returnvalues' ... so obviously I'm not understanding how to convert between the two classes.
Private Function cvt_websvc_returnvalues(i As Object) As websvc_returnvalues
Return CType(i, websvc_returnvalues)
End Function
Thanks in advance for any suggestions I can try! Stackoverflow is my primary source for answering my software questions!
Webservice:
I have the following webservice referenced as websvctest in my project:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://sample.org/")>
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebServiceTest
Inherits System.Web.Services.WebService
<Serializable()>
Public Class websvc_returnvalues
Public w_brtn As Boolean
Public w_rtnval As String
Public w_rtnerr As String
Sub New()
w_brtn = False
w_rtnval = ""
w_rtnerr = ""
End Sub
Public Property Ok As Boolean
Get
Return w_brtn
End Get
Set(value As Boolean)
w_brtn = value
End Set
End Property
Public Property value As String
Get
Return w_rtnval
End Get
Set(value As String)
w_rtnval = value
End Set
End Property
Public Property err As String
Get
Return w_rtnerr
End Get
Set(value As String)
w_rtnerr = value
End Set
End Property
End Class
Public Sub New()
End Sub
<WebMethod()>
Public Function test() As websvc_returnvalues
Dim b As Boolean = False
Dim rtn As websvc_returnvalues = New websvc_returnvalues
Try
b = True
Catch ex As Exception
rtn.err = ex.Message
End Try
rtn.Ok = b
Return rtn
End Function
End Class
WebServiceConsume.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="WebServiceTestConsume.aspx.vb" Inherits="WebServiceConsume" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<label for="websvc_ok">ok</label><asp:Literal ID="websvc_ok" runat="server"></asp:Literal><br />
<label for="websvc_value">value</label><asp:Literal ID="websvc_value" runat="server"></asp:Literal><br />
<label for="websvc_err">err</label><asp:Literal ID="websvc_err" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
WebServiceconsume.aspx.vb
Note the same class definition for websvc_returnvalues here as in the asmx
Partial Class WebServiceConsume
Inherits System.Web.UI.Page
Private websvc As New websvctest.WebServiceTest
Public Class websvc_returnvalues
Public w_brtn As Boolean
Public w_rtnval As String
Public w_rtnerr As String
Sub New()
w_brtn = False
w_rtnval = ""
w_rtnerr = ""
End Sub
Public Property Ok As Boolean
Get
Return w_brtn
End Get
Set(value As Boolean)
w_brtn = value
End Set
End Property
Public Property value As String
Get
Return w_rtnval
End Get
Set(value As String)
w_rtnval = value
End Set
End Property
Public Property err As String
Get
Return w_rtnerr
End Get
Set(value As String)
w_rtnerr = value
End Set
End Property
End Class
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim rtnvals As websvc_returnvalues
Try
rtnvals = websvc.test() ' visual studio error
rtnvals = cvt_websvc_returnvalues(websvc.test()) ' runtime error
Me.websvc_ok.Text = rtnvals.Ok.ToString
simp Me.websvc_value.Text = rtnvals.value.ToString
Me.websvc_err.Text = rtnvals.err.ToString
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Testing")
End Try
End Sub
Private Function cvt_websvc_returnvalues(i As Object) As websvc_returnvalues
Return CType(i, websvc_returnvalues)
End Function
End Class
Doh! I can answer my own question ...
I just needed to type in the correct namespace against the websvc_returnvalues class:
Dim rtnvals As websvctest.websvc_returnvalues

how to use webmethod on webform in asp.net

I have a user control on my web form which is as follows
I select the program year, category type , category and position group and then click search, it should return a collection which contains the programyearID, categorytypeID and positionGroupID for the corresponding selected items in the dropdownlist and pass it on to a webmethod which is already defined to get a specific certificationID.
Interface
Namespace SI.Certification.UserControl
Public Interface IUserSearchResultList
Property DataSource() As User.Learning.Business.HR.UserCollection
ReadOnly Property List() As User.Web.UI.WebControls.PagedRepeater
End Interface
End Namespace
here is this the code for my usercontrol1.vb
Public Class curriculum_search
Inherits System.Web.UI.UserControl
Implements IUserSearchResultList
Private _dataSource As UserCollection
Public Property DataSource As UserCollection Implements UserControl.IUserSearchResultList.DataSource
Get
Return _dataSource
End Get
Set(value As UserCollection)
End Set
End Property
Public ReadOnly Property List As PagedRepeater Implements UserControl.IUserSearchResultList.List
Get
Return ctlListControl
End Get
End Property
#Region "Public Properties"
Public Property ProgramYearID() As Int32
Get
If Me.ShowProgramYearSearch Then
If Me.lstProgramYear.SelectedValue = 0 Then
Return Integer.MinValue
Else
Return Me.lstProgramYear.SelectedValue
End If
Else
If ViewState("ProgramYearID") Is Nothing Then
Return Integer.MinValue
Else
Return ViewState("ProgramYearID")
End If
End If
End Get
Set(ByVal Value As Int32)
If Me.ShowProgramYearSearch Then
Me.lstProgramYear.SelectedValue = Value.ToString()
End If
ViewState("ProgramYearID") = Value
End Set
End Property
Public ReadOnly Property CategoryTypeID() As Int32
Get
If Me.lstCategoryType.SelectedValue = 0 Then
Return Integer.MinValue
Else
Return Me.lstCategoryType.SelectedValue
End If
End Get
End Property
Public ReadOnly Property CategoryID() As Int32
Get
If Me.lstCategory.SelectedValue = 0 Then
Return Integer.MinValue
Else
Return Me.lstCategory.SelectedValue
End If
End Get
End Property
Public Property PositionGroupID() As Int32
Get
If Me.ShowPositionCodeSearch Then
If Me.lstPositionGroup.Selected = -1 Then
Return Integer.MinValue
Else
Return Me.lstPositionGroup.Selected
End If
Else
If ViewState("PositionGroupID") Is Nothing Then
Return Integer.MinValue
Else
Return ViewState("PositionGroupID")
End If
End If
End Get
Set(ByVal Value As Int32)
If Me.ShowPositionCodeSearch Then
Me.lstPositionGroup.Selected = Value.ToString()
End If
ViewState("PositionGroupID") = Value
End Set
End Property
Public Property ShowPositionCodeSearch() As Boolean
Get
If ViewState("ShowPositionCodeSearch") Is Nothing Then
Return True
Else
Return ViewState("ShowPositionCodeSearch")
End If
End Get
Set(ByVal Value As Boolean)
ViewState("ShowPositionCodeSearch") = Value
If Not Value Then
spnPositionGroup.Visible = False
Else
spnPositionGroup.Visible = True
End If
End Set
End Property
Public Property ShowProgramYearSearch() As Boolean
Get
If ViewState("ShowProgramYearSearch") Is Nothing Then
Return True
Else
Return ViewState("ShowProgramYearSearch")
End If
End Get
Set(ByVal Value As Boolean)
ViewState("ShowProgramYearSearch") = Value
If Not Value Then
spnProgramYear.Visible = False
End If
End Set
End Property
#End Region
#Region "Events"
Public Event Submit(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
If Page.IsPostBack = False Then
If spnCategoryType.Visible = True Then
BindCertificationCategoryTypeCollection()
'method to bind the category collection
End If
If spnProgramYear.Visible = True AndAlso Me.ShowProgramYearSearch Then
BindProgramYearCollection(GetProgramYearCollection())
'method to bind the porgram year collection
End If
If spnPositionGroup.Visible = True AndAlso Me.ShowPositionCodeSearch Then
End If
lstPositionGroup.DefaultToPrimary = False
End If
End Sub
Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlSearch.SearchClick
RaiseEvent Submit(sender, e)
End Sub
This is the webmethod I have to use in my usercontrol to pass the parameters
<WebMethod(Description:="Get list of certification levels")> _
Public Function GetCertificationLevelsList(ByVal programYearId As Integer, ByVal PositionGroupId As String, ByVal CategoryId As
String) As User.Department.Application.Curriculum.Items
Dim items As User.Department.Application.Curriculum.Items = User.Department.Application.Curriculum.CategoryCollection.GetCategoryLevelsList(programYearId,
PositionGroupId, CategoryId)
items.Sort()
Return items
End Function
I am stuck here.
You can't use webmethods on Usercontrols. Webmethods must live on webpages. Since UserControls can live on multiple pages, you can't put a webmethod in a codebehind page of a UserControl.
So your options are:
Create an .asmx for your webmethod and call it from your webcontrol (old-skool)
Create a WCF enpoint (.svc) and call it from your webcontrol (semi-old-skool)
Create a WebAPI Controller and call it from your webcontrol (new-skool)
However, what you want to do is to call the webservice from the code-behind of your usercontrol. If you want to do this, you're mixing server-side and client-side. Your asmx is there to be used from the client-side. You will either need to copy the code from the asmx, or provide an abstraction that can be used in both the webservice and the usercontrol.

How to read a page variable from a master page

If a unique variable is assigned to each individual page; in this case PageID.
How can PageID be referenced from a master page on load?
PAGE VB
Partial Class Index
Inherits System.Web.UI.Page
Dim PageID As Integer = 1
End Class
MASTER VB
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If PageID = 1 Then
...
End If
End Sub
End Class
I am aware this can be achieved with querystring however I am looking for a more efficient and secure solution due to the potential quantity of pages.
Expose a public property that returns that field, then you just have to cast the Page property in the MasterPage to the concrete type:
Dim index As Index = TryCast(Me.Page, Index)
If index IsNot Nothing Then
Dim pageID As Int32 = index.PageID
End If
And in the Index class:
Partial Class Index
Inherits System.Web.UI.Page
Private _PageID As Integer = 1
Public Property PageID As Int32
Get
Return _PageID
End Get
Set(value As Int32)
_PageID = value
End Set
End Property
End Class
Since you want that in every page you need to define an interface. You could let them implement the same Interface for example IPageable:
Public Interface IPagable
Property PageID As Int32
End Interface
Now let all pages implement it:
Partial Class Index
Inherits System.Web.UI.Page
Implements IPagable
Private _PageID As Integer = 1
Public Property PageID As Int32 Implements IPagable.PageID
Get
Return _PageID
End Get
Set(value As Int32)
_PageID = value
End Set
End Property
End Class
and change the MasterPage accordingly:
Dim pageable As IPagable = TryCast(Me.Page, IPagable)
If pageable IsNot Nothing Then
Dim pageID As Int32 = pageable.PageID
End If

Consume ASMX Webservice From Classic ASP Using SOAP Client 3.0

I made a webservice in VB.Net with a method returning a custom class or object.
<WebMethod()> _
Public Function CreatePerson(ByVal LastName As String, ByVal FirstName As String) As Person
Return New Person(LastName, FirstName)
End Function
Public Class Person
Public Sub New()
End Sub
Public Sub New(ByVal LastName As String, ByVal FirstName As String)
_LastName = LastName
_FirstName = FirstName
End Sub
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName= value
End Set
End Property
End Class
I can consume it from another ASP.NET Application but the problem is when i try to consume it from Classic ASP through SOAP Client 3.0
<%
Dim Result, oSoapClient
Set oSoapClient = Server.CreateObject("MSSOAP.SoapClient30")
oSoapClient.ClientProperty("ServerHTTPRequest") = True
Call oSoapClient.mssoapinit ("http://MyServer/MyWebService/MyWebService.asmx?WSDL")
Result = oSoapClient.CreatePerson("Sassaroli", "Rinaldo")
Response.Write(Result.LastName)
%>
I get an error:
Microsoft VBScript runtime error '800a01a8'
Object required
at "Response.Write(Result.LastName)" Line.
I can see Result is a string array with no elements
I believe that the root cause of the error is the lack of a Set keyword on the line that invokes the web service method. It should look like:
Set Result = oSoapClient.CreatePerson("Sassaroli", "Rinaldo")
That will get you past your initial problem. After that you'll need to read the result object. Your subsequent line of code:
Response.Write(Result.LastName)
will most likely result in another error. That's because the result you're getting is not really a "Person" object, it's an XML representation of that object. You can verify this with the following code:
<%
Dim Result, oSoapClient
Set oSoapClient = Server.CreateObject("MSSOAP.SoapClient30")
oSoapClient.ClientProperty("ServerHTTPRequest") = True
Call oSoapClient.mssoapinit ("http://MyServer/MyWebService/MyWebService.asmx?WSDL")
Set Result = oSoapClient.CreatePerson("Sassaroli", "Rinaldo")
Response.Write( TypeName( Result ) & "<br/>" & vbCrLf )
Response.Write( Result.item(0).text )
%>
The type that will be shown by the TypeName call will be IXMLDomSelection. You can access nodes for this object through methods and properties that are documented here.
The last line of code will display the value of the LastName property.
Hope this leads you in the correct direction.

WCF - Can I use an existing type to be passed through my WCF service

I have a service. I have an existing class of business objects. What I would like to know is how can I pass a class through WCF from the business object assembly without having to create a new class in my WCF site while appending or tags?
Here is an existing UDT:
Namespace example: Application.BusinessObjects.Appointments
Public Structure AppointmentResource
Private _id As String
Private _type As ResourceTypeOption
Private _name As String
Property id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
Property type() As ResourceTypeOption
Get
Return CType(_type, Int32)
End Get
Set(ByVal value As ResourceTypeOption)
_type = value
End Set
End Property
Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New(ByVal id As String, ByVal type As ResourceTypeOption, ByVal name As String)
_id = id
_type = type
_name = name
End Sub
End Structure
Here is the same one I created with the data contract attributes:
Namespace example: Application.Service.Appointments
<DataContract()> _
Public Structure AppointmentResource
Private _id As String
Private _type As ResourceTypeOption
Private _name As String
<DataMember()> _
Property id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
<DataMember()> _
Property type() As ResourceTypeOption
Get
Return CType(_type, Int32)
End Get
Set(ByVal value As ResourceTypeOption)
_type = value
End Set
End Property
<DataMember()> _
Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New(ByVal id As String, ByVal type As ResourceTypeOption, ByVal name As String)
_id = id
_type = type
_name = name
End Sub
End Structure
There is an easy way to share types between client and service, just by adding reference to shared type assembly to your client BEFORE adding the service reference.
You can find the detailed scenario and sample project there:
http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types-between-server-and-client.html
ResourceTypeOption also appears to be a custom class, so you would to define that as part of the contract in its own class. The client has to know about that and so it needs its own contract. Clients already know how to deal with CLR types like string. Any other custom types would also have to be defined in the contract.

Resources