redzoneglobal
asked on
UpdatePanel with Dynamic Controls Inside and External to UpdatePanel
I have a ASPX page that reads data from a database, and creates a table dynamically, and adds it to an update panel. I also have other controls on the page that trigger the update panel data to update. My problem is that I can get the controls in the table (which is in the updatepanel) to work, or the controls outside of the update panel to work, butnot both at the same time.
I have cobbled together some code that recreates the problem.
Basically, I have a CheckBoxList ousdie of the UpdatePanel. When you check/uncheck a box, it displays/hides columns in the table  in the update panel. This only works when you comment out the doPostBackPageLoad() subroutine in Page_Load.
I also have ImageButtons at the top of each column in the table in the UpdatePanel to sort the table data ascending or descending. (Note that the sort function isn';t working here, but works in my original code). If I leave doPostBackPageLoad() commented out, the table disappears when I click on any of the sort buttons.
If I uncomment the doPostBackPageLoad() subroutine call, and refresh the page, the sort buttons work, but the CheckBoxList generates an error.
Any help would be greatly appreciated.
This is the aspx page (test_ppc_creative.aspx):
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="test_ppc_creativ e.aspx.vb" Inherits="ppc_creative" %>
<%@ Register Assembly="System.Web.Exten sions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad3 64e35"
  Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:CheckBoxList ID="frmCheckboxCurrentView " runat="server" AutoPostBack="True">
    </asp:CheckBoxList><br />
    <br />
  <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
  <asp:UpdatePanel ID="UpdatePanelCreatives" runat="server" UpdateMode="Conditional">
          <ContentTemplate>
            <span class="heading_text"></spa n>
            <table cellpadding="0" cellspacing="0" style="background: #cccccc; width: 100%">
              <tr>
                <td style="width: 33%; height: 19px;" align="left">
        </td>
                <td style="width: 33%; height: 19px;" align="center">
        </td>
                <td style="width: 33%; height: 19px;" align="right">
                  </td>
              </tr>
            </table>
            <asp:TextBox ID="frmFields" runat="server" BorderStyle="None" Columns="30">Name</asp:Tex tBox><asp: TextBox
              ID="frmFieldLabels" runat="server" BorderStyle="None" Columns="30">Name</asp:Tex tBox>
            <asp:TextBox ID="frmSortField" runat="server" Columns="30"></asp:TextBox >
            <asp:TextBox ID="frmSortDirection" runat="server" Columns="30"></asp:TextBox ><br />
            <br />
            <br />
          </ContentTemplate>
          <Triggers>
          <asp:AsyncPostBackTrigger ControlID="frmCheckboxCurr entView" EventName="SelectedIndexCh anged" />
          </Triggers>
        </asp:UpdatePanel>
      <br />
  </form>
</body>
</html>
This is the code behind file (test_ppc_creative.aspx.vb ):
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Web.HttpRequest
Imports System.Web.HttpContext
Imports System.Collections.Generic
Partial Class ppc_creative
  Inherits System.Web.UI.Page
  Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
    '-----
    ' take actions based on whether there has been a postback
    ' or this is the initial page view
    '-----
    If Not (Page.IsPostBack) Then
      doInitialPageLoad()
    Else
      'doPostBackPageLoad()
    End If
  End Sub
  Protected Sub doInitialPageLoad()
    ' build checkboxlist
    frmCheckboxCurrentView.Ite ms.Add(New ListItem("Name", "Name"))
    frmCheckboxCurrentView.Ite ms.Add(New ListItem("Age", "Age"))
    frmCheckboxCurrentView.Ite ms.Add(New ListItem("ZipCode", "ZipCode"))
    ' check all checkbox items
    frmCheckboxCurrentView.Ite ms.Item(0) .Selected = True
    frmCheckboxCurrentView.Ite ms.Item(1) .Selected = True
    frmCheckboxCurrentView.Ite ms.Item(2) .Selected = True
    ' set default fields to show
    frmFields.Text = "Name,Age,ZipCode"
    frmFieldLabels.Text = "Name,Age,ZipCode"
    ' fill panel
    ' pass: column names, column titles/headings, sortfield, sortorder
    FillPanel(frmFields.Text, frmFieldLabels.Text)
  End Sub
  Protected Sub doPostBackPageLoad()
    ' fill panel
    FillPanel(frmFields.Text, frmFieldLabels.Text)
  End Sub
  Protected Sub frmCheckboxCurrentView_Sel ectedIndex Changed(By Val sender As Object, ByVal e As System.EventArgs) Handles frmCheckboxCurrentView.Sel ectedIndex Changed
    ' get new columns to display
    Dim myListOfTableColumns As String = String.Empty
    Dim myListOfColumnHeadings As String = String.Empty
    ' find checkboxes that are checked
    Dim myItem As ListItem
    For Each myItem In frmCheckboxCurrentView.Ite ms
      If (myItem.Selected = True) Then
        If Not (myItem.Value.ToString = "") Then
          myListOfTableColumns &= myItem.Value.ToString & ","
          myListOfColumnHeadings &= myItem.Text & ","
        End If
      End If
    Next
    ' chop off trailing space and comma
    If (myListOfTableColumns.Leng th > 1) Then
      myListOfTableColumns = Left(myListOfTableColumns, myListOfTableColumns.Lengt h - 1)
      myListOfColumnHeadings = Left(myListOfColumnHeading s, myListOfColumnHeadings.Len gth - 1)
    End If
    ' if no column headings specified, add default heading
    If String.IsNullOrEmpty(myLis tOfColumnH eadings) Then
      myListOfTableColumns = "Name"
      myListOfColumnHeadings = "Name"
    End If
    ' save data to text fields
    frmFields.Text = myListOfTableColumns
    frmFieldLabels.Text = myListOfColumnHeadings
    ' fill panel
    FillPanel(myListOfTableCol umns, myListOfColumnHeadings)
  End Sub
  '-----
  ' fill panel 1 with data based on data submitted in form fields
  '-----
  Public Sub FillPanel(ByVal myDBFieldNames As String, ByVal myColumnNames As String)
    ' get default sort field and direction
    Dim mySortField As String = "Name"
    Dim mySortDirection As String = "ASC"
    ' overwrite if user has specified another sort
    If Not (frmSortField.Text = "") Then
      mySortField = frmSortField.Text
    End If
    If Not (frmSortDirection.Text = "") Then
      mySortDirection = frmSortDirection.Text
    End If
    ' begin table
    Dim myTable As Table = New Table
    myTable.ID = "UpdatePanelTable"
    myTable.CellPadding = "3"
    myTable.CellSpacing = "1"
    ' get data
    Dim myDataTable As DataTable = New DataTable
    myDataTable = GetDataTable()
    Dim myDSEmployees As DataSet = New DataSet
    myDSEmployees.Tables.Add(m yDataTable )
    ' put in dataview to sort
    Dim myDVEmployees As DataView = New DataView
    myDVEmployees = myDSEmployees.Tables(0).De faultView
    myDVEmployees.Sort = mySortField & " " & mySortDirection
    ' create table header
    ' create table sort buttons on each column
    Dim arDBFieldNames As Array = Split(myDBFieldNames, ",")
    Dim arColumnNames As Array = Split(myColumnNames, ",")
    Dim myTableHeaderRow As TableRow = New TableRow
    myTableHeaderRow.Attribute s.Add("bgc olor", "#5D7B9D")
    Dim i, j As Integer
    For i = 0 To arDBFieldNames.Length - 1
      Dim myHeaderRowCell As TableCell = New TableCell
      myHeaderRowCell.Wrap = False
      Dim myHeaderCellText As Label = New Label
      Dim mySortButtonUp As ImageButton = New ImageButton
      mySortButtonUp.ImageUrl = "~/images/btn_sort_up.gif"
      mySortButtonUp.ID = arDBFieldNames(i).ToString & "_DESC"
      mySortButtonUp.Attributes. Add("runat ", "server")
      AddHandler mySortButtonUp.Click, AddressOf CreativesSortButton_Click
      'ScriptManager1.RegisterAs yncPostBac kControl(m ySortButto nUp)
      'Dim myTrigger1 As PostBackTrigger = New PostBackTrigger
      'myTrigger1.ControlID = mySortButtonUp.UniqueID
      'UpdatePanelCreatives.Trig gers.Add(m yTrigger1)
      Dim mySortButtonDown As ImageButton = New ImageButton
      mySortButtonDown.ImageUrl = "~/images/btn_sort_down.gi f"
      mySortButtonDown.ID = arDBFieldNames(i).ToString & "_ASC"
      mySortButtonDown.Attribute s.Add("run at", "server")
      AddHandler mySortButtonDown.Click, AddressOf CreativesSortButton_Click
      'ScriptManager1.RegisterAs yncPostBac kControl(m ySortButto nDown)
      'Dim myTrigger2 As PostBackTrigger = New PostBackTrigger
      'myTrigger2.ControlID = mySortButtonUp.UniqueID
      'UpdatePanelCreatives.Trig gers.Add(m yTrigger2)
      myHeaderCellText.Text = arColumnNames(i).ToString & "<br>"
      myHeaderRowCell.Controls.A dd(myHeade rCellText)
      myHeaderRowCell.ForeColor = Drawing.Color.White
      myHeaderRowCell.Font.Bold = True
      myHeaderRowCell.Controls.A dd(mySortB uttonUp)
      myHeaderRowCell.Controls.A dd(mySortB uttonDown)
      myTableHeaderRow.Controls. Add(myHead erRowCell)
    Next
    myTable.Controls.Add(myTab leHeaderRo w)
    ' create table data
    For i = 0 To myDSEmployees.Tables(0).Ro ws.Count - 1
      Dim myRow As TableRow = New TableRow
      For j = 0 To arDBFieldNames.Length - 1
        Dim myCell As TableCell = New TableCell
        myCell.Text = myDSEmployees.Tables(0).Ro ws(i).Item (arDBField Names(j)). ToString
        myRow.Controls.Add(myCell)
      Next
      myTable.Controls.Add(myRow )
    Next
    ' create panel to hold table
    Dim myPanelCreatives As Panel = New Panel
    myPanelCreatives.Width = 950
    myPanelCreatives.ScrollBar s = ScrollBars.Horizontal
    UpdatePanelCreatives.Conte ntTemplate Container. Controls.A dd(myPanel Creatives)
    ' add table to panel
    myPanelCreatives.Controls. Add(myTabl e)
  End Sub
  Protected Sub CreativesSortButton_Click( ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEv entArgs)
    Dim myData As String = String.Empty
    myData = sender.id.ToString
    Dim arData As Array = Split(myData, "_")
    frmSortField.Text = arData(0)
    frmSortDirection.Text = arData(1)
    ' fill panel
    ' FillPanel(frmFields.Text, frmFieldLabels.Text)
    UpdatePanelCreatives.Updat e()
  End Sub
  '-----
  ' creates data table
  ' normally done by call to database
  '-----
  Protected Function GetDataTable() As DataTable
    'creating table
    Dim myDataTable As DataTable
    myDataTable = New DataTable("Employees")
    ' three rows for the table
    Dim Row1, Row2, Row3 As DataRow
    ' three columns
    Dim Name As DataColumn = New DataColumn("Name")
    Name.DataType = System.Type.GetType("Syste m.String")
    myDataTable.Columns.Add(Na me)
    Dim Product As DataColumn = New DataColumn("Age")
    Product.DataType = System.Type.GetType("Syste m.String")
    myDataTable.Columns.Add(Pr oduct)
    Dim Location As DataColumn = New DataColumn("ZipCode")
    Location.DataType = System.Type.GetType("Syste m.String")
    myDataTable.Columns.Add(Lo cation)
    ' row 1
    Row1 = myDataTable.NewRow()
    Row1.Item("Name") = "Andy"
    Row1.Item("Age") = "40"
    Row1.Item("ZipCode") = "33333"
    myDataTable.Rows.Add(Row1)
    ' row 2
    Row2 = myDataTable.NewRow()
    Row2.Item("Name") = "Barry"
    Row2.Item("Age") = "30"
    Row2.Item("ZipCode") = "22222"
    myDataTable.Rows.Add(Row2)
    ' row 3
    Row3 = myDataTable.NewRow()
    Row3.Item("Name") = "Clyde"
    Row3.Item("Age") = "35"
    Row3.Item("ZipCode") = "11111"
    myDataTable.Rows.Add(Row3)
    ' return datatable
    Return myDataTable
  End Function
End Class
I have cobbled together some code that recreates the problem.
Basically, I have a CheckBoxList ousdie of the UpdatePanel. When you check/uncheck a box, it displays/hides columns in the table  in the update panel. This only works when you comment out the doPostBackPageLoad() subroutine in Page_Load.
I also have ImageButtons at the top of each column in the table in the UpdatePanel to sort the table data ascending or descending. (Note that the sort function isn';t working here, but works in my original code). If I leave doPostBackPageLoad() commented out, the table disappears when I click on any of the sort buttons.
If I uncomment the doPostBackPageLoad() subroutine call, and refresh the page, the sort buttons work, but the CheckBoxList generates an error.
Any help would be greatly appreciated.
This is the aspx page (test_ppc_creative.aspx):
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="test_ppc_creativ
<%@ Register Assembly="System.Web.Exten
  Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:CheckBoxList ID="frmCheckboxCurrentView
    </asp:CheckBoxList><br />
    <br />
  <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
  <asp:UpdatePanel ID="UpdatePanelCreatives" runat="server" UpdateMode="Conditional">
          <ContentTemplate>
            <span class="heading_text"></spa
            <table cellpadding="0" cellspacing="0" style="background: #cccccc; width: 100%">
              <tr>
                <td style="width: 33%; height: 19px;" align="left">
        </td>
                <td style="width: 33%; height: 19px;" align="center">
        </td>
                <td style="width: 33%; height: 19px;" align="right">
                  </td>
              </tr>
            </table>
            <asp:TextBox ID="frmFields" runat="server" BorderStyle="None" Columns="30">Name</asp:Tex
              ID="frmFieldLabels" runat="server" BorderStyle="None" Columns="30">Name</asp:Tex
            <asp:TextBox ID="frmSortField" runat="server" Columns="30"></asp:TextBox
            <asp:TextBox ID="frmSortDirection" runat="server" Columns="30"></asp:TextBox
            <br />
            <br />
          </ContentTemplate>
          <Triggers>
          <asp:AsyncPostBackTrigger ControlID="frmCheckboxCurr
          </Triggers>
        </asp:UpdatePanel>
      <br />
  </form>
</body>
</html>
This is the code behind file (test_ppc_creative.aspx.vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Web.HttpRequest
Imports System.Web.HttpContext
Imports System.Collections.Generic
Partial Class ppc_creative
  Inherits System.Web.UI.Page
  Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
    '-----
    ' take actions based on whether there has been a postback
    ' or this is the initial page view
    '-----
    If Not (Page.IsPostBack) Then
      doInitialPageLoad()
    Else
      'doPostBackPageLoad()
    End If
  End Sub
  Protected Sub doInitialPageLoad()
    ' build checkboxlist
    frmCheckboxCurrentView.Ite
    frmCheckboxCurrentView.Ite
    frmCheckboxCurrentView.Ite
    ' check all checkbox items
    frmCheckboxCurrentView.Ite
    frmCheckboxCurrentView.Ite
    frmCheckboxCurrentView.Ite
    ' set default fields to show
    frmFields.Text = "Name,Age,ZipCode"
    frmFieldLabels.Text = "Name,Age,ZipCode"
    ' fill panel
    ' pass: column names, column titles/headings, sortfield, sortorder
    FillPanel(frmFields.Text, frmFieldLabels.Text)
  End Sub
  Protected Sub doPostBackPageLoad()
    ' fill panel
    FillPanel(frmFields.Text, frmFieldLabels.Text)
  End Sub
  Protected Sub frmCheckboxCurrentView_Sel
    ' get new columns to display
    Dim myListOfTableColumns As String = String.Empty
    Dim myListOfColumnHeadings As String = String.Empty
    ' find checkboxes that are checked
    Dim myItem As ListItem
    For Each myItem In frmCheckboxCurrentView.Ite
      If (myItem.Selected = True) Then
        If Not (myItem.Value.ToString = "") Then
          myListOfTableColumns &= myItem.Value.ToString & ","
          myListOfColumnHeadings &= myItem.Text & ","
        End If
      End If
    Next
    ' chop off trailing space and comma
    If (myListOfTableColumns.Leng
      myListOfTableColumns = Left(myListOfTableColumns,
      myListOfColumnHeadings = Left(myListOfColumnHeading
    End If
    ' if no column headings specified, add default heading
    If String.IsNullOrEmpty(myLis
      myListOfTableColumns = "Name"
      myListOfColumnHeadings = "Name"
    End If
    ' save data to text fields
    frmFields.Text = myListOfTableColumns
    frmFieldLabels.Text = myListOfColumnHeadings
    ' fill panel
    FillPanel(myListOfTableCol
  End Sub
  '-----
  ' fill panel 1 with data based on data submitted in form fields
  '-----
  Public Sub FillPanel(ByVal myDBFieldNames As String, ByVal myColumnNames As String)
    ' get default sort field and direction
    Dim mySortField As String = "Name"
    Dim mySortDirection As String = "ASC"
    ' overwrite if user has specified another sort
    If Not (frmSortField.Text = "") Then
      mySortField = frmSortField.Text
    End If
    If Not (frmSortDirection.Text = "") Then
      mySortDirection = frmSortDirection.Text
    End If
    ' begin table
    Dim myTable As Table = New Table
    myTable.ID = "UpdatePanelTable"
    myTable.CellPadding = "3"
    myTable.CellSpacing = "1"
    ' get data
    Dim myDataTable As DataTable = New DataTable
    myDataTable = GetDataTable()
    Dim myDSEmployees As DataSet = New DataSet
    myDSEmployees.Tables.Add(m
    ' put in dataview to sort
    Dim myDVEmployees As DataView = New DataView
    myDVEmployees = myDSEmployees.Tables(0).De
    myDVEmployees.Sort = mySortField & " " & mySortDirection
    ' create table header
    ' create table sort buttons on each column
    Dim arDBFieldNames As Array = Split(myDBFieldNames, ",")
    Dim arColumnNames As Array = Split(myColumnNames, ",")
    Dim myTableHeaderRow As TableRow = New TableRow
    myTableHeaderRow.Attribute
    Dim i, j As Integer
    For i = 0 To arDBFieldNames.Length - 1
      Dim myHeaderRowCell As TableCell = New TableCell
      myHeaderRowCell.Wrap = False
      Dim myHeaderCellText As Label = New Label
      Dim mySortButtonUp As ImageButton = New ImageButton
      mySortButtonUp.ImageUrl = "~/images/btn_sort_up.gif"
      mySortButtonUp.ID = arDBFieldNames(i).ToString
      mySortButtonUp.Attributes.
      AddHandler mySortButtonUp.Click, AddressOf CreativesSortButton_Click
      'ScriptManager1.RegisterAs
      'Dim myTrigger1 As PostBackTrigger = New PostBackTrigger
      'myTrigger1.ControlID = mySortButtonUp.UniqueID
      'UpdatePanelCreatives.Trig
      Dim mySortButtonDown As ImageButton = New ImageButton
      mySortButtonDown.ImageUrl = "~/images/btn_sort_down.gi
      mySortButtonDown.ID = arDBFieldNames(i).ToString
      mySortButtonDown.Attribute
      AddHandler mySortButtonDown.Click, AddressOf CreativesSortButton_Click
      'ScriptManager1.RegisterAs
      'Dim myTrigger2 As PostBackTrigger = New PostBackTrigger
      'myTrigger2.ControlID = mySortButtonUp.UniqueID
      'UpdatePanelCreatives.Trig
      myHeaderCellText.Text = arColumnNames(i).ToString & "<br>"
      myHeaderRowCell.Controls.A
      myHeaderRowCell.ForeColor = Drawing.Color.White
      myHeaderRowCell.Font.Bold = True
      myHeaderRowCell.Controls.A
      myHeaderRowCell.Controls.A
      myTableHeaderRow.Controls.
    Next
    myTable.Controls.Add(myTab
    ' create table data
    For i = 0 To myDSEmployees.Tables(0).Ro
      Dim myRow As TableRow = New TableRow
      For j = 0 To arDBFieldNames.Length - 1
        Dim myCell As TableCell = New TableCell
        myCell.Text = myDSEmployees.Tables(0).Ro
        myRow.Controls.Add(myCell)
      Next
      myTable.Controls.Add(myRow
    Next
    ' create panel to hold table
    Dim myPanelCreatives As Panel = New Panel
    myPanelCreatives.Width = 950
    myPanelCreatives.ScrollBar
    UpdatePanelCreatives.Conte
    ' add table to panel
    myPanelCreatives.Controls.
  End Sub
  Protected Sub CreativesSortButton_Click(
    Dim myData As String = String.Empty
    myData = sender.id.ToString
    Dim arData As Array = Split(myData, "_")
    frmSortField.Text = arData(0)
    frmSortDirection.Text = arData(1)
    ' fill panel
    ' FillPanel(frmFields.Text, frmFieldLabels.Text)
    UpdatePanelCreatives.Updat
  End Sub
  '-----
  ' creates data table
  ' normally done by call to database
  '-----
  Protected Function GetDataTable() As DataTable
    'creating table
    Dim myDataTable As DataTable
    myDataTable = New DataTable("Employees")
    ' three rows for the table
    Dim Row1, Row2, Row3 As DataRow
    ' three columns
    Dim Name As DataColumn = New DataColumn("Name")
    Name.DataType = System.Type.GetType("Syste
    myDataTable.Columns.Add(Na
    Dim Product As DataColumn = New DataColumn("Age")
    Product.DataType = System.Type.GetType("Syste
    myDataTable.Columns.Add(Pr
    Dim Location As DataColumn = New DataColumn("ZipCode")
    Location.DataType = System.Type.GetType("Syste
    myDataTable.Columns.Add(Lo
    ' row 1
    Row1 = myDataTable.NewRow()
    Row1.Item("Name") = "Andy"
    Row1.Item("Age") = "40"
    Row1.Item("ZipCode") = "33333"
    myDataTable.Rows.Add(Row1)
    ' row 2
    Row2 = myDataTable.NewRow()
    Row2.Item("Name") = "Barry"
    Row2.Item("Age") = "30"
    Row2.Item("ZipCode") = "22222"
    myDataTable.Rows.Add(Row2)
    ' row 3
    Row3 = myDataTable.NewRow()
    Row3.Item("Name") = "Clyde"
    Row3.Item("Age") = "35"
    Row3.Item("ZipCode") = "11111"
    myDataTable.Rows.Add(Row3)
    ' return datatable
    Return myDataTable
  End Function
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
can i get same solution in C# code for same question