Mam taký to problém: po neúspešnom inserte teda chybe vratenej z DB ošetrujem výnimku v UI vrstve na stranke v udalosti ObjectDataSource_Inserted pričom stále nastava databind na formview. Problem je, že všetky zadane udaje sú odstránené a užívateľ musí znova vyplniť formular ako sa tomu vyhnuť ? Dátový zdroj je ObjectDataSource.
Namespace Admin.Roles.Modules
Partial Public Class RoleDetail
Inherits PS_Soft.Portal.Web.Web_Controls.BaseWebControl
'nastavenie FormView do insert modu ak v QueryString chýba parameter "Id"
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Init
If Not IsPostBack Then
If Request.QueryString("Id") Is Nothing Then
Me.FormView1.DefaultMode = WebControls.FormViewMode.Insert
ElseIf Request.QueryString("Id") = "0" Then
Me.FormView1.DefaultMode = WebControls.FormViewMode.Insert
End If
End If
End Sub
Private Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles FormView1.ItemCreated
Dim _txt As PS.Web.UI.WebControls.TextBox = TryCast(Me.FormView1.FindControl("txtRoleName"), PS.Web.UI.WebControls.TextBox)
If _txt IsNot Nothing Then _txt.Focus()
If FormView1.CurrentMode = FormViewMode.Insert AndAlso Not IsPostBack Then
Dim _txt1 As PS.Web.UI.WebControls.TextBox = TryCast(Me.FormView1.FindControl("txtRightWeight"), PS.Web.UI.WebControls.TextBox)
If _txt1 IsNot Nothing Then _txt1.Text = Integer.MaxValue - 1
End If
End Sub
#Region " Vkladnie a aktualizacia udajov "
Private key As String = ""
Private Sub ODS_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) _
Handles ODS.Inserting
e.Cancel = Not Page.IsValid
key = e.InputParameters("roleName").ToString
e.InputParameters("locCollection") = GetLocalization(0)
End Sub
Private Sub ODS_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) _
Handles ODS.Updating
e.Cancel = Not Page.IsValid
key = e.InputParameters("roleName").ToString
e.InputParameters("locCollection") = GetLocalization(CInt(e.InputParameters("original_RoleId")))
End Sub
Protected Sub ODS_InsertedAndUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) _
Handles ODS.Inserted, ODS.Updated
'kontrola po vlozeni parent zaznamu
If e.Exception IsNot Nothing Then
'kod pre osetrenie chyb pri zapise parent zaznamu
e.ExceptionHandled = True
If TypeOf e.Exception Is DBConcurrencyException Then
Me.ErMes.ErrorMessage = e.Exception.Message
ElseIf e.Exception.InnerException Is Nothing Then
Me.ErMes.ErrorMessage = e.Exception.Message
Else
Me.ErMes.ErrorMessage = e.Exception.InnerException.Message
End If
End If
End Sub
Protected Function GetLocalization(ByVal parentId As Integer) As List(Of PS_Role_Loc)
Dim rp1 As Repeater = CType(Me.FormView1.FindControl("rp1"), Repeater)
If rp1 IsNot Nothing Then
Dim list As New List(Of PS_Role_Loc)
For i As Integer = 0 To rp1.Items.Count - 1
Dim hf As HiddenField = CType(rp1.Items(i).FindControl("CultureId"), HiddenField) 'id cultures v skritom poli
Dim txt1 As PS.Web.UI.WebControls.TextBox = CType(rp1.Items(i).FindControl("txtRoleDescription"), PS.Web.UI.WebControls.TextBox) 'vkladana hodnota pre urcenu kulturu
Try
Dim role As New PS_Role_Loc With {.CultureId = CInt(hf.Value), .Description = If(String.IsNullOrEmpty(txt1.Text.Trim), "", txt1.Text), .RoleId = parentId}
list.Add(role)
Catch ex As Exception
If ex.InnerException Is Nothing Then
Me.ErMes.ErrorMessage = ex.Message
Else
Me.ErMes.ErrorMessage = ex.InnerException.Message
End If
Return Nothing
End Try
Next
If list.Count > 0 Then Return list
End If
Return Nothing
End Function
#End Region
End Class
End Namespace
|