我一直在努力让 FormViews 以 Microsoft 期望我的方式工作大约一天,并想出了一堆很棒的东西。
我可以在 ObjectDataSource.Inserting 事件处理程序中捕获 e.Exception 和 e.ReturnValue ,我什至可以通过检查 e.ObjectInstance 来欺骗和检查 ObjectDataSource.ObjectDisposing 中对象的其他属性......我什至了解到 FormView 的 Inserting处理程序在 ObjectDisposing 处理程序之后运行,因此如果发现问题,我仍有时间对其使用react,并将 FormView 上的 e.KeepInInsertMode 设置为 true。
我的问题是,用户在插入表单中输入的值似乎都被清除了。
那么,如何防止 FormView 在其 Insert 方法触发后清除?
(使用 ASP.NET + VB)
我不认为在这里发布我的代码真的会有什么好处,我将不得不修改它以删除 secret 的业务逻辑内容......所以我现在将跳过它。
编辑:
我找到了一个临时且公认的非常笨拙的解决方案(以防没有人找到解决问题的真正解决方案)。
我有一个页面变量定义为:
Dim eInsertArgs As FormViewInsertedEventArgs
然后我在我的 ItemInserted 处理程序中执行以下操作
If boolInsertErrorOccurred = False Then
e.KeepInInsertMode = True
eInsertArgs = e
Else
eInsertArgs = Nothing
End If
然后在每个控件上,我在控件数据绑定(bind)事件中都有类似的内容:
If IsNothing(eInsertArgs) = False Then
Dim _sender As TextBox = sender
_sender.Text = eInsertArgs.Values("_FieldName")
End If
这样做的效果是,在 ASP.NET 将 FormView 绑定(bind)到默认(空白)模板之后,我将值设置回提交的值。
请帮我找到一个不那么糟糕的解决方案。 :)
最佳答案
您需要创建自己的服务器控件,该控件继承自 FormView 控件。
Public Class MyFormView
Inherits FormView
Protected Overrides Sub OnDataSourceViewChanged(ByVal sender As Object,
ByVal e As EventArgs)
If (MyBase.CurrentMode = FormViewMode.Insert) Then
MyBase.RequiresDataBinding = False
Else
MyBase.OnDataSourceViewChanged(sender, e)
End If
End Sub
End Class
请看一下这个页面:http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net/76885/FormView-Insert
关于asp.net - Insert 方法触发后,如何防止 FormView 清除用户输入的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2342940/