Tuesday, May 5, 2015

Dynamic Button Asp.net

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SQLSTR = ""
        SQLSTR = " SELECT  [ID],[MENU] FROM [CMS_MENU]"
        DSC = GetDS(SQLSTR)
      

            count = Convert.ToInt32(DSC.Tables(0).Rows.Count)

            For i As Integer = 0 To count - 1
            'specifying the condition in for loop for creating no. of textboxes
            'Create the object of TexBox Class
            bt1.ID = ""
            Dim bt As New Button()
            bt.ID = DSC.Tables(0).Rows(i)("ID").ToString
            bt.Text = DSC.Tables(0).Rows(i)("MENU").ToString
            bt.CssClass = "submit1"
            bt1.ID = bt.ID
            AddHandler bt.Click, AddressOf info
            Form.Controls.Add(bt)

            Next
    
    End Sub

    Public Function info(ByVal sender As Object, ByVal e As EventArgs)
        Dim bttoclicked As Button = TryCast(sender, Button)
        If bttoclicked IsNot Nothing Then
            menu = bttoclicked.Text
            If menu = "RENT" Then
                Response.Redirect("Child_Main.aspx")
            ElseIf menu = "LEASE" Then
                Response.Redirect("Child2_Main.aspx")
            End If
        End If
        Return menu
    End Function