Skip to main content

Posts

Showing posts from 2013

Adding Item to Generic List using Reflection

Say the object pX has a property which is a IList of something public property SomeListProperty as List of (AnotherClass) To add an item to this List, use this technique Dim pInfo As System.Reflection.PropertyInfo            pInfo = pX.GetType().GetProperty(“SomeListProperty")            Dim Obj As Object = pInfo.GetValue(pX, Nothing)            Obj.GetType().GetMethod("Add").Invoke(Obj, New Object() {InstanceofAnotherClass})

ASP.Net–Creating Controls Dynamically and Adding Events to it

Create a form and add a panel to it. Say pnlW In the code behind Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load     Try         Dim tx As New TextBox         tx.Text = "Hello"         pnlW.Controls.Add(tx) ‘textbox added         Dim bx As New Button         bx.ID = "btnX"         bx.Text = "Do something"         bx.ToolTip = "Button does something"         bx.CssClass = "somecssclass"         bx. CommandArgument = "somethingunique" ‘key lines         AddHandler bx.Command, AddressOf btnX_Click      pnlW.Controls.Add(bx)     Catch ex As Exception     End Try End Sub Protected Sub btnX_Click(ByVal sender As System.Object, ByVal e As CommandEventArgs )     Try         Dim Arg As String = e.CommandArgument.ToString         Select Case Arg             Case Is = "somethingunique"                 Response.Write