VAR.WebFormsCore: Implement Postback handling, for buttons, textbox and hidden fields.

This commit is contained in:
2021-06-21 03:47:25 +02:00
parent 211b500821
commit 0edf26e125
9 changed files with 100 additions and 20 deletions

View File

@@ -4,7 +4,7 @@ using System.IO;
namespace VAR.WebFormsCore.Controls
{
// TODO: Implememnt control
public class Button : Control
public class Button : Control, IReceivePostbackEvent
{
public Button()
{
@@ -28,5 +28,10 @@ namespace VAR.WebFormsCore.Controls
textWriter.Write("</input>");
}
public void ReceivePostBack()
{
Click?.Invoke(this, null);
}
}
}

View File

@@ -141,12 +141,6 @@ namespace VAR.WebFormsCore.Controls
"if(event.keyCode==13){{document.getElementById('{0}').focus(); return false;}}",
_nextFocusOnEnter.ClientID));
}
// FIX: The framework deletes textbox values on password mode
if (_txtContent.TextMode == TextBoxMode.Password)
{
_txtContent.Attributes["value"] = _txtContent.Text;
}
}
#endregion Control life cycle

View File

@@ -34,8 +34,11 @@ namespace VAR.WebFormsCore.Controls
public event EventHandler Load;
protected virtual void Process() { }
protected void OnLoad()
{
Process();
Load?.Invoke(this, null);
foreach (Control control in Controls)
{
@@ -172,9 +175,9 @@ namespace VAR.WebFormsCore.Controls
textWriter.Write(" {0}=\"{1}\"", key, value);
}
protected void RenderAttributes(TextWriter textWriter)
protected void RenderAttributes(TextWriter textWriter, bool forceId = false)
{
if (string.IsNullOrEmpty(_id) == false)
if (string.IsNullOrEmpty(_id) == false || forceId)
{
RenderAttribute(textWriter, "id", ClientID);
RenderAttribute(textWriter, "name", ClientID);
@@ -198,5 +201,21 @@ namespace VAR.WebFormsCore.Controls
}
}
public List<Control> ChildsOfType<T>(List<Control> controls = null)
{
if (controls == null)
{
controls = new List<Control>();
}
if (this is T) { controls.Add(this); }
foreach (Control child in _controls)
{
child.ChildsOfType<T>(controls);
}
return controls;
}
}
}

View File

@@ -6,10 +6,19 @@ namespace VAR.WebFormsCore.Controls
public class HiddenField : Control
{
public string Value { get; set; }
protected override void Process()
{
if (Page.IsPostBack && Page.Context.Request.Form.ContainsKey(ClientID))
{
Value = Page.Context.Request.Form[ClientID];
}
}
public override void Render(TextWriter textWriter)
{
textWriter.Write("<input type=\"hidden\" ");
RenderAttributes(textWriter);
RenderAttributes(textWriter, forceId: true);
if (string.IsNullOrEmpty(Value) == false)
{
textWriter.Write(" value=\"{0}\"", Value);

View File

@@ -1,8 +1,25 @@
namespace VAR.WebFormsCore.Controls
using System.IO;
namespace VAR.WebFormsCore.Controls
{
// TODO: Implment this control
public class HtmlForm : HtmlGenericControl
public class HtmlForm : Control
{
public HtmlForm() : base("form") { }
private string _method = "post";
public HtmlForm() { }
public override void Render(TextWriter textWriter)
{
textWriter.Write("<form ");
RenderAttributes(textWriter);
RenderAttribute(textWriter, "method", _method);
RenderAttribute(textWriter, "action", Page.GetType().Name);
textWriter.Write(">");
base.Render(textWriter);
textWriter.Write("</form>");
}
}
}

View File

@@ -0,0 +1,7 @@
namespace VAR.WebFormsCore.Controls
{
public interface IReceivePostbackEvent
{
void ReceivePostBack();
}
}

View File

@@ -1,7 +1,7 @@
namespace VAR.WebFormsCore.Controls
{
// TODO: Implememnt control
public class Panel : HtmlGenericControl
public class Panel : HtmlGenericControl, INamingContainer
{
public Panel() : base("div") { }
}

View File

@@ -9,12 +9,20 @@ namespace VAR.WebFormsCore.Controls
public TextBoxMode TextMode { get; set; } = TextBoxMode.Normal;
protected override void Process()
{
if (Page.IsPostBack && Page.Context.Request.Form.ContainsKey(ClientID))
{
Text = Page.Context.Request.Form[ClientID];
}
}
public override void Render(TextWriter textWriter)
{
if (TextMode == TextBoxMode.MultiLine)
{
textWriter.Write("<textare ");
RenderAttributes(textWriter);
RenderAttributes(textWriter, forceId: true);
textWriter.Write(">");
textWriter.Write(Text);
textWriter.Write("</textare>");
@@ -22,7 +30,7 @@ namespace VAR.WebFormsCore.Controls
else if (TextMode == TextBoxMode.Normal)
{
textWriter.Write("<input type=\"text\" ");
RenderAttributes(textWriter);
RenderAttributes(textWriter, forceId: true);
if (string.IsNullOrEmpty(Text) == false)
{
RenderAttribute(textWriter, "value", Text);
@@ -33,7 +41,7 @@ namespace VAR.WebFormsCore.Controls
else if (TextMode == TextBoxMode.Password)
{
textWriter.Write("<input type=\"password\" ");
RenderAttributes(textWriter);
RenderAttributes(textWriter, forceId: true);
if (string.IsNullOrEmpty(Text) == false)
{
RenderAttribute(textWriter, "value", Text);

View File

@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Http;
using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Controls;
@@ -19,16 +20,36 @@ namespace VAR.WebFormsCore.Pages
Context = context;
Page = this;
if (context.Request.Method == "POST")
{
_isPostBack = true;
}
OnPreInit();
OnInit();
OnLoad();
OnPreRender();
if (_isPostBack)
{
List<Control> controls = ChildsOfType<IReceivePostbackEvent>();
foreach (Control control in controls)
{
string clientID = control.ClientID;
if (context.Request.Form.ContainsKey(clientID))
{
(control as IReceivePostbackEvent).ReceivePostBack();
}
}
}
OnPreRender();
Render(stringWriter);
context.Response.Headers.Add("Content-Type", "text/html");
Context.Response.WriteAsync(stringWriter.ToString());
}
public bool IsPostBack { get; }
private bool _isPostBack = false;
public bool IsPostBack { get { return _isPostBack; } }
}
}