2011-12-26 151 views
0

我是使用asp.net的新手,我在更新数据后如何刷新GridView出现问题,但看起来它不适用于其他页面。当我更新供应商信息时,我有相同的代码,然后GridView1.Databind()正在工作,但是当我尝试在我的其他页面上再次使用它时,它不起作用。你能否说出为什么会发生这种情况?如何刷新gridview?

这里是我的代码:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click 
     Dim cmd As New SqlCommand 

     cmd.Connection = cn 
     cmd.CommandText = "UPDATE ProductTable SET ProductCode = ('" & lbl_productcode.Text & "'), ProductName = ('" & txt_prodname.Text & "'),ProductCategory =('" & lbl_category.Text & "'),Price =('" & txt_price.Text & "'), Quantity=('" & txt_qty.Text & "'), CategoryID=('" & lbl_catid.Text & "') WHERE ProductCategory = '" & TextBox1.Text & "'" 
     cmd.Connection.Open() 

     cmd.ExecuteNonQuery() 
     cmd.Connection.Close() 
     MsgBox("RECORD UPDATED", MsgBoxStyle.Information) 
     GridView1.DataBind() 
     Call clear() 
    End Sub 
+0

由于我主要使用*企业Java *和*移动应用程序*,我不是很熟悉asp.net,虽然只是说,因为asp.net支持服务器控件,不应该有需要像你说的那样刷新'GridView',你是什么意思*“在我的其他页面上它不起作用”**?我真的不明白你想说什么,以及你的查询是什么。 – Lion 2011-12-26 15:44:06

+2

Arrgh! [Little Bobby Tables](http://imgs.xkcd.com/comics/exploits_of_a_mom.png)检测到! – 2011-12-26 15:51:47

回答

1

我没有看到你的代码的任何地方,你其实你DataBind()之前设置你的GridViewDataSource。一探究竟!

UPDATE:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click 
    Dim cmd As New SqlCommand 

    cmd.Connection = cn 
    cmd.CommandText = "UPDATE ProductTable SET ProductCode = ('" & lbl_productcode.Text & "'), ProductName = ('" & txt_prodname.Text & "'),ProductCategory =('" & lbl_category.Text & "'),Price =('" & txt_price.Text & "'), Quantity=('" & txt_qty.Text & "'), CategoryID=('" & lbl_catid.Text & "') WHERE ProductCategory = '" & TextBox1.Text & "'" 
    cmd.Connection.Open() 

    Me.GridView1.DataSource = cmd.ExecuteReader() 
    GridView1.DataBind() 

    cmd.Connection.Close() 
    MsgBox("RECORD UPDATED", MsgBoxStyle.Information) 

    Call clear() 
End Sub 

祝你好运!

+0

我的数据源是cn ... cn在我的模块中声明 – Carisle 2011-12-26 16:05:26

+0

@Carisle我已更新我的代码以演示您的代码应该如何。请注意,您使用的ExecuteNonQuery不应该用于此特定情况,因此我使用了ExecuteReader,并将其用作gridview的数据源。祝你好运! – 2011-12-26 17:56:25