VAR.WebFormsCore: Implementation of controls
This commit is contained in:
@@ -9,9 +9,19 @@ namespace VAR.WebFormsCore.Code
|
||||
|
||||
public static string GetRequestParm(this HttpContext context, string parm)
|
||||
{
|
||||
if (context.Request.Method == "POST")
|
||||
{
|
||||
foreach (string key in context.Request.Form.Keys)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key) == false && key == parm)
|
||||
{
|
||||
return context.Request.Form[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (string key in context.Request.Query.Keys)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key) == false && key.EndsWith(parm))
|
||||
if (string.IsNullOrEmpty(key) == false && key == parm)
|
||||
{
|
||||
return context.Request.Query[key];
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace VAR.WebFormsCore.Code
|
||||
{
|
||||
@@ -12,8 +13,101 @@ namespace VAR.WebFormsCore.Code
|
||||
|
||||
public static string MapContentPath(string path)
|
||||
{
|
||||
string mappedPath = Path.Combine(_contentRoot, path);
|
||||
string mappedPath = string.Concat(_contentRoot, "/", path);
|
||||
return mappedPath;
|
||||
}
|
||||
|
||||
public static string HtmlEncode(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text)) { return text; }
|
||||
|
||||
StringBuilder sbResult = new();
|
||||
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
char ch = text[i];
|
||||
|
||||
if (ch == '<')
|
||||
{
|
||||
sbResult.Append("<");
|
||||
}
|
||||
else if (ch == '>')
|
||||
{
|
||||
sbResult.Append(">");
|
||||
}
|
||||
else if (ch == '"')
|
||||
{
|
||||
sbResult.Append(""");
|
||||
}
|
||||
else if (ch == '\'')
|
||||
{
|
||||
sbResult.Append("'");
|
||||
}
|
||||
else if (ch == '&')
|
||||
{
|
||||
sbResult.Append("&");
|
||||
}
|
||||
else if (ch > 127)
|
||||
{
|
||||
sbResult.Append("&#");
|
||||
sbResult.Append(((int)ch).ToString(NumberFormatInfo.InvariantInfo));
|
||||
sbResult.Append(';');
|
||||
}
|
||||
else
|
||||
{
|
||||
sbResult.Append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
return sbResult.ToString();
|
||||
}
|
||||
|
||||
public static string UrlEncode(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text)) { return text; }
|
||||
|
||||
StringBuilder sbResult = new();
|
||||
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
char ch = text[i];
|
||||
|
||||
if (ch == ' ')
|
||||
{
|
||||
sbResult.Append('+');
|
||||
}
|
||||
else if (IsUrlSafe(ch) == false)
|
||||
{
|
||||
sbResult.AppendFormat("%{0:X02}", ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
sbResult.Append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
return sbResult.ToString();
|
||||
}
|
||||
|
||||
public static bool IsUrlSafe(char ch)
|
||||
{
|
||||
if (
|
||||
(ch >= 'a' && ch <= 'z') ||
|
||||
(ch >= 'A' && ch <= 'Z') ||
|
||||
(ch >= '0' && ch <= '9') ||
|
||||
ch == '-' ||
|
||||
ch == '_' ||
|
||||
ch == '.' ||
|
||||
ch == '!' ||
|
||||
ch == '*' ||
|
||||
ch == '(' ||
|
||||
ch == ')' ||
|
||||
false)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.IO;
|
||||
|
||||
namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
// TODO: Implememnt control
|
||||
public class Button : Control, IReceivePostbackEvent
|
||||
{
|
||||
public Button()
|
||||
@@ -11,17 +10,21 @@ namespace VAR.WebFormsCore.Controls
|
||||
CssClass = "button";
|
||||
}
|
||||
|
||||
public string Text { get; set; }
|
||||
public string CommandArgument { get; set; }
|
||||
private string _text = string.Empty;
|
||||
|
||||
public string Text { get { return _text; } set { _text = value; } }
|
||||
|
||||
private string _commandArgument = string.Empty;
|
||||
|
||||
public string CommandArgument { get { return _commandArgument; } set { _commandArgument = value; } }
|
||||
|
||||
public event EventHandler Click;
|
||||
|
||||
|
||||
public override void Render(TextWriter textWriter)
|
||||
{
|
||||
textWriter.Write("<input type=\"submit\" ");
|
||||
RenderAttributes(textWriter);
|
||||
RenderAttribute(textWriter, "value", Text);
|
||||
RenderAttribute(textWriter, "value", _text);
|
||||
textWriter.Write(">");
|
||||
|
||||
base.Render(textWriter);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using VAR.WebFormsCore.Code;
|
||||
using VAR.WebFormsCore.Pages;
|
||||
|
||||
namespace VAR.WebFormsCore.Controls
|
||||
@@ -172,7 +173,7 @@ namespace VAR.WebFormsCore.Controls
|
||||
|
||||
public void RenderAttribute(TextWriter textWriter, string key, string value)
|
||||
{
|
||||
textWriter.Write(" {0}=\"{1}\"", key, value);
|
||||
textWriter.Write(" {0}=\"{1}\"", key, ServerHelpers.HtmlEncode(value));
|
||||
}
|
||||
|
||||
protected void RenderAttributes(TextWriter textWriter, bool forceId = false)
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
// TODO: Implememnt control
|
||||
public class HiddenField : Control
|
||||
{
|
||||
public string Value { get; set; }
|
||||
private string _value = string.Empty;
|
||||
|
||||
public string Value { get { return _value; } set { _value = value; } }
|
||||
|
||||
protected override void Process()
|
||||
{
|
||||
@@ -21,7 +22,7 @@ namespace VAR.WebFormsCore.Controls
|
||||
RenderAttributes(textWriter, forceId: true);
|
||||
if (string.IsNullOrEmpty(Value) == false)
|
||||
{
|
||||
textWriter.Write(" value=\"{0}\"", Value);
|
||||
RenderAttribute(textWriter, "value", _value);
|
||||
}
|
||||
textWriter.Write(">");
|
||||
textWriter.Write("</input>");
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using VAR.WebFormsCore.Code;
|
||||
|
||||
namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
// TODO: Implment this control
|
||||
public class HtmlForm : Control
|
||||
{
|
||||
private string _method = "post";
|
||||
@@ -14,12 +17,29 @@ namespace VAR.WebFormsCore.Controls
|
||||
textWriter.Write("<form ");
|
||||
RenderAttributes(textWriter);
|
||||
RenderAttribute(textWriter, "method", _method);
|
||||
RenderAttribute(textWriter, "action", Page.GetType().Name);
|
||||
RenderAttribute(textWriter, "action", GetAction());
|
||||
textWriter.Write(">");
|
||||
|
||||
base.Render(textWriter);
|
||||
|
||||
textWriter.Write("</form>");
|
||||
}
|
||||
private string GetAction()
|
||||
{
|
||||
StringBuilder sbAction = new();
|
||||
sbAction.Append(Page.GetType().Name);
|
||||
|
||||
if (Page.Context.Request.Query.Count > 0)
|
||||
{
|
||||
sbAction.Append('?');
|
||||
|
||||
foreach (KeyValuePair<string, StringValues> queryParam in Page.Context.Request.Query)
|
||||
{
|
||||
sbAction.AppendFormat("&{0}={1}", ServerHelpers.UrlEncode(queryParam.Key), ServerHelpers.UrlEncode(queryParam.Value[0]));
|
||||
}
|
||||
}
|
||||
|
||||
return sbAction.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,23 @@
|
||||
using System.IO;
|
||||
using VAR.WebFormsCore.Code;
|
||||
|
||||
namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
// TODO: Implememnt control
|
||||
public class Label : Control
|
||||
{
|
||||
#region Declarations
|
||||
#region Properties
|
||||
|
||||
private string _tagName = "span";
|
||||
|
||||
#endregion Declarations
|
||||
|
||||
#region Properties
|
||||
|
||||
public string Tag
|
||||
{
|
||||
get { return _tagName; }
|
||||
set { _tagName = value; }
|
||||
}
|
||||
|
||||
public string Text { get; set; }
|
||||
private string _text = string.Empty;
|
||||
|
||||
public string Text { get { return _text; } set { _text = value; } }
|
||||
|
||||
#endregion Properties
|
||||
|
||||
@@ -31,7 +29,7 @@ namespace VAR.WebFormsCore.Controls
|
||||
RenderAttributes(textWriter);
|
||||
textWriter.Write(">");
|
||||
|
||||
textWriter.Write(Text);
|
||||
textWriter.Write(ServerHelpers.HtmlEncode(_text));
|
||||
|
||||
base.Render(textWriter);
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
// TODO: Implememnt control
|
||||
public class Panel : HtmlGenericControl, INamingContainer
|
||||
{
|
||||
public Panel() : base("div") { }
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
using System.IO;
|
||||
using VAR.WebFormsCore.Code;
|
||||
|
||||
namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
// TODO: Implememnt control
|
||||
public class TextBox : Control
|
||||
{
|
||||
public string Text { get; set; }
|
||||
private string _text = string.Empty;
|
||||
|
||||
public string Text { get { return _text; } set { _text = value; } }
|
||||
|
||||
public TextBoxMode TextMode { get; set; } = TextBoxMode.Normal;
|
||||
|
||||
@@ -13,7 +15,7 @@ namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
if (Page.IsPostBack && Page.Context.Request.Form.ContainsKey(ClientID))
|
||||
{
|
||||
Text = Page.Context.Request.Form[ClientID];
|
||||
_text = Page.Context.Request.Form[ClientID];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +23,11 @@ namespace VAR.WebFormsCore.Controls
|
||||
{
|
||||
if (TextMode == TextBoxMode.MultiLine)
|
||||
{
|
||||
textWriter.Write("<textare ");
|
||||
textWriter.Write("<textarea ");
|
||||
RenderAttributes(textWriter, forceId: true);
|
||||
textWriter.Write(">");
|
||||
textWriter.Write(Text);
|
||||
textWriter.Write("</textare>");
|
||||
textWriter.Write(ServerHelpers.HtmlEncode(_text));
|
||||
textWriter.Write("</textarea>");
|
||||
}
|
||||
else if (TextMode == TextBoxMode.Normal)
|
||||
{
|
||||
@@ -33,7 +35,7 @@ namespace VAR.WebFormsCore.Controls
|
||||
RenderAttributes(textWriter, forceId: true);
|
||||
if (string.IsNullOrEmpty(Text) == false)
|
||||
{
|
||||
RenderAttribute(textWriter, "value", Text);
|
||||
RenderAttribute(textWriter, "value", _text);
|
||||
}
|
||||
textWriter.Write(">");
|
||||
textWriter.Write("</input>");
|
||||
@@ -44,7 +46,7 @@ namespace VAR.WebFormsCore.Controls
|
||||
RenderAttributes(textWriter, forceId: true);
|
||||
if (string.IsNullOrEmpty(Text) == false)
|
||||
{
|
||||
RenderAttribute(textWriter, "value", Text);
|
||||
RenderAttribute(textWriter, "value", _text);
|
||||
}
|
||||
textWriter.Write(">");
|
||||
textWriter.Write("</input>");
|
||||
|
||||
@@ -6,7 +6,6 @@ using VAR.WebFormsCore.Controls;
|
||||
|
||||
namespace VAR.WebFormsCore.Pages
|
||||
{
|
||||
// TODO: Implement Page
|
||||
public class Page : Control, IHttpHandler
|
||||
{
|
||||
public string Title { get; set; }
|
||||
@@ -26,8 +25,13 @@ namespace VAR.WebFormsCore.Pages
|
||||
}
|
||||
|
||||
OnPreInit();
|
||||
if (context.Response.HasStarted) { return; }
|
||||
|
||||
OnInit();
|
||||
if (context.Response.HasStarted) { return; }
|
||||
|
||||
OnLoad();
|
||||
if (context.Response.HasStarted) { return; }
|
||||
|
||||
if (_isPostBack)
|
||||
{
|
||||
@@ -38,12 +42,17 @@ namespace VAR.WebFormsCore.Pages
|
||||
if (context.Request.Form.ContainsKey(clientID))
|
||||
{
|
||||
(control as IReceivePostbackEvent).ReceivePostBack();
|
||||
if (context.Response.HasStarted) { return; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OnPreRender();
|
||||
if (context.Response.HasStarted) { return; }
|
||||
|
||||
Render(stringWriter);
|
||||
if (context.Response.HasStarted) { return; }
|
||||
|
||||
context.Response.Headers.Add("Content-Type", "text/html");
|
||||
Context.Response.WriteAsync(stringWriter.ToString());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user