Kevin
Kevin

Reputation: 53

ASP.Net GridView doesn't refresh

I am relativity new to asp.net programming, so this one has me stumped. I manually created a dataset and set its value to the Datasource of the GridView control and then call the Databind method, but it isn't refreshing. I recreated a simple version of what I am doing so someone can advise me what I am doing wrong. I didn't include the Master file, as I didn't see it as pertainent.

Code for ASP page

<%@ Page Language="vb" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestGridView._Default" %>

Updating price please stand by.



<p>
    <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="VendorNumber" HeaderText="Vendor" />
        <asp:BoundField DataField="PartNumber" HeaderText="Part or Sku" />
        <asp:BoundField DataField="Message" HeaderText="Error Message" />
    </Columns>
    </asp:GridView>
</p>
<br />
<br />
<%
    If Not Page.IsPostBack Then
        Response.Write(Me.UpdatePricing())

    End If
%>    

Code for Code Behind

Public Class _Default
Inherits System.Web.UI.Page

Public Function UpdatePricing() As String
    Dim showerrors As Boolean = False

    Dim Head As New PnAHead()

    Dim ds As DataSet = Head.GetErrorDataset()

    If (ds.Tables("Errors").Rows.Count > 0) Then
        showerrors = True
    End If

    If showerrors Then
        GV1.DataSource = ds
        GV1.DataBind()
    End If

    Return "Sales Line Number has been updated."
End Function

End Class

Class that creates the Dataset

Public Class PnAHead

Public Function GetErrorDataset() As DataSet
    Dim dstemp = New DataSet()
    Dim tbl As DataTable = dstemp.Tables.Add("Errors")
    Dim col As DataColumn = tbl.Columns.Add("VendorNumber", System.Type.GetType("System.String"))
    col.MaxLength = 20

    col = tbl.Columns.Add("PartNumber", System.Type.GetType("System.String"))
    col.MaxLength = 50

    col = tbl.Columns.Add("Message", System.Type.GetType("System.String"))
    col.MaxLength = 500

    Dim row As DataRow

    row = tbl.NewRow()

    row("VendorNumber") = "Vendor 1"
    row("PartNumber") = "Part Number 1"
    row("Message") = "Message for Part 1"

    tbl.Rows.Add(row)

    row = tbl.NewRow()

    row("VendorNumber") = "Vendor 2"
    row("PartNumber") = "Part Number 2"
    row("Message") = "Message for Part 2"

    tbl.Rows.Add(row)

    Return dstemp
End Function

End Class

Upvotes: 1

Views: 3100

Answers (2)

Phaedrus
Phaedrus

Reputation: 8421

You need to add your call to update pricing into the Page_Load event. You can also remove the Response.Write().

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not Page.IsPostBack Then
      Me.UpdatePricing()
  End If
End Sub

You can do this in the .aspx page by wrapping the Page_Load event in script tags, like this:

<script type="text/VB" runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Me.UpdatePricing()
    End If
End Sub

</script>

Or you can just add the Page_Load event to your code behind, which i think is preferable as you already have a code behind page.

Upvotes: 2

Khaled
Khaled

Reputation: 851

Why don't you debug your code and check of the dataset has data or not,u can do this by adding a break point on the row where you set the data source,you can get into the properties of the dataset..and you will find a magnifier icon..click it,,a pop up window will open showing the set of data retrieved.

Upvotes: 0

Related Questions