2011-12-26 119 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