Results 1 to 7 of 7

Thread: datagrids disapear on postback

  1. #1

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    datagrids disapear on postback

    I define my datagrids by vb-code (no AutoGenerateColumns) on the first load of my page (If Not Page.IsPostBack Then...). On the first load everything is fine.

    But once the user interacts (for example clicks on a calendar-object) the datagrids disabear! Only the datagrids disapear, not other elements - event text that was written in a textbox is stored and reloaded when the user does a postback... but not the datagrids.

    Is this normal?

    Thank you very much for your help !

    P.S. By the way, I am still looking for someone who could be so kind and help me with this other datagrid-problem :

    http://www.vbforums.com/showthread.p...17#post2026217

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: datagrids disapear on postback

    What happens when you remove the Postback check?

  3. #3

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Question Re: datagrids disapear on postback

    Hi mendhak,

    what postback check ?
    I can not find any postback check in the datagrid properties!

    But here is an additional information. As I told in my first post, I did create columns by vb-code (see code beneath).

    But, if I create a datagrid and I define the columns in the aspx-page like this it works!

    HTML Code:
    <asp:datagrid id="myDataGrid_lastConsumedProducts" style="Z-INDEX: 123; LEFT: 752px; POSITION: absolute; TOP: 80px" runat="server" Width="168px" AutoGenerateColumns="False" OnItemCommand="viewRow" DataKeyField="product_id">
        <Columns>
             <asp:ButtonColumn DataTextField="fullProductName" CommandName="SelsfdectItem"></asp:ButtonColumn>
        </Columns>
    </asp:datagrid>
    Now this rejoins a bit my other problem:
    http://www.vbforums.com/showthread.p...17#post2026217

    Because like in this other problem about datagrids (specifically concerning buttonColumns) the vb-code seems to make problems. I call this code (see beneath) only once at the first upload of the page (not at postbacks), as the data do not change (and if they change I recall the same function again that I use at the first upload).

    So probably there is a problem in my vb-code (see beneath). But I really need to be able to define the content of my datagrids by vb-code, so if you could help me to understand the problem I would be more than glad!

    Here is the vb-code for defining and filling a datagrid and there are two problems with it : first on postbacks the datagrid disabears and secondly I do not know how to make that the buttonColumn that I create here starts a mySub when it is clicked...


    VB Code:
    1. Function fill_myDataGrid_todayConsumptions(ByVal Datehour_start As Date, ByVal Datehour_end As Date)
    2.         Dim field_list(,) As String = _
    3.         {{"id", "id"}, _
    4.         {"consumptionDate", translate("consumption date", , myUser.language_id)}, _
    5.         {"fullProductName", translate("product name", , myUser.language_id)}, _
    6.         {"weight", translate("weight", , myUser.language_id)}, _
    7.         {"unit", translate("unit", , myUser.language_id)}, _
    8.         {"productCondition", translate("product condition", , myUser.language_id)}}
    9.  
    10.         Dim myDataset As DataSet = myConsumptions.get_Dataset_of_Consumptions(field_list)
    11.         myDataGrid_todayConsumptions.AutoGenerateColumns = False
    12.         myDataGrid_todayConsumptions.DataSource = myDataset
    13.         myDataGrid_todayConsumptions.DataKeyField = "id"
    14.  
    15.         Dim myColumn As BoundColumn
    16.         For i As Integer = 0 To field_list.Length / field_list.Rank - 1
    17.             myColumn = New BoundColumn
    18.             myColumn.DataField = field_list(i, 0)
    19.             myColumn.HeaderText = field_list(i, 1)
    20.             If i = 0 Then myColumn.Visible = False
    21.             myDataGrid_todayConsumptions.Columns.Add(myColumn)
    22.         Next
    23.         myDataGrid_todayConsumptions.HeaderStyle.CssClass = "Lable"
    24.         Dim myButtonColumn As ButtonColumn = New ButtonColumn
    25.         myButtonColumn.Text = translate("Edit", , myUser.language_id)
    26.         myButtonColumn.ItemStyle.CssClass = "datagridEditButton"
    27.         myButtonColumn.CommandName = "Delete"
    28.         myDataGrid_todayConsumptions.Columns.Add(myButtonColumn)
    29.         myDataGrid_todayConsumptions.DataBind()
    30.     End Function

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: datagrids disapear on postback

    Havent read the rest of the reply so not sure if you have solved your problem. But usually if you have the EnableViewState property to false then your DataGrid will dissappear if you are binding it dynamically. So check the EnableViewState property of your datagrid.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Question Re: datagrids disapear on postback

    EnableViewState was checked (true). But by changing this the problem does not solve.

    Do you have any other idea?

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: datagrids disapear on postback

    Postback check = "If Not Page.IsPostBack Then.."

  7. #7

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Question Re: datagrids disapear on postback

    Hi mendhak,

    If I get off the Postback check, the content of the datagrid will be defined at each postback (even if the data do not change), and this would cost time...

    I played a bit around to understand what happens, and I think that I have the answer - could you please tell me what you think about it?

    Normally the "enableViewState" indicates, if the page-object should keep the control's state in memory from one postback to the next, so that data are not lost – right?

    But this seems only to work if the columns of the datagrid are defined in the html-code of the aspx-page … and not if the columns are added by code.

    What do you think about it? Could this be?


    Here my text-code, in this case too the datagrid disappears once the button is clicked!


    VB Code:
    1. Public Class WebForm1
    2.     Inherits System.Web.UI.Page
    3.  
    4. #Region " Web Form Designer Generated Code "
    5.  
    6.     'This call is required by the Web Form Designer.
    7.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    8.  
    9.     End Sub
    10.     Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    11.     Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    12.     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    13.  
    14.     'NOTE: The following placeholder declaration is required by the Web Form Designer.
    15.     'Do not delete or move it.
    16.     Private designerPlaceholderDeclaration As System.Object
    17.  
    18.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    19.         'CODEGEN: This method call is required by the Web Form Designer
    20.         'Do not modify it using the code editor.
    21.         InitializeComponent()
    22.     End Sub
    23.  
    24. #End Region
    25.  
    26.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    27.         If Not Page.IsPostBack Then
    28.             Dim myDBconnectionManagement As New db_connection_management
    29.             myDBconnectionManagement.SetConnection()
    30.  
    31.             Dim myDataset As DataSet = New DataSet
    32.  
    33.             Dim sql As String = "select * from consumption"
    34.             myDBconnectionManagement.fill_dataset(myDataset, sql, "table")
    35.  
    36.             Dim field_list(,) As String = _
    37.             {{"id", "id"}, _
    38.             {"Date", Translate("consumption date")}, _
    39.             {"Product_id", Translate("product name")}, _
    40.             {"weight", Translate("weight")}, _
    41.             {"unit_id", Translate("unit")}, _
    42.             {"product_condition_id", Translate("product condition")}}
    43.  
    44.             Dim myColumn As BoundColumn
    45.             For i As Integer = 0 To field_list.Length / field_list.Rank - 1
    46.                 myColumn = New BoundColumn
    47.                 myColumn.DataField = field_list(i, 0)
    48.                 myColumn.HeaderText = field_list(i, 1)
    49.                 If i = 0 Then myColumn.Visible = False
    50.                 DataGrid1.Columns.Add(myColumn)
    51.             Next
    52.             Dim myButtonColumn As ButtonColumn = New ButtonColumn
    53.             myButtonColumn.Text = Translate("Edit")
    54.             myButtonColumn.ItemStyle.CssClass = "datagridEditButton"
    55.             myButtonColumn.CommandName = "Delete"
    56.             DataGrid1.Columns.Add(myButtonColumn)
    57.  
    58.             DataGrid1.AutoGenerateColumns = False
    59.             DataGrid1.DataSource = myDataset
    60.  
    61.             DataGrid1.DataKeyField = "id"
    62.  
    63.             DataGrid1.HeaderStyle.CssClass = "label"
    64.             DataGrid1.ItemStyle.CssClass = "datagrid"
    65.             DataGrid1.DataBind()
    66.         End If
    67.     End Sub
    68.  
    69.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    70.         TextBox1.Text = "now the datagrid will not show-up anymore!"
    71.     End Sub
    72. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width