gpt4 book ai didi

ASP.NET - 显示数据透视表的理想控件

转载 作者:行者123 更新时间:2023-12-02 15:42:53 26 4
gpt4 key购买 nike

我试图在数据绑定(bind)控件中显示一组表格数据,但我需要旋转表格,以便各个记录是表格列而不是表格行。最终结果是一个包含固定列数和可变行数的表,每行显示所有记录的单个字段 like this .由于必须为每个字段而不是每个记录定义 标记,因此转发器不是解决此问题的合适方法。我想知道是否有任何内置的 ASP.NET 控件可以实现我想要的。我关注的是 ListView 控件,但我不确定它是否真的能够实现我所描述的功能。

实际上,假设记录如下:

       Number Yardage Par  ...
(Hole) 1 300 4 ...
(Hole) 2 275 4 ...
(Hole) 3 390 5 ...
(Hole) ... ... ... ...

我需要显示:

           1   2   3   ...
Yardage: 300 275 390 ...
Par: 4 4 5 ...
...: ... ... ... ...

与 标签对抗的一个可行替代方案当然是使用 display: inline 和一些优雅的 CSS,但如果我能保留

谢谢!

最佳答案

我最近遇到了同样的问题,在寻找答案时发现大多数解决方案都围绕着旋转源数据。

我突然想到,问题不在于源数据,而在于我们希望呈现它的方式。尽管 Chris 上面的回答确实看起来会在渲染点改变数据,但我发现如果我需要一个模板化的 GridView ,它就不够灵活。就在那时,也许一个更好的解决方案是捕获网格的表格 HTML 标记并改变它——这实际上意味着该解决方案可以应用于绝对呈现表格标记的任何控件,以及包含的任何模板控件在其中将继续工作。

由于时间压力,我只实现了 GridView 的解决方案;我最初想制作一个模板服务器控件,如果在其中放置任何控件,它将检查它的标记输出并旋转其中包含的任何表)

无论如何,这是为 GridView 实现此解决方案所需的代码。

自定义服务器控制代码

[ToolboxData("<{0}:PivotGridView runat=server></{0}:PivotGridView>")]
public class PivotGridView : GridView
{
bool _pivotGrid = true;

[Browsable(true)]
public bool PivotGrid
{
get
{
return _pivotGrid;
}
set
{
_pivotGrid = value;
EnsureChildControls();
}
}

protected override void RenderContents(HtmlTextWriter output)
{
if (_pivotGrid)
{
System.IO.TextWriter baseOutputTextWriter = new System.IO.StringWriter();
HtmlTextWriter baseOutputHtmlWriter = new HtmlTextWriter(baseOutputTextWriter);

base.RenderContents(baseOutputHtmlWriter);

output.Write(HtmlParserService.PivotHtmlTableMarkup(baseOutputTextWriter.ToString()));
}
else
{
base.RenderContents(output);
}
}
}

HTML 解析器服务代码,分离出来以便于在其他地方实现。

//... using System.Text.RegularExpressions;

public static class HtmlParserService
{
/// <summary>
/// Takes HTML markup locates any table definition held within it and returns that
/// markup with the table HTML pivotted
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string PivotHtmlTableMarkup(string html)
{
html = ReplaceShorthandTableTags(html);

int openingTableTagIndex;
string openingTableTagText;
int closingTableTagIndex;
string tableContentText;

tableContentText = GetTagContentText("table", html, out openingTableTagIndex, out openingTableTagText, out closingTableTagIndex);

MatchCollection rows = GetTagMatches("tr", tableContentText);
if (rows.Count > 0)
{
MatchCollection columns = GetTagMatches("(td|th)", rows[0].Value);

StringBuilder pivottedTableMarkup = new StringBuilder();

for (int i = 0; i < columns.Count; i++)
{
pivottedTableMarkup.Append("<tr>");
foreach (Match row in rows)
{
if (row.Value.Length > 0)
{
columns = GetTagMatches("(td|th)", row.Value);

if (columns.Count>i)
{
pivottedTableMarkup.Append(columns[i].Value);
}
}
}
pivottedTableMarkup.Append("</tr>");
}

string preTableText = "";
if (openingTableTagIndex > 1)
{
preTableText = html.Substring(1, openingTableTagIndex);
}

string postTableText;
postTableText = html.Substring(closingTableTagIndex, html.Length - closingTableTagIndex);

string newHtmlWithPivottedTable;
newHtmlWithPivottedTable = preTableText + openingTableTagText + pivottedTableMarkup.ToString() + postTableText;

return newHtmlWithPivottedTable;
}
else
{
return html;
}

}

/// <summary>
/// Gets the content between the specified tag.
/// </summary>
/// <param name="tag">The tag excluding any markup (e.g. "table" not "<table>"</param>
/// <param name="text">The xml text string to extract content from</param>
/// <param name="openingTagIndex">Outputs the indexed position of the opening tag</param>
/// <param name="openingTagText">Outputs the definition of the tag, e.g. it's attributes etc</param>
/// <param name="closingTagIndex">Outputs the indexed position of the closing tag</param>
/// <returns></returns>
public static string GetTagContentText(string tag, string text, out int openingTagIndex, out string openingTagText, out int closingTagIndex)
{
string contentText;

openingTagIndex = text.ToLower().IndexOf("<" + tag);
openingTagText = text.Substring(openingTagIndex, text.IndexOf(">", openingTagIndex) - openingTagIndex+1);
closingTagIndex = text.ToLower().LastIndexOf("</" + tag + ">");

contentText = text.Substring(
openingTagIndex + openingTagText.Length,
closingTagIndex - (openingTagIndex + openingTagText.Length) );

return contentText;
}

/// <summary>
/// Returns a collection of matches containing the content of each matched tag
/// </summary>
/// <param name="tag">HTML tag to match. Exclude opening and close angled braces.
/// Multiple tags can be matched by specifying them in the following format (tag1|tag2),
/// e.g. (td|th)</param>
/// <param name="html"></param>
/// <returns></returns>
public static MatchCollection GetTagMatches(string tag, string html)
{
Regex regexp = new Regex(@"<" + tag + @"\b[^>]*>(.*?)</" + tag + @">", RegexOptions.IgnoreCase | RegexOptions.Singleline);
return regexp.Matches(html);
}

/// <summary>
/// Ensures any shorthand XML tags are full expressed, e.g.
/// <td/> is converted to <td></td>
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static string ReplaceShorthandTableTags(string value)
{
value=value.Replace("<tr/>", "<tr></tr>");
value=value.Replace("<td/>", "<td></td>");
value=value.Replace("<th/>", "<th></th>");

return value;
}


}

关于ASP.NET - 显示数据透视表的理想控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1533185/

结构,那将是理想的。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com