/
ivan
/
ModMas
Обзор
Документация
Войти
/
ivan
/
ModMas
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ModuleMaster/usercontrols/grideditcontrol.ascx.vb
474 строки
16 KB
i.krivokon
Initial commit
13 мар 2026, 13:49
13 мар 2026, 13:49
00d9fb6
Код
Авторство
О чём код?
Imports System.Data.SqlClient Imports sharedClasses.dbFunctions Partial Class UserControls_gridEditControl Inherits System.Web.UI.UserControl Public IsReadOnly As Boolean = False Public Property Type As String Set(value As String) sp.Attributes.Add("type", value.ToUpper) If value = "CHECKBOX" Then lbl.Visible = False chk.Checked = (lbl.Text = "Y") chkshow.Visible = True If chk.Checked Then chkshow.ImageUrl = "~/Images/checked.png" Else chkshow.ImageUrl = "~/Images/unchecked.png" End If ElseIf value = "MARK" Then lbl.Visible = False ddlMark.Visible = False chkshow.Visible = True If lbl.Text <> " " Then chkshow.ImageUrl = "~/Images/checked.png" Else chkshow.ImageUrl = "~/Images/unchecked.png" End If ElseIf value = "PHOTO" Then lbl.Visible = False chk.Visible = False ElseIf value = "URL" Then lbl.Visible = False chk.Visible = False hl.Visible = True End If End Set Get If IsNothing(sp.Attributes("type")) Then sp.Attributes.Add("type", "") End If Return sp.Attributes("type") End Get End Property Public Property UpdateMode As UpdatePanelUpdateMode Set(value As UpdatePanelUpdateMode) up.UpdateMode = value End Set Get Return up.UpdateMode End Get End Property Public Property Text As String Set(value As String) lbl.Text = value chk.Checked = (value = "Y") hl.Text = value hl.NavigateUrl = value Try ddlMark.SelectedIndex = -1 If Not IsNothing(ddlMark.Items.FindByText(value)) Then ddlMark.Items.FindByText(value).Selected = True End If Catch ex As Exception End Try End Set Get Return lbl.Text End Get End Property Public Property RecordID As Integer Set(value As Integer) sp.Attributes.Add("recordId", value) End Set Get If IsNothing(sp.Attributes("recordId")) Then sp.Attributes.Add("recordId", "") End If Return sp.Attributes("recordId") End Get End Property Public Property Table As String Set(value As String) sp.Attributes.Add("table", value) End Set Get If IsNothing(sp.Attributes("table")) Then sp.Attributes.Add("table", "") End If Return sp.Attributes("table") End Get End Property Public Property Field As String Set(value As String) sp.Attributes.Add("field", value) End Set Get If IsNothing(sp.Attributes("field")) Then sp.Attributes.Add("field", "") End If Return sp.Attributes("field") End Get End Property Private Property CurrentState As String Set(value As String) sp.Attributes.Add("currentState", value) End Set Get If IsNothing(sp.Attributes("currentState")) Then sp.Attributes.Add("currentState", "Show") End If Return sp.Attributes("currentState") End Get End Property Public Property DataSource As String Set(value As String) ddl.DataSourceID = value End Set Get Return ddl.DataSourceID End Get End Property Public Property DataTextField As String Set(value As String) ddl.DataTextField = value End Set Get Return ddl.DataTextField End Get End Property Public Property DataValueField As String Set(value As String) ddl.DataValueField = value End Set Get Return ddl.DataValueField End Get End Property Public WriteOnly Property TextMode As Integer Set(value As Integer) Select Case value Case 0 : txt.TextMode = TextBoxMode.SingleLine Case 1 : txt.TextMode = TextBoxMode.MultiLine End Select End Set End Property Public Property TextWidth As Integer Set(value As Integer) txt.Width = Unit.Pixel(value) End Set Get Return txt.Width.Value End Get End Property Public WriteOnly Property OperationMode As String Set(value As String) If value = "FORM" Then txtDatePicker1.CIdMode = UI.ClientIDMode.Predictable Else txtDatePicker1.CIdMode = UI.ClientIDMode.Static End If End Set End Property Public Event Saving(tableName As String, fieldName As String, id As String, value As String, fieldType As String, byButton As Boolean, text As String, ByRef cancelSave As Boolean) Public Event Saved(tableName As String, fieldName As String, id As String, value As String, fieldType As String, byButton As Boolean, text As String) Public Event Cancelled(rowId As Integer, fieldName As String) Public Event Editing(rowId As Integer, fieldName As String) Public Sub Edit() lbl.Visible = False If lbl.Text = " " Then lbl.Text = "" Select Case Type Case "TEXT", "URL" hl.Visible = False txt.Visible = True txt.Text = lbl.Text.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "'").Replace(""", """") txt.Focus() 'txt.Width = Unit.Percentage(90) Case "NUMBER" hl.Visible = False txt.Visible = True txt.Text = lbl.Text.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "'").Replace(""", """").Replace(" ", "") txt.Focus() Case "TEXTBOX" txt.Visible = True txt.Text = lbl.Text.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "'").Replace(""", """").Replace("<br>", Chr(10)) txt.TextMode = TextBoxMode.MultiLine txt.Width = Unit.Percentage(98) txt.Rows = 2 txt.Focus() Case "ENCODED TEXT" txt.Visible = True txt.Text = lbl.Text.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "'").Replace(""", """").Replace("<br>", Chr(10)) txt.TextMode = TextBoxMode.MultiLine txt.Width = Unit.Percentage(98) txt.Rows = 2 txt.Focus() Case "TIME" txt.Visible = True txt.Text = lbl.Text.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "'").Replace(""", """").Replace("<br>", Chr(10)) txt.TextMode = TextBoxMode.Time txt.Focus() Case "DROPDOWNLIST" ddl.Visible = True ddl.DataBind() Try ddl.SelectedIndex = -1 ddl.Items.FindByValue(lbl.Text.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "'").Replace(""", """")).Selected = True Catch ex As Exception End Try ddl.Focus() Case "DATE" txtDatePicker1.Text = lbl.Text txtDatePicker1.Visible = True txtDatePicker1.Focus() Case "CHECKBOX" chkshow.Visible = False chk.Visible = True chk.Enabled = True chk.Checked = (lbl.Text = "Y") chk.Focus() Case "MARK" chkshow.Visible = False ddlMark.Visible = True Try ddlMark.Items.FindByText(lbl.Text).Selected = True Catch ex As Exception End Try End Select ibSave.Visible = True ibCancel.Visible = True sp.Attributes("onclick") = "" If up.UpdateMode = UpdatePanelUpdateMode.Conditional Then up.Update() End If CurrentState = "Edit" End Sub Public Sub Show(ByVal update As Boolean) If Type = "URL" Then lbl.Visible = False Else lbl.Visible = True End If txt.Visible = False ddl.Visible = False txtDatePicker1.Visible = False ibSave.Visible = False ibCancel.Visible = False If update Then Select Case Type Case "TEXT", "TIME" lbl.Text = txt.Text hl.Visible = False Case "NUMBER" lbl.Text = FormatNumber(txt.Text) hl.Visible = False Case "TEXTBOX", "ENCODED TEXT" Dim literalText As String = "" For Each c As Char In txt.Text If c = Chr(10) Then literalText &= "<br>" Else literalText &= c End If Next lbl.Text = literalText ' txt.Text.Replace(Chr(10), "<br>") hl.Visible = False Case "URL" hl.Text = txt.Text hl.NavigateUrl = txt.Text lbl.Visible = False hl.Visible = True Case "DROPDOWNLIST" lbl.Text = ddl.SelectedValue Case "DATE" lbl.Text = txtDatePicker1.Text Case "CHECKBOX" lbl.Visible = False chk.Visible = False If chk.Checked Then chkshow.ImageUrl = "~/Images/checked.png" Else chkshow.ImageUrl = "~/Images/unchecked.png" End If chkshow.Visible = True Case "MARK" ddlMark.Visible = False If ddlMark.SelectedItem.Text <> " " Then chkshow.ImageUrl = "~/Images/checked.png" Else chkshow.ImageUrl = "~/Images/unchecked.png" End If chkshow.Visible = True End Select 'Save() Else If Type = "CHECKBOX" Or Type = "MARK" Then lbl.Visible = False 'chk.Visible = True 'chk.Enabled = False chk.Checked = (lbl.Text = "Y") If chk.Checked Then chkshow.ImageUrl = "~/Images/checked.png" Else chkshow.ImageUrl = "~/Images/unchecked.png" End If chkshow.Visible = True End If End If Dim _jsSingle1 As String = Page.ClientScript.GetPostBackClientHyperlink(btnFake, "") sp.Attributes("onclick") = _jsSingle1 sp.Style("cursor") = "pointer" If up.UpdateMode = UpdatePanelUpdateMode.Conditional Then up.Update() End If CurrentState = "Show" End Sub Protected Sub ibSave_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ibSave.Click Save(True) Show(True) End Sub Public Sub Save(Optional byButton As Boolean = False) Dim value As String = "" Dim text As String = "" Select Case Type Case "TEXT", "TEXTBOX", "URL", "TIME", "ENCODED TEXT" value = txt.Text text = value Case "NUMBER" If IsNumeric(txt.Text.Replace(" ", "")) Or IsNumeric(txt.Text.Replace(" ", "").Replace(".", ",")) Then value = txt.Text.Replace(" ", "").Replace(",", ".") ElseIf txt.Text.Trim = "" Then value = "" Else 'Not a valid number txt.Text = "Format Error!" lbl.ForeColor = Drawing.Color.Red Exit Sub End If text = value Case "DROPDOWNLIST" value = ddl.SelectedValue text = ddl.SelectedItem.Text Case "DATE" value = txtDatePicker1.Text text = value Case "CHECKBOX" value = IIf(chk.Checked, "Y", "N") text = value Case "MARK" value = ddlMark.SelectedItem.Text text = value End Select Dim cancelSave As Boolean = False RaiseEvent Saving(Table, Field, RecordID, value, Type, byButton, text, cancelSave) If Not cancelSave Then Dim sql As String = "UPDATE @table SET @field=@value WHERE id=@id" If Type = "ENCODED TEXT" Then sql = "UPDATE @table SET @field=hr.dbo.fn_encodeString(@value, 'name') WHERE id=@id" End If sql = sql.Replace("@table", Table).Replace("@field", "[" & Field & "]") Dim com As New SqlCommand(sql) With com.Parameters .AddWithValue("id", RecordID) If Type = "NUMBER" And value = "" Then .AddWithValue("value", DBNull.Value) Else .AddWithValue("value", value) End If End With Dim sqlf As New SqlFunctions sqlf.NonQuery(com) RaiseEvent Saved(Table, Field, RecordID, value, Type, byButton, text) End If End Sub Protected Sub ibCancel_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ibCancel.Click Show(False) RaiseEvent Cancelled(RecordID, Field) End Sub Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 'lbl.ForeColor = Drawing.Color.Black End Sub Protected Sub PagePreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender btnFake.CommandArgument = Field & "_" & RecordID If CurrentState = "Show" And Not IsReadOnly Then Dim _jsSingle1 As String = Page.ClientScript.GetPostBackClientHyperlink(btnFake, "") 'sp.Attributes("ondblclick") = _jsSingle1 sp.Attributes("onclick") = _jsSingle1 sp.Style("cursor") = "pointer" 'chk.Attributes("ondblclick") = _jsSingle1 chk.Attributes("onclick") = _jsSingle1 chk.Style("cursor") = "pointer" End If If Type = "NUMBER" Or Type = "FORMULA" Then lbl.ToolTip = lbl.Text lbl.Text = FormatNumber(lbl.Text) txt.Width = Unit.Pixel(90) End If End Sub Protected Sub btnFake_Click(sender As Object, e As System.EventArgs) Handles btnFake.Click If Not IsReadOnly Then Edit() RaiseEvent Editing(RecordID, Field) End If End Sub Private Function FormatNumber(val As String) As String If IsNumeric(val.Replace(" ", "")) Or IsNumeric(val.Replace(" ", "").Replace(".", ",")) Then Dim dTmp As Decimal Dim sTmp As String sTmp = val.Replace(" ", "").Replace(",", ".") Try dTmp = Convert.ToDecimal(sTmp) sTmp = dTmp.ToString '("#,#.##", System.Globalization.CultureInfo.CreateSpecificCulture("ru-RU")) If sTmp.Contains(".") Then sTmp = sTmp.Trim("0") End If If sTmp.StartsWith(".") Then sTmp = "0" & sTmp End If If sTmp.EndsWith(".") Then sTmp = sTmp.Trim(".") End If Return sTmp Catch ex As Exception Return sTmp End Try Else Return "" End If End Function End Class