2015-12-18 30 views
0

我有一個帶有gridview的網頁。我點擊編輯按鈕並以編程方式填寫2個字段,Reviewdate(當前日期)和用戶(當前用戶)。當我單擊更新按鈕時,這兩個字段中的數據不保存在SQL表中。無法在asp.net gridview中保存編輯的值

<asp:TemplateField HeaderText="ReviewDate" SortExpression="ReviewDate" > 
<EditItemTemplate> 
<asp:TextBox ID="TextBox1" runat="server" dataformatstring="{0:d}" Text='<% # GetDate()%>'></asp:TextBox> 
</EditItemTemplate> 
<ItemTemplate> 
<asp:Label ID="Label1" runat="server" dataformatstring="{0:d}" Text='<%# Bind("ReviewDate")%>'></asp:Label> 
</ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Reviewer" SortExpression="Reviewer"> 
<EditItemTemplate> 
<asp:TextBox ID="TextBox2" runat="server" Text='<%# GetUser)% '> 
</asp:TextBox> 
</EditItemTemplate> 
<ItemTemplate> 
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Reviewer") %>'></asp:Label> 
</ItemTemplate> 
<asp:TemplateField> 

我嘗試使用RowDataBound事件,但沒有成功

Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound 
If e.Row.RowType = DataControlRowType.DataRow Then 
    If e.Row.RowState = DataControlRowState.Edit Then 
     Dim txtreviewDate As TextBox = DirectCast(e.Row.FindControl("ReviewDate"), TextBox) 
     txtreviewDate.Text = GetDate() 
     Dim txtUser As TextBox = DirectCast(e.Row.FindControl("Reviewer"), TextBox) 
     txtUser.Text = GetUser() 
    End If 

結束如果 結束小組}

我再次使用RowUpdating事件,但沒有成功也試過。這時,控制找不到(空異常錯誤。

Private Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating 
Try 
Dim con As New SqlConnection(ConnectionString) 
Dim row As GridViewRow = DirectCast(GridView1.Rows(e.RowIndex), GridViewRow) 
Dim id As TextBox = DirectCast(row.FindControl("SMasterID"), TextBox) 'Int32.Parse(GridView1.DataKeys(e.RowIndex).Value.ToString()) 
Dim tdate As TextBox = DirectCast(row.FindControl("ReviewDate"), TextBox) 
Dim tname As TextBox = DirectCast(row.FindControl("Reviewer"), TextBox) 
Dim cmd As New SqlCommand("update SMasterCurrentYear set Reviewer= '" + @reviewer + "',ReviewDate= " + @ReviewDate+ " where SMasterID [email protected]", con) 
GridView1.EditIndex = -1 
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id 
cmd.Parameters.Add("@reviewer", SqlDbType.VarChar, 30).Value = tname.Text  cmd.Parameters.Add("@ReviewDate", SqlDbType.Date).Value = tdate.Text 
con.Open() 
cmd.ExecuteNonQuery() 
con.Close() 
bind() 
Catch ex As Exception 
    Throw New Exception(ex.Message) 
End Try 
End Sub 

我不知道在這一點上做的事情。 謝謝您的幫助。

回答

0

你檢查Page.IsPostback上頁面加載事件時綁定gridview?