基于ASP:表控制我们如何创建一个THEAD? [英] On asp:Table Control how do we create a thead?

查看:249
本文介绍了基于ASP:表控制我们如何创建一个THEAD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN文章关于这个问题,我们可以看到,我们创建了一个 TableHeaderRow 包含 TableHeaderCell 秒。



不过,他们补充表头是这样的:

  myTable.Row.AddAt(0,headerRow ); 



其输出HTML:

 <表ID =表1...> 
< TR>
百分位范围=列简称=1上校团长>第1列页眉和LT; /第i
百分位范围=列简称=2上校团长>列2部< /第i
百分位范围=列简称=西3头>第3栏页眉和LT; /第i
< / TR>
< TR>
< TD>(0,0)LT; / TD>
< TD>(0,1)LT; / TD>
< TD>(0,2)LT; / TD>
< / TR>

...

和它应该有 < THEAD> < TBODY> (因此它可以无缝使用的tablesorter)的:)

 <表ID =表1...> 
<&THEAD GT;
< TR>
百分位范围=列简称=1上校团长>第1列页眉和LT; /第i
百分位范围=列简称=2上校团长>列2部< /第i
百分位范围=列简称=西3头>第3栏页眉和LT; /第i
< / TR>
< / THEAD>
<&TBODY GT;
< TR>
< TD>(0,0)LT; / TD>
< TD>(0,1)LT; / TD>
< TD>(0,2)LT; / TD>
< / TR>

< / TBODY>



HTML ASPX代码



 < ASP:表ID =表1=服务器/> 



我怎样才能输出正确的语法?






正如信息 GridView控件控件还具有以下内置作为我们只需要设置Accesbility并使用 HeaderRow

  gv.UseAccessibleHeader = TRUE; 
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
gv.HeaderRow.CssClass =MyClass的;



但问题是控制


解决方案

只是找到了一个办法做到这一点,我们需要使用从基本控制继承我们自己的控制,对例如

 公共类myTable的:System.Web.UI程序。 WebControls.Table 
{
保护覆盖无效的OnPreRender(EventArgs的发送)
{
表表=控制[0]如表;

如果(表=空&安培;!&安培; table.Rows.Count大于0)
{
//第一行是表头<&THEAD GT;
table.Rows [0] = .TableSection TableRowSection.TableHeader;
//最后一行是页脚页眉和LT; TFOOT>
table.Rows(不使用对此有何评论)[table.Rows.Count - 1] .TableSection = TableRowSection.TableFooter;

字段信息字段= typeof运算(的WebControl).GetField(tagKey,BindingFlags.Instance | BindingFlags.NonPublic可);
的foreach(在table.Rows [0] .Cells TableCell的细胞)
field.SetValue(细胞,HtmlTextWriterTag.Th);
}
base.OnPreRender(E);
}
}



正常工作与的DataGrid 以及

 公共类myDataGrid:System.Web.UI.WebControls.DataGrid 
{
保护覆盖无效的OnPreRender(EventArgs的发送)
{
表表=控制[0]如表;

如果(表=空&安培;!&安培; table.Rows.Count大于0)
{
//第一行是表头<&THEAD GT;
table.Rows [0] = .TableSection TableRowSection.TableHeader;
//最后一行是页脚页眉和LT; TFOOT>
table.Rows(不使用对此有何评论)[table.Rows.Count - 1] .TableSection = TableRowSection.TableFooter;

字段信息字段= typeof运算(的WebControl).GetField(tagKey,BindingFlags.Instance | BindingFlags.NonPublic可);
的foreach(在table.Rows [0] .Cells TableCell的细胞)
field.SetValue(细胞,HtmlTextWriterTag.Th);
}
base.OnPreRender(E);
}
}



再比如你只需要而不是用它基地控制:

 保护无效的Page_Load(对象发件人,EventArgs五)
{
如果(页! .IsPostBack)
{
myGridView DG =新myGridView();
dg.DataSource =新的字符串[] {1,2,3,4,5,6};
dg.DataBind();

ph.Controls.Add(DG);
}
}

和在aspx页面,只需添加像一个占位符

 < ASP:占位符ID =PH=服务器/> 




在引擎收录

From an MSDN article on the subject, we can see that we create a TableHeaderRowthat contains TableHeaderCells.

But they add the table header like this:

myTable.Row.AddAt(0, headerRow);

which outputs the HTML:

<table id="Table1" ... > 
<tr> 
    <th scope="column" abbr="Col 1 Head">Column 1 Header</th>
    <th scope="column" abbr="Col 2 Head">Column 2 Header</th>
    <th scope="column" abbr="Col 3 Head">Column 3 Header</th> 
</tr>
<tr> 
    <td>(0,0)</td>
    <td>(0,1)</td>
    <td>(0,2)</td>
</tr>

...

and it should have <thead> and <tbody> (so it works seamless with tablesorter) :)

<table id="Table1" ... > 
<thead>
<tr> 
    <th scope="column" abbr="Col 1 Head">Column 1 Header</th>
    <th scope="column" abbr="Col 2 Head">Column 2 Header</th>
    <th scope="column" abbr="Col 3 Head">Column 3 Header</th> 
</tr>
</thead>
<tbody>
<tr> 
    <td>(0,0)</td>
    <td>(0,1)</td>
    <td>(0,2)</td>
</tr>
    ...
    </tbody>

the HTML aspx code is

<asp:Table ID="Table1" runat="server" />

How can I output the correct syntax?


Just as information, the GridViewcontrol has this built in as we just need to set the Accesbility and use the HeaderRow

gv.UseAccessibleHeader = true;
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
gv.HeaderRow.CssClass = "myclass";

but the question is for the Table control.

解决方案

Just found a way to do this, we do need to use our own controls that inherit from the base control, for example a Table

public class myTable : System.Web.UI.WebControls.Table
{
    protected override void OnPreRender(EventArgs e)
    {
        Table table = Controls[0] as Table;

        if (table != null && table.Rows.Count > 0)
        {
            // first row is the Table Header <thead>
            table.Rows[0].TableSection = TableRowSection.TableHeader;
            // last row is the Footer Header <tfoot> (comment for not using this)
            table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter;

            FieldInfo field = typeof(WebControl).GetField("tagKey", BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (TableCell cell in table.Rows[0].Cells)
                field.SetValue(cell, HtmlTextWriterTag.Th);
        }
        base.OnPreRender(e);
    }
}

works fine with DataGrid as well

public class myDataGrid : System.Web.UI.WebControls.DataGrid 
{
    protected override void OnPreRender(EventArgs e)
    {
        Table table = Controls[0] as Table;

        if (table != null && table.Rows.Count > 0)
        {
            // first row is the Table Header <thead>
            table.Rows[0].TableSection = TableRowSection.TableHeader;
            // last row is the Footer Header <tfoot> (comment for not using this)
            table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter;

            FieldInfo field = typeof(WebControl).GetField("tagKey", BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (TableCell cell in table.Rows[0].Cells)
                field.SetValue(cell, HtmlTextWriterTag.Th);
        }
        base.OnPreRender(e);
    }
}

then for example you just need to use it instead the base control:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        myGridView dg = new myGridView();
        dg.DataSource = new string[] { "1", "2", "3", "4", "5", "6" };
        dg.DataBind();

        ph.Controls.Add(dg);
    }
}

and in aspx page, just add a place holder like:

<asp:PlaceHolder ID="ph" runat="server" />

full example in pastebin

这篇关于基于ASP:表控制我们如何创建一个THEAD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆