add rudimentary ListView control

This commit is contained in:
Michael Becker 2024-08-05 22:59:28 -04:00
parent 3569914d3f
commit 9a4dbda5df
5 changed files with 169 additions and 15 deletions

View File

@ -2,8 +2,9 @@ namespace MBS.Web.UI.HtmlControls;
using System.Xml;
using MBS.Core.Collections.Generic;
using MBS.Web.UI.WebControls;
public class HtmlGenericControl : Control
public class HtmlGenericControl : Container
{
private string _tagName;
protected override string TagName => _tagName;
@ -36,10 +37,16 @@ public class HtmlGenericControl : Control
protected override void RenderContents(XmlWriter writer)
{
base.RenderContents(writer);
if (Content != null)
if (GetChildControls().Count == 0)
{
writer.WriteRaw(Content);
if (Content != null)
{
writer.WriteRaw(Content);
}
}
else
{
base.RenderContents(writer);
}
}

View File

@ -16,7 +16,7 @@ public class Button : WebControlWithMnemonic
// if (!UseSubmitBehavior)
{
WriteTextWithMnemonic(writer);
}
}
}
protected override IDictionary<string, string> GetControlAttributes()

View File

@ -0,0 +1,144 @@
using MBS.Core.Collections.Generic;
using MBS.Web.UI.HtmlControls;
namespace MBS.Web.UI.WebControls;
public class ListViewItemColumn
{
public class ListViewItemColumnCollection
: System.Collections.ObjectModel.Collection<ListViewItemColumn>
{
}
public string ColumnName { get; set; }
public object Value { get; set; }
public ListViewItemColumn(string columnName, object value)
{
ColumnName = columnName;
Value = value;
}
}
public class ListViewItem
{
public class ListViewItemCollection
: System.Collections.ObjectModel.Collection<ListViewItem>
{
}
public ListViewItemColumn.ListViewItemColumnCollection Columns { get; } = new ListViewItemColumn.ListViewItemColumnCollection();
public ListViewItem(IEnumerable<ListViewItemColumn> columns = null)
{
if (columns != null)
{
Columns.AddRange(columns);
}
}
}
public class ListViewColumn
{
public class ListViewColumnCollection
: System.Collections.ObjectModel.Collection<ListViewColumn>
{
private Dictionary<string, ListViewColumn> _itemsByName = new Dictionary<string, ListViewColumn>();
public ListViewColumn this[string name]
{
get
{
if (_itemsByName.ContainsKey(name))
{
return _itemsByName[name];
}
return null;
}
}
protected override void ClearItems()
{
foreach (ListViewColumn item in this)
{
if (_itemsByName[item.Name] == item)
{
_itemsByName.Remove(item.Name);
}
}
base.ClearItems();
}
protected override void RemoveItem(int index)
{
base.RemoveItem(index);
_itemsByName.Remove(this[index].Name);
}
protected override void InsertItem(int index, ListViewColumn item)
{
base.InsertItem(index, item);
_itemsByName[item.Name] = item;
}
}
public string Name { get; set; }
public string Title { get; set; }
public ListViewColumn(string name, string title)
{
Name = name;
Title = title;
}
}
public class ListView : Container
{
protected override string TagName => "table";
public ListView(IEnumerable<ListViewColumn>? columns = null, IEnumerable<ListViewItem>? items = null)
{
if (columns != null)
{
Columns.AddRange(columns);
}
if (items != null)
{
Items.AddRange(items);
}
}
public ListViewColumn.ListViewColumnCollection Columns { get; } = new ListViewColumn.ListViewColumnCollection();
public ListViewItem.ListViewItemCollection Items { get; } = new ListViewItem.ListViewItemCollection();
private HtmlGenericControl thead = null, tbody = null;
protected override IList<Control> GetChildControls()
{
thead = new HtmlGenericControl("thead");
HtmlGenericControl tr = new HtmlGenericControl("tr");
foreach (ListViewColumn lvc in Columns)
{
HtmlGenericControl th = new HtmlGenericControl("th");
th.Content = lvc.Title;
tr.Controls.Add(th);
}
thead.Controls.Add(tr);
tbody = new HtmlGenericControl("tbody");
foreach (ListViewItem lvi in Items)
{
HtmlGenericControl tr2 = new HtmlGenericControl("tr");
foreach (ListViewItemColumn col in lvi.Columns)
{
HtmlGenericControl td = new HtmlGenericControl("td");
td.Content = col.Value?.ToString();
tr2.Controls.Add(td);
}
tbody.Controls.Add(tr2);
}
return new Control[] { thead, tbody };
}
}

View File

@ -2,7 +2,7 @@ namespace MBS.Web;
public class WebContext
{
public WebContext(WebApplication application, WebRequest request, WebResponse response, Dictionary<string, string> session)
public WebContext(WebApplication application, WebRequest request, WebResponse response, Dictionary<string, object> session)
{
Application = application;
Request = request;
@ -14,5 +14,5 @@ public class WebContext
public WebRequest Request { get; }
public WebResponse Response { get; }
public Dictionary<string, string> Session { get; }
public Dictionary<string, object> Session { get; }
}

View File

@ -133,7 +133,7 @@ public class WebServer
return sb.ToString();
}
private Dictionary<string, Dictionary<string, string>> _sessions = new Dictionary<string, Dictionary<string, string>>();
private Dictionary<string, Dictionary<string, object>> _sessions = new Dictionary<string, Dictionary<string, object>>();
private void HandleClient(System.Net.Sockets.TcpClient client)
{
@ -204,15 +204,18 @@ public class WebServer
sr.ReadBlock(buffer, 0, contentLengthInt);
string content = new string(buffer);
string[] kvps = content.Split(new char[] { '&' });
foreach (string kvp in kvps)
if (!String.IsNullOrEmpty(content))
{
string[] pv = kvp.Split(new char[] { '=' });
form[pv[0]] = pv[1];
string[] kvps = content.Split(new char[] { '&' });
foreach (string kvp in kvps)
{
string[] pv = kvp.Split(new char[] { '=' });
form[pv[0]] = pv[1].Replace('+', ' ');
}
}
}
Dictionary<string, string> session;
Dictionary<string, object> session;
string cookie = "", cookieKey = "", cookieValue = "";
if (headers[HttpRequestHeader.Cookie] != null)
{
@ -231,14 +234,14 @@ public class WebServer
if (!_sessions.ContainsKey(cookieValue))
{
_sessions[cookieValue] = new Dictionary<string, string>();
_sessions[cookieValue] = new Dictionary<string, object>();
}
session = _sessions[cookieValue];
}
else
{
session = new Dictionary<string, string>();
session = new Dictionary<string, object>();
}
if (cookieValue == "")