我有一个类clsAnswers的列表:
这是那个班级
<Serializable()> Public Class clsAnswer
Public Property Text As String
Public Property AnswerID As Integer
Public Property IsCorrect As Boolean
Public Sub New(Text As String, IsCorrect As Boolean, Id As Integer)
Me.Text = Text
Me.IsCorrect = IsCorrect
Me.AnswerID = Id
End Sub
结束班
我将它绑定到Gridview。 这工作正常,我在网格中看到了我的数据。
我遇到的问题是,当我点击网格中的编辑并输入新值并单击更新时,新值显然已丢失。
这可能是正确保存数据的问题。我不太了解它应该如何在网页中工作。
当代码到达GridView1_RowUpdating时,新值已丢失且旧值为空。
我假设我在做回传错了,这是我的页面加载:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Answers.Add(New clsAnswer("Answer 1", False, 1))
Answers.Add(New clsAnswer("Answer 2", False, 2))
Answers.Add(New clsAnswer("Answer 3", False, 3))
Answers.Add(New clsAnswer("Answer 4", False, 4))
ViewState("Grid") = Answers
GridView1.DataSource = Answers
GridView1.DataKeyNames = {"AnswerID"}
GridView1.DataBind()
Else
Answers = ViewState("Grid")
GridView1.DataSource = Answers
GridView1.DataBind()
End If
我已经尝试了所有我能想到的你能发现我做错了吗?
我已将页面加载修改为:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Answers.Add(New clsAnswer("Answer 1", False, 1))
Answers.Add(New clsAnswer("Answer 2", False, 2))
Answers.Add(New clsAnswer("Answer 3", False, 3))
Answers.Add(New clsAnswer("Answer 4", False, 4))
ViewState("Grid") = Answers
GridView1.DataSource = Answers
GridView1.DataKeyNames = {"AnswerID"}
GridView1.DataBind()
Else
Answers = ViewState("Grid")
End If
End Sub
我将GridView1_RowUpdating as:
Private Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Answers(e.RowIndex).Text = e.NewValues!Text.ToString
Answers(e.RowIndex).IsCorrect = e.NewValues!IsCorrect
ViewState("Grid") = Answers
Answers = ViewState("Grid")
GridView1.EditIndex = -1
bindgrid()
End Sub
网格正确地保存数据,但我发现我必须点击“编辑”#39;并且&#39;更新&#39;两次任何想法为什么
答案 0 :(得分:0)
删除以下代码
Else
Answers = ViewState("Grid")
GridView1.DataSource = Answers
GridView1.DataBind()
原因是您在PostBack上将原始数据重新绑定到GridView,因此您所做的所有更改都会被ViewState("Grid")