How to read a page variable from a master page - asp.net

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

Related

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.

Convert string to list of custom objects

Say I have an object Fish with properties like
Public Property ID As Integer
Public Property Name As String
Public Property Type As Integer
Public Property Age As Integer
And I have a string that looks like this:
"Fishes[0].ID=1&Fishes[0].Name=Fred&Fishes[0].Type=1&Fishes[0].Age=3&Fishes[1].ID=2&Fishes[1].Name=George&Fishes[1].Type=2&Fishes[1].Age=5&..."
Is there any way of converting/casting/whatever my string into a list of Fish objects? I can't seem to find anything to help. I do have some control over the format of the string if that would make things easier.
Many thanks
What you need to do is to parse the input string to look for Fishes[X].PROPNAME=VALUE pattern. Loop through all matches found in the string and add into or set the existing object in Dictionary. Use X as each object key in the Dictionary.
Create Fish structure:
Structure Fish
Public ID As String
Public Name As String
Public Type As Integer
Public Age As Integer
End Structure
Codes to process the input string:
Dim Fishes As New Dictionary(Of String, Fish)
Dim m As Match = Regex.Match(str, "Fishes\[(?<key>\d+)]\.(?<prop>.+?)=(?<value>[^&]+)", RegexOptions.IgnoreCase)
Do While m.Success
Dim key As String = m.Groups("key").Value.Trim.ToUpper
Dim prop As String = m.Groups("prop").Value.Trim.ToUpper
Dim value As String = m.Groups("value").Value
' if the key not yet exist in the Dictionary, create and add into it.
If Not Fishes.ContainsKey(key) Then
Fishes.Add(key, New Fish)
End If
Dim thisFish As Fish = Fishes(key) ' get the Fish object for this key
' determine the object property to set
Select Case prop
Case "ID" : thisFish.ID = value
Case "NAME" : thisFish.Name = value
Case "TYPE" : thisFish.Type = CInt(value)
Case "AGE" : thisFish.Age = CInt(value)
End Select
Fishes(key) = thisFish ' since the Fish object is declared as Structure,
' update the Dictionary item of key with the modified object.
' If Fish is declared as Class, then this line is useless
m = m.NextMatch()
Loop
Try this ..
Structure Test
Dim ID As String
Dim Name As String
Dim Type As Integer
Dim Age As Integer
End Structure
In your Button click event ..
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Fishes As New List(Of Test)
Dim Fish As New Test
Fish.ID = "GF"
Fish.Name = "Gold Fish"
Fish.Age = 1
Fish.Type = 1
Fishes.Add(Fish)
MsgBox(Fishes(0).Name)
End Sub
this is vb.net converted
Public Partial Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
End If
End Sub
Public Sub MYtest()
Dim ls As New List(Of Fish)()
Dim f As New Fish()
f.ID = 1
f.Name = "My name"
f.Type = "My type"
f.Age = 20
ls.Add(f)
End Sub
End Class
Public Class Fish
Public Property ID() As Integer
Get
Return m_ID
End Get
Set
m_ID = Value
End Set
End Property
Private m_ID As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property Type() As String
Get
Return m_Type
End Get
Set
m_Type = Value
End Set
End Property
Private m_Type As String
Public Property Age() As Integer
Get
Return m_Age
End Get
Set
m_Age = Value
End Set
End Property
Private m_Age As Integer
End Class
In the comments you mentioned that this is via ASP.Net MVC. The ModelBinder will do everything for you.
public ActionResult UpdateFish(IList<Fish> listOfFish)
{
//the ModelBinder will figure out that the user has posted a list of Fish instances.
//do something with listOfFish
return View(listOfFish);
}

ASP.Net Accessing Object Properties in Control Attribute

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

How can I data bind <%# Eval("Object.Property")%> programmatically in VB.NET

I can bind a inner object property to gridview using the following setup at design time in an ASP.NET gridview
<asp:TemplateField HeaderText="ObjectName" >
<ItemTemplate>
<%# Eval("Object.property")%>
</ItemTemplate>
</asp:TemplateField>
but what I would like to do know is create this programmatically at runtime
i.e. define my columns, add them to the gridview and then databind
I'm not sure if this is what you want to achieve. But if you want to create columns dynamically according to your object's properties on runtime, have a look at following code(example is with BoundColumns, have a look at Volpa's answer when you need TemplateColumns):
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"></asp:GridView>
Codebehind:
Public Partial Class GridViewTest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim cList As New List(Of CustomClass)
For i As Int32 = 1 To 10
Dim c As New CustomClass
c.ID = i
c.Name = "Object " & i
cList.Add(c)
Next
Dim model As New CustomClass
For Each prop As Reflection.PropertyInfo In model.GetType.GetProperties
If prop.CanRead Then
Dim field As New BoundField()
field.HeaderText = prop.Name
field.DataField = prop.Name
Me.GridView1.Columns.Add(field)
End If
Next
Me.GridView1.DataSource = cList
Me.GridView1.DataBind()
End Sub
End Class
Public Class CustomClass
Private _id As Int32
Private _name As String
Public Property ID() As Int32
Get
Return _id
End Get
Set(ByVal value As Int32)
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
One way would be to create a class that implements ITemplate interface:
public class PropertyTemplate : ITemplate
{
private string _value = string.Empty;
public PropertyTemplate(string propValue)
{
this._value = propValue;
}
public void InstantiateIn(Control container)
{
container.Controls.Add(new LiteralControl(this._value));
}
}
Then in your code-behind assing the ItemTemplate as following:
myTemplateField.ItemTemplate = new PropertyTemplate(myBusinessObject.MyProperty);
Another way would be to use Page.LoadTemplate if your custom template resides in the separate .ascx file:
myTemplateField.ItemTemplate = Page.LoadTemplate("~/MyTemplate.ascx");
And the .ascx file will look like:
<%# Eval("MyProperty") %>

Design-time trouble with ASP.NET v2.0 Custom Control, with List<T> for child items

Folks,
I am having a devil of a time with a custom control. The control is very simple - it just displays a list of "StepItems" (rendered as table rows), each with an icon. When I first drag it onto a page, and add StepItems to its collection, it renders perfectly. If I provide some text for its Header property, that also renders perfectly.
If I then look at the HTML source view, and then back to the design view, I get an error where my control should be. There are two kinds of errors:
If I set the .Header property, the error reads "StepProgressControl1:'someheadertext' could not be set on property 'Header'.
If I don't set the .Header, but add StepItems to the collection, I get this: "ErrorStepProgressControl1:'StepItems' could not be initialized. Details: Method not found: 'System.Collections.Generic.List`1 StepProgressControl.TKC.Experiment.StepProgressControl.get_StepItems()'."
The complete code for my custom control is below. If you can provide any help, thank you a great deal!
Tom
'================================
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions
Imports System.ComponentModel
Namespace TKC.Experiment
' THIS IS THE INTERNAL "CHILD" ELEMENT
< _
PersistenceMode(PersistenceMode.InnerProperty), _
TypeConverter(GetType(StepItemConverter)) _
> _
Public Class StepItem
Private _name As String
Public Sub New()
Me.New("")
End Sub
Public Sub New(ByVal name As String)
Me._name = name
End Sub
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
'=====================================================================
' THIS IS THE ACTUAL "PARENT" WEB CONTROL
< _
ParseChildren(True, "StepItems"), _
PersistChildren(False) _
> _
Public NotInheritable Class StepProgressControl
Inherits WebControl
Private _header As String = String.Empty
Private _stepItems As New List(Of StepItem)
Public Sub New()
Me.Header = "StepProgressControl"
End Sub
< _
PersistenceMode(PersistenceMode.Attribute) _
> _
Public Property Header() As String
Get
Return _header
End Get
Set(ByVal value As String)
_header = value
End Set
End Property
< _
DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
PersistenceMode(PersistenceMode.InnerProperty) _
> _
Public ReadOnly Property StepItems() As List(Of StepItem)
Get
If _stepItems Is Nothing Then
_stepItems = New List(Of StepItem)
End If
Return _stepItems
End Get
'Set(ByVal value As List(of stepitem))
' _stepItems = value
'End Set
End Property
Public Overrides Sub RenderControl(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.RenderControl(writer)
Dim label As New Label()
label.Text = Header
label.RenderControl(writer)
Dim table As New Table()
Dim htr As New TableRow()
Dim hcell1 As New TableHeaderCell()
hcell1.Text = "Name"
htr.Cells.Add(hcell1)
Dim hcell2 As New TableHeaderCell()
hcell2.Text = "Title"
htr.Cells.Add(hcell2)
table.BorderWidth = Unit.Pixel(0)
Dim stepItem As StepItem
For Each stepItem In StepItems
Dim tr As New TableRow()
Dim cell1 As New TableCell()
Dim img As New HtmlImage
img.Src = ""
img.Alt = ""
cell1.Controls.Add(img)
tr.Cells.Add(cell1)
Dim cell2 As New TableCell()
cell2.Text = stepItem.Name
tr.Cells.Add(cell2)
table.Rows.Add(tr)
Next stepItem
table.RenderControl(writer)
End Sub
End Class
'========================================
'THIS IS A "TYPE CONVERTER" - JUST A COSMETIC THING, NOT CAUSING TROUBLE...
Public Class StepItemConverter
Inherits TypeConverter
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
Dim obj As StepItem = DirectCast(value, StepItem)
Return obj.Name
End Function
End Class
End Namespace
You will want to implement your own Collection object to represent the list - otherwise the designer will not display it properly.
See the ICollection, IEnumerable, etc. interfaces.

Resources