How to get a reference to Ajax

elmowatson

Member
Joined
Jun 5, 2007
Messages
17
Programming Experience
1-3
I am using a PropertyGrid control. There are two things I need to be able to do.
1. I need to be able to access Ajax controltoolkit components and populate the propertygrid with the properties from the Ajax control toolkit.
2. I need to be able to do the same with WebUI components.

The basic part, populating the propertygrid with a control, I understand how to do - -

What I don't know is what to import or how to access what dll (for the Ajax Control Toolkit and the Web UI controls), in my Winforms project to be able to do this.

Can anyone give me a little help here, please?
 
You can't use web controls in winforms projects.
 
I don't really want to use them in the project itself - I just want to access the dlls and get the properties - -
Technically VS.Net does it, so there must be some way, I would think
 
I see, you have to use the 'Add Reference' dialog to add the assembly that has the class you want, documentation for the standard web controls say they are in assembly System.Web. For example for the TextBox class you can see this:
help said:
Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
When you have added the reference you can use the class, either import the namespace or qualify the type like this:
VB.NET:
Me.PropertyGrid1.SelectedObject = New System.Web.UI.WebControls.TextBox
 
Theoretically, anyone using this application will have the DotNet platform on his/her computer.
So, once I've done this - if I compile and package the application - but the AjaxControlToolkit is not installed with DotNet - - Will the end user need to have this dll installed somewhere on his/her computer, or should my reference (and therefore, dll in the Bin and in the installed local app) in the project be enough to be able for the user to use the application?
 
If you reference a assembly your app becomes dependent of it, it must be present in order for your app to run. In deployment scenario it must then be included in setup for local libraries ('copy local' setting), or as a prerequisite (something that must be installed first) for external libraries such as the .Net Framework and the AjaxControlToolkit where these libraries are installed to GAC.

It is also possible to use libraries without referencing them at design time, by loading them dynamically at runtime using Reflection. You can then not write any strongly typed code for these libraries, only discover them with the Reflection type system. The most basic example is this, same as the Textbox example only now System.Web assembly is not referenced:
VB.NET:
Dim asm As Reflection.Assembly = Reflection.Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
Dim o As Object = asm.CreateInstance("System.Web.UI.WebControls.TextBox")
Me.PropertyGrid1.SelectedObject = o
Reflection and the example above has little interest if you have to write the control names manually, so I include a more advanced example that show how control types can be discovered and listed, and later used to create dynamic instances to be assigned the PropertyGrid. This method loads all public types from System.Web that is derived from WebControl and belongs to System.Web.UI.WebControls namespace, and that has a parameterless contructor, much similar to the controls in the web toolbox probably.
VB.NET:
Private Sub LoadWebControls()
    Dim asm As Reflection.Assembly = Reflection.Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    Dim webControlType As Type = asm.GetType("System.Web.UI.WebControls.WebControl")
    Dim controlTypes As New List(Of Type)
    For Each t As Type In asm.GetExportedTypes
        If webControlType.IsAssignableFrom(t) AndAlso t.Namespace = "System.Web.UI.WebControls" Then
            Dim flags As Reflection.BindingFlags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public
            Dim ctor As Reflection.ConstructorInfo = t.GetConstructor(flags, Nothing, Type.EmptyTypes, Nothing)
            If ctor IsNot Nothing Then
                controlTypes.Add(t)
            End If
        End If
    Next
    Me.ListBox1.DataSource = controlTypes
    Me.ListBox1.DisplayMember = "Name"
End Sub
When user selects as type in listbox the existing control instance in PropertyGrid is disposed and the new instance is created:
VB.NET:
Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
    Dim o As Object = Me.PropertyGrid1.SelectedObject
    If o IsNot Nothing Then
        CType(o, IDisposable).Dispose()
    End If
    Dim t As Type = CType(Me.ListBox1.SelectedValue, Type)
    o = Activator.CreateInstance(t)
    Me.PropertyGrid1.SelectedObject = o
End Sub
 
Back
Top