Datagrid Postback

Discussion in 'ASP .Net Datagrid Control' started by Bob Trabucco, Jan 22, 2004.

  1. Bob Trabucco

    Bob Trabucco Guest

    Hello all,

    I have a Datagrid on a page. 1 column is a template column with a checkbox
    (unbound). The rest of the columns are databound that are added
    programatically.


    Grid Definition...

    <asp:datagrid id="dgEquipment" tabIndex="50" runat="server"
    Width="664px" BackColor="White" BorderColor="#CCCCCC"
    ToolTip="Equipment at this location" EnableViewState="true"
    PageSize="9999" CellPadding="3"
    AutoGenerateColumns="False" BorderWidth="1px" BorderStyle="None">
    <SelectedItemStyle Font-Size="Smaller" Font-Names="Arial"
    Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
    <EditItemStyle Font-Size="Larger" Font-Names="Arial"></EditItemStyle>
    <AlternatingItemStyle Font-Size="XX-Small"
    Font-Names="Arial"></AlternatingItemStyle>
    <ItemStyle Font-Size="XX-Small" Font-Names="Arial"
    ForeColor="#000066"></ItemStyle>
    <HeaderStyle Font-Size="Smaller" Font-Names="Arial" Font-Bold="True"
    ForeColor="Black" BackColor="#D3F2FE"></HeaderStyle>
    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox runat="server" AutoPostBack="false"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle Font-Size="Smaller" Font-Names="Arial"
    HorizontalAlign="Left" ForeColor="#000066"
    BackColor="White" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>

    Private Sub InitGrid()

    ' Add the columns programmatically since we will eventually let the
    users define which columns are displayed...
    If dgEquipment.Columns.Count <= 1 Then

    Dim col As BoundColumn

    col = New BoundColumn
    col.HeaderText = "Counter"
    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Left
    col.DataField = "Counter"
    col.ItemStyle.Font.Size = System.Web.UI.WebControls.FontUnit.XXSmall
    col.Visible = False
    dgEquipment.Columns.Add(col)

    col = New BoundColumn
    col.HeaderText = "Make"
    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Left
    col.DataField = "MFG"
    col.ItemStyle.Font.Size = System.Web.UI.WebControls.FontUnit.XXSmall
    dgEquipment.Columns.Add(col)

    col = New BoundColumn
    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Left
    col.HeaderText = "Model"
    col.DataField = "MODEL"
    col.ItemStyle.Font.Size = System.Web.UI.WebControls.FontUnit.XXSmall
    dgEquipment.Columns.Add(col)

    col = New BoundColumn
    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Left
    col.HeaderText = "Serial #"
    col.DataField = "SERIAL"
    col.ItemStyle.Font.Size = System.Web.UI.WebControls.FontUnit.XXSmall
    dgEquipment.Columns.Add(col)

    col = New BoundColumn
    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Right
    col.HeaderText = "Install Date"
    col.DataField = "INSTALL"
    col.DataFormatString = "{0:d}"
    col.ItemStyle.Font.Size = System.Web.UI.WebControls.FontUnit.XXSmall
    dgEquipment.Columns.Add(col)

    col = New BoundColumn
    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Right
    col.HeaderText = "Warranty"
    col.DataField = "Warranty"
    col.DataFormatString = "{0:d}"
    col.ItemStyle.Font.Size = System.Web.UI.WebControls.FontUnit.XXSmall
    dgEquipment.Columns.Add(col)

    col = New BoundColumn
    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Right
    col.HeaderText = "Agreement #"
    col.DataField = "SerAgrNo"
    col.ItemStyle.Font.Name = "Arial"
    col.ItemStyle.Font.Size = System.Web.UI.WebControls.FontUnit.XXSmall
    dgEquipment.Columns.Add(col)

    End If
    End Sub

    Private Sub LoadGrid()

    ' Equipment Grid

    Dim equip As New clsEquip
    equip.InitDBConnection(Session("ConnectionString"))
    equip.Load(Session("CustNo"), Session("LocNo"))
    dgEquipment.DataSource = equip

    DataBind()

    equip = Nothing

    End Sub


    With the following the grid displays all the data and the checkbox
    correctly. But when I click any button on the screen any checks I have made
    are cleared so I can't check and see what the user checked....

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    If IsPostBack Then

    ' Do some stuff here

    Else

    ' Do other stuff here

    End If

    InitGrid()
    LoadGrid()

    Exit Sub


    With this one the second time around the grid is empty except for the
    checkbox column...

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load


    If IsPostBack Then

    ' Do some stuff here

    Else

    ' Do other stuff here
    InitGrid()
    LoadGrid()
    End If


    Exit Sub


    I am sure I am just missing something. Thanks in advance!

    Bob
    Bob Trabucco, Jan 22, 2004
    #1
    1. Advertising

  2. Bob Trabucco

    George Guest

    Bob,

    You will probably get MANY responses to this but I will chime in
    also. There is a known challenge with the DataGrid control when
    columns are added programmatically. As I understand it, it has to do
    with when the View State is applied back to grid and the concept the
    programmatically bound columns are not stored in the viewstate and the
    fact that the view state is applied after the bad init but before the
    page load events. The answer I had to implement was to rebuild the
    layout of the datagrid and rebind it in the Init Event of the page.

    I hope this helps.
    George
    George, Jan 27, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Psych971
    Replies:
    5
    Views:
    4,610
    bruce barker
    Dec 17, 2004
  2. =?Utf-8?B?Q2hhZA==?=

    Postback not being set to True on first postback

    =?Utf-8?B?Q2hhZA==?=, Apr 4, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    494
    =?Utf-8?B?Q2hhZA==?=
    Apr 4, 2005
  3. gce
    Replies:
    2
    Views:
    1,709
  4. Jacky Chau
    Replies:
    2
    Views:
    2,045
    Ninjamyst
    Jun 8, 2005
  5. Fabio Mastria
    Replies:
    5
    Views:
    4,345
    TechMCAD13
    Jan 30, 2008
Loading...

Share This Page