ASP.Net GridView不刷新
问题描述:
我是asp.net编程的新手,所以这一个让我难倒了。我手动创建了一个数据集并将它的值设置为GridView控件的数据源,然后调用Databind方法,但不刷新。我重新创建了一个我正在做的简单版本,所以有人可以告诉我我做错了什么。我没有包括主文件,因为我没有把它看作是有关的。ASP.Net GridView不刷新
代码ASP页
<%@ Page Language="vb" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestGridView._Default" %>
更新价格请袖手旁观。
<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
%>
代码码后面
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
末级
类CRE阿泰的数据集
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
末级
答
您需要添加呼叫才能将定价更新到Page_Load事件中。您也可以删除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
您可以通过在脚本标记包装Page_Load事件中,像这样在.aspx页面做到这一点:
<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>
或者,你可以在Page_Load事件添加到您的代码背后,我认为是可取的,因为你已经有一个页面后面的代码。
答
你为什么不调试你的代码和数据集的检查有数据与否,美国可以通过增加你在哪里设置行一个破发点做到这一点数据源,你可以进入数据集的属性..你会发现一个放大镜图标..点击它,一个弹出窗口将打开显示检索到的数据集。
+0
我已经验证数据是在数据集中作为我的疑难解答的一部分。我只是不明白为什么它不令人耳目一新。 – Kevin 2010-09-17 12:31:49
谢谢你是解决方案。我来自Windows编程的世界,我可以在任何时候更新datagridview,并且它始终工作。你知道为什么你必须从Page_Load事件中调用它来使其工作吗? – Kevin 2010-09-17 12:31:09