Skip to main content

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(Arg)
            Case Else

‘ other buttons..

        End Select

    Catch ex As Exception
        
    End Try

Comments

Popular posts from this blog

Siemens Washing Machine : Kill the Buzzer

The washing machine we have has the habit of sending out an irritating beep after it finishes the wash cycle. Ok enough; but these intermittent beeps go on and on and on till it is sure everyone came back home after your attending your funeral. One awful engineer who programmed the chip. Anyway, been looking around the net if there is a work around. Found this written by some Russian. I didn’t have patience to correct the grammar entirely. If the below does  not work, there is always the option of an Axe and ‘hey Siemens,..Heeeere is Johnyyy!) -- text You can change the volume of the buzzer according to your requirement. The operation procedure: 1. Switch on the machine,turn the program selector to Off . 2. Turn the program selector to cold Easy-care , press the additional function button Intensive stains and dont let go. You can hear the volume of the buzzer from minimum to maximum to off cycled (I didnt hear this!). If you decide the volume that yo...

.Net Serialize and Deserialize Objects to XML and database

Serialization Create an empty instance of your object Dim Obj as New MYOBJECT Dim cn As New SqlConnection(ConnectionString.Text_) Dim cmd As New SqlCommand Dim Trans As SqlTransaction   With cmd                 .Connection = cn                 .Transaction = Trans                 .CommandType = CommandType.StoredProcedure                 .CommandText = "somestoredprocedure"             Dim xMLSerialiser_ As New System.Xml.Serialization.XmlSerializer ( Obj.GetType )                 Dim sWriter As New System.IO.StringWrit...

SQL Server - Get columns and data types from all tables

Useful script USE SmartHire GO  SELECT TABLE_NAME, COLUMN_NAME, COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'ColumnID') AS COLUMN_ID, DATA_TYPE  FROM SmartHire.INFORMATION_SCHEMA.COLUMNS  GO   Output TABLE_NAME COLUMN_NAME COLUMN_ID DATA_TYPE TblBankDetails BankID 1 int TblBankDetails BankName 2 varchar TblContactPreferences ContactPreferenceID 1 int