Link to home
Start Free TrialLog in
Avatar of farjack1
farjack1Flag for United States of America

asked on

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
 
Avatar of HainKurt
HainKurt
Flag of Canada image

we need code... actually you dont need to put data in session, it should be already in viewstate, no need to bind again...
Avatar of joeylu
joeylu

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
Avatar of farjack1

ASKER

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
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
;) 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

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?
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial