CTextBox: Keep size between postbacks when resized.

This commit is contained in:
2017-04-04 23:54:19 +02:00
parent 3b480c58ca
commit 3ede7ea52c
6 changed files with 155 additions and 17 deletions

View File

@@ -1,13 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using VAR.Json;
namespace VAR.Focus.Web.Controls
{
public class CTextBox : TextBox, IValidableControl
public class CTextBox : Control, INamingContainer, IValidableControl
{
#region Declarations
private TextBox _txtContent = new TextBox();
private HiddenField _hidSize = null;
private const string CssClassBase = "textbox";
private string _cssClassExtra = "";
@@ -19,6 +26,8 @@ namespace VAR.Focus.Web.Controls
private Control _nextFocusOnEnter = null;
private bool _keepSize = false;
#endregion Declarations
#region Properties
@@ -53,44 +62,93 @@ namespace VAR.Focus.Web.Controls
set { _nextFocusOnEnter = value; }
}
public bool KeepSize
{
get { return _keepSize; }
set { _keepSize = value; }
}
public string Text
{
get { return _txtContent.Text; }
set { _txtContent.Text = value; }
}
public TextBoxMode TextMode
{
get { return _txtContent.TextMode; }
set { _txtContent.TextMode = value; }
}
#endregion Properties
#region Control life cycle
public CTextBox()
{
Init += CTextBox_Init;
PreRender += CTextbox_PreRender;
}
private void CTextBox_Init(object sender, EventArgs e)
{
Controls.Add(_txtContent);
if (TextMode == TextBoxMode.MultiLine)
{
if (_keepSize)
{
_hidSize = new HiddenField();
Controls.Add(_hidSize);
}
string strCfgName = string.Format("{0}_cfg", this.ClientID);
Dictionary<string, object> cfg = new Dictionary<string, object>
{
{"txtContent", _txtContent.ClientID},
{"hidSize", _hidSize.ClientID},
{"keepSize", _keepSize },
};
JsonWriter jsonWriter = new JsonWriter();
StringBuilder sbCfg = new StringBuilder();
sbCfg.AppendFormat("<script>\n");
sbCfg.AppendFormat("var {0} = {1};\n", strCfgName, jsonWriter.Write(cfg));
sbCfg.AppendFormat("CTextBox_Multiline_Init({0});\n", strCfgName);
sbCfg.AppendFormat("</script>\n");
LiteralControl liScript = new LiteralControl(sbCfg.ToString());
Controls.Add(liScript);
}
}
private void CTextbox_PreRender(object sender, EventArgs e)
{
CssClass = CssClassBase;
_txtContent.CssClass = CssClassBase;
if (string.IsNullOrEmpty(_cssClassExtra) == false)
{
CssClass = string.Format("{0} {1}", CssClassBase, _cssClassExtra);
_txtContent.CssClass = string.Format("{0} {1}", CssClassBase, _cssClassExtra);
}
if (Page.IsPostBack && (_allowEmpty == false && IsEmpty()) || _markedInvalid)
{
CssClass += " textboxInvalid";
_txtContent.CssClass += " textboxInvalid";
}
Attributes.Add("onchange", "ElementRemoveClass(this, 'textboxInvalid');");
_txtContent.Attributes.Add("onchange", "ElementRemoveClass(this, 'textboxInvalid');");
if (string.IsNullOrEmpty(_placeHolder) == false)
{
Attributes.Add("placeholder", _placeHolder);
_txtContent.Attributes.Add("placeholder", _placeHolder);
}
if (_nextFocusOnEnter != null)
{
this.Attributes.Add("onkeydown", string.Format(
_txtContent.Attributes.Add("onkeydown", string.Format(
"if(event.keyCode==13){{document.getElementById('{0}').focus(); return false;}}",
_nextFocusOnEnter.ClientID));
}
// FIX: The framework deletes textbox values on password mode
if (TextMode == TextBoxMode.Password)
if (_txtContent.TextMode == TextBoxMode.Password)
{
Attributes["value"] = Text;
_txtContent.Attributes["value"] = _txtContent.Text;
}
}
@@ -100,12 +158,12 @@ namespace VAR.Focus.Web.Controls
public bool IsEmpty()
{
return string.IsNullOrEmpty(Text);
return string.IsNullOrEmpty(_txtContent.Text);
}
public bool IsValid()
{
return _allowEmpty || (string.IsNullOrEmpty(Text) == false);
return _allowEmpty || (string.IsNullOrEmpty(_txtContent.Text) == false);
}
#endregion Public methods