How can I data bind <%# Eval("Object.Property")%> programmatically in VB.NET - asp.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") %>

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.

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

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

request querystring in variable

hello iam trying to get the id from a url and send it to the clint side this is what i did
this is my url :
http://localhost:53010/edit.aspx?Id=4
code behind
Public Partial Class Edit
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
End Sub
Private _myId As String = Request.QueryString("id")
Public Property myId() As String
Get
Return _myId
End Get
Set(ByVal value As String)
_myId = value
End Set
End Property
End Class
client
<%= myId%>
error
Request is not available in this context
this is also what i get when i move the private prop to page_load()
"private " is not valid on local variable declaration –
any idea what is going on
Thanks
i solve this problem here is the answer
Public Partial Class Edit
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
MyIdVal = Request.QueryString("id")
End Sub
Private _myIdVal As String
Public Property MyIdVal() As String
Get
Return _myIdVal
End Get
Set(ByVal value As String)
_myIdVal = value
End Set
End Property
End Class
That's a field initializer.
Field initializers run before the constructor and cannot access the instance they're initializing.
Therefore, you can't use the Request property there.
You need to move that to the constructor or Page_Load.
You're accessing the Request too early.
It will work if you set myId on Init, Page_Load or any other similar page event.
Try to set _myId in your PageLoad.
So I wanted a class with properties that were set from querystrings and found this thread. I also wanted to be able to access properties on the front page and even in JavaScript from a single location. Here is what I came up with:
// App_Code/QueryStrings.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for QueryStrings
/// </summary>
public class QS
{
private int id = -1;
public QS()
{
if (HttpContext.Current.Request.QueryString["id"] != null)
try
{
Int32.TryParse(HttpContext.Current.Request.QueryString["id"], out id);
}
catch
{
id = -2;
}
else
id = -3;
}
public int ID
{
get
{
return id;
}
}
}
Then you can call it from your .aspx page as follows:
<body>
<form id="form1" runat="server">
<div>
<% QS qs = new QS(); %>
ID = <%= qs.ID %>
</div>
</form>
</body>
Of course you can call from code behind with the same syntax.

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