Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all

Here is my scenario:

1. I create a dynamic table.
2. Inside that table, I create dynamic panel
3. Inside the panel, there is a dynamic checkbox.

I want to add attributes for that checkbox.
Below is my code:

For Each drCab As DataRow In dt.Rows
           Dim TempPanelrack As New Panel
           Dim BtnCab As New Button
           Dim chkcab As New CheckBox
           TempPanelrack.HorizontalAlign = HorizontalAlign.Center
           TempPanelrack.BorderStyle = BorderStyle.Inset
           TempPanelrack.Width = 110
           TempPanelrack.Height = 80
           TempPanelrack.ID = drCab("REFNO_T")

           BtnCab.Text = drCab("REFNO_T") & vbCrLf & "(" & CStr(drCab("NAME_T")).Substring(0, 12) & ")" & vbCrLf & "-" & CDate(drCab("FRDATETIME_D")).ToString("HH:mm") & "-"
           BtnCab.BorderStyle = BorderStyle.Groove
           BtnCab.ToolTip = drCab("NAME_T") & vbCrLf & "From: " & CDate(drCab("FRDATETIME_D")).ToString("dd/MM/yyyy HH:mm:ss") & vbCrLf & " To: " & CDate(drCab("TODATETIME_D")).ToString("dd/MM/yyyy HH:mm:ss") & " "
           BtnCab.Attributes.Add("onclick", "Showdetails('" & drCab("REGISNO_T") & "');")


           BtnCab.Width = 110
           BtnCab.Height = 80
           BtnCab.Font.Size = 8
           BtnCab.Font.Bold = True

           chkcab.ToolTip = drCab.Item("REFNO_T")
           chkcab.BorderStyle = BorderStyle.None
           chkcab.ID = drCab.Item("REFNO_T")

           chkcab.Attributes.Add("onClick", "chkcabchecked('" & TempPanelrack.ID & "');")


           TempPanelrack.Controls.Add(BtnCab)
           TempPanelrack.Controls.Add(chkcab)

           Dim TC As New TableCell()
           TC.Controls.Add(TempPanelrack)
           TR.Cells.Add(TC)

           If CountCab Mod 4 = 0 Then
               Me.tableALL.Rows.Add(TR)
               TR = New TableRow()
           Else
               Me.tableALL.Rows.Add(TR)
           End If


VB
Private Sub chkcabchecked(ByVal panelid As String)
      'Dim chkbox As CheckBox = CType(sender, CheckBox)
      'MsgBox(sender.ID)

      Dim mainContent As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
      'Dim chkcontrol As Panel = DirectCast(mainContent.FindControl("Panel1"), Panel)
      Dim tr As Table = DirectCast(mainContent.FindControl("tableALL"), Table)
      Dim trpanels As Panel = DirectCast(tr.FindControl(panelid), Panel)
      Dim chkbox As CheckBox = DirectCast(trpanels.FindControl("chkcab"), CheckBox)
      If (Not chkbox Is Nothing) Then
          ' Get control's parent.
          'Dim chkcontrol2 As Control = chkcontrol.Parent
          MsgBox("Parent of the text box is : " & chkbox.ID)
      Else
          MsgBox("Control not found.....")
      End If



  End Sub


However, the error
that I get when I click on the checkbox is:
Microsoft JScript runtime error: 'chkcabchecked' is undefined
Posted

1 solution

Well, your first issue is that you appear to be using ASP.NET and did not label your question properly.

Secondly:

chkcab.Attributes.Add("onClick", "chkcabchecked('" & TempPanelrack.ID & "');")

I don't know what you thnk this does, but it assumes there's a javascript method called chkcabchecked. I see you have a VB.NET method called that, which tells me you have not got the foggiest notion of what you're doing. You desperately need to read some books and understand what ASP.NET is. Your VB.NET code runs when a page is being generated, but the browser has no idea it exists. The only way to call your code behind from the browser is to do AJAX. You should not worry about AJAX for now. You're clearly a beginner, so I assume no-one is going to use this code for anything, there is no level on which it would be moral for anyone to pay you to write code right now. Buy some books and work through them, take a class perhaps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900