• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

GridView problem under not ispostback

i have a grid and its working fine in grid i have Edit and Delete button, its working fine if i put the gridview code in GridView_Load event, if i put my code under
 If Not IsPostBack Then
 End If

then i press the Edit button then page postback and get blank since i am putting the data in session and binding grid again with session variable

Please suggest

Thanks
 
0
farjack1
Asked:
farjack1
  • 5
1 Solution
 
HainKurtSr. System AnalystCommented:
we need code... actually you dont need to put data in session, it should be already in viewstate, no need to bind again...
0
 
joeyluCommented:
yes we need code, however,  

If Not IsPostBack Then
End If

means if the page is not trigger the postback, and if you put this statement in page_load, after you click the EDIT button, since the EDIT button will trigger the postback, therefore every code inside of this IF statement will not run
0
 
farjack1Author Commented:
markup
<asp:GridView ID="testGridView" runat="server" AutoGenerateColumns="False"
            EnableViewState="False">
            <Columns>
                <asp:BoundField DataField="gvOrderID" HeaderText="OrderID" ReadOnly="True"/>
                <asp:TemplateField HeaderText="ProductName">
                    <ItemTemplate>
                        <asp:Label id="labelProductName" runat="server" text='<%# Bind("gvProductName") %>'>></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList id="ddlProductName" runat="server"
                             DataTextField="ProductName" DataValueField="ID" DataSource="<%# loadProducts %>" />                            
                       
                    </EditItemTemplate>                    
                </asp:TemplateField>                
                <asp:BoundField DataField="gvUnitPrice" HeaderText="UnitPrice" ReadOnly="True" SortExpression="UnitPrice" />
                <asp:BoundField DataField="gvQuantity" HeaderText="Quantity" ReadOnly="true" SortExpression="Quantity" />    
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:Button id="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
                        <asp:Button id="btnDelete" runat="server" Text="Deate" CommandName="Delete" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Button id="btnUpdate" runat="server" Text="Update" CommandName="Update"/>
                        <asp:Button id="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false" />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

        If Not IsPostBack Then
            loadProducts()
            loadOrderGridData
        End If

    End Sub
Private Sub loadOrderGridData()

        Dim NewRow As DataRow
        Dim table As DataTable = New DataTable("OrderDetail")
       
        Dim colItemOrderID As DataColumn = New DataColumn("gvOrderID", Type.GetType("System.String"))
        Dim colItemProductID As DataColumn = New DataColumn("gvProductID", Type.GetType("System.String"))
        Dim colItemProductName As DataColumn = New DataColumn("gvProductName", Type.GetType("System.String"))
        Dim colItemUnitPrice As DataColumn = New DataColumn("gvUnitPrice", Type.GetType("System.String"))
        Dim colItemQty As DataColumn = New DataColumn("gvQuantity", Type.GetType("System.String"))

        table.Columns.Add(colItemOrderID)
        table.Columns.Add(colItemProductID)
        table.Columns.Add(colItemProductName)
        table.Columns.Add(colItemUnitPrice)
        table.Columns.Add(colItemQty)

        ordDetailList = objOrderMapper.getOrder()

        For i = 0 To ordDetailList.Count - 1

            NewRow = table.NewRow()

            NewRow("gvOrderID") = CType(ordDetailList.Item(i), Order).OrderID  'testGridView.Rows(i).Cells(0).Text
            'gvLabelProduct = DirectCast(DirectCast(sender, GridView).Rows(i).FindControl("ddlProductName"), Label)
            NewRow("gvProductName") = CType(ordDetailList.Item(i), Order).ProductName 'gvLabelProduct.Text
            NewRow("gvUnitPrice") = CType(ordDetailList.Item(i), Order).Quantity  'testGridView.Rows(i).Cells(2).Text
            NewRow("gvQuantity") = CType(ordDetailList.Item(i), Order).UnitPrice  'testGridView.Rows(i).Cells(3).Text

            table.Rows.Add(NewRow)

        Next

        'Dim dvData As DataView = New DataView(table)
        Session("OrderTable") = table
       
        BindData()


    End Sub


    Private Sub BindData()
        Me.testGridView.DataSource = Session("OrderTable")
        Me.testGridView.DataBind()
    End Sub

 Function loadProducts() As IList
        prdList = objOrderMapper.getProducts()
        Return prdList
    End Function
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
HainKurtSr. System AnalystCommented:
what is this doing? loadProducts is a function and used like a sub...

   Function loadProducts() As IList
        prdList = objOrderMapper.getProducts()
        Return prdList
    End Function

    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
        If Not IsPostBack Then
            loadProducts() >>>>>>>>>>> ????? <<<<<<<<<<<
        End If
    End Sub
0
 
HainKurtSr. System AnalystCommented:
;) got it, you can remove this line

and also why it is form1_Load, should not it be Page_Load?
    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
        If Not IsPostBack Then
            loadOrderGridData()
        End If
    End Sub

Open in new window

0
 
HainKurtSr. System AnalystCommented:
maybe you turned off your viewstate... thats why you get nothing...

do you have anything like this

EnableViewState="false"

in page/form or web config?
0
 
HainKurtSr. System AnalystCommented:
try this instead...
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
  If Not IsPostBack Then Session("OrderTable") = nothing  'set it to null and refresh when on first load
  loadOrderGridData() 
End Sub

Private Sub loadOrderGridData()
  if Session("OrderTable") is nothing then
    ...
    Session("OrderTable") = table
  end if
  BindData()
End Sub

Open in new window

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now