在webgrid c#网页中显示“找不到记录”

时间:2015-09-04 15:51:41

标签: c# razor webgrid asp.net-webpages

是否有更简单的方法在空网页中显示“找不到记录”?以下是我的代码,对我有用。但是,我不想在else块中为其他选项卡创建HTML表。

@if (Page.Tab == "DisplayGrid")
{
    if (Page.IncompleteCount > 0)
    {
        var grid = new WebGrid(Page.DisplayData, rowsPerPage: 10, defaultSort: "UserName", canSort: true);
        @grid.Pager(mode: WebGridPagerModes.All, previousText: "previous", nextText: "next", numericLinksCount: 20)
        @grid.GetHtml(columns: grid.Columns(
            grid.Column("UserName", "User"),
            grid.Column("Column1", "Column 1", format: @<div><a href="@Href("~/thispage/", item.UserId)" target="_blank">@item.UserId</a></div>),
            grid.Column("Column2", "Column 2", format: @<div class="w400">@HH.TruncateString(@item.Column2, 125)</div>),
            grid.Column("Column3", "Column 3", format: @<div class="w200"> @HH.FmtDate(@item.Column3)</div>)),
                tableStyle: "simple", headerStyle: "hdr", rowStyle: "odd", alternatingRowStyle: "even", mode: WebGridPagerModes.All, previousText: "previous", nextText: "next", numericLinksCount: 20)
    }
   else
    { 
        <table>
            <tr bgcolor ="#336699">
                <td width="150">
                    <font color="white"><b><u>User</u></b></font>
                </td>
                <td width="100">
                    <font color="white"><b>Column 1</b></font>
                </td>
                <td width="300">
                    <font color="white"><b>Column 2</b></font>
                </td>
                <td width="100">
                    <font color="white"><b>Column 3</b></font>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="center">
                    <br />
                    <b>No User Records Found</b>

             <br />
                </td>
            </tr>
        </table>
    }
}

其他选项卡的Webgrid具有不同的列和列数。确保我有正确的colspan#是很繁琐的,以便正确显示“找不到记录”的信息(中心对齐)。有没有更简单的方法呢?感谢。

2 个答案:

答案 0 :(得分:0)

当你没有记录时,你可以只显示一个div,就像这样吗?

@if (Page.Tab == "DisplayGrid")
{
    if (Page.IncompleteCount > 0)
    {
        var grid = new WebGrid(Page.DisplayData, rowsPerPage: 10, defaultSort: "UserName", canSort: true);
        @grid.Pager(mode: WebGridPagerModes.All, previousText: "previous", nextText: "next", numericLinksCount: 20)
        @grid.GetHtml(columns: grid.Columns(
            grid.Column("UserName", "User"),
            grid.Column("Column1", "Column 1", format: @<div><a href="@Href("~/thispage/", item.UserId)" target="_blank">@item.UserId</a></div>),
            grid.Column("Column2", "Column 2", format: @<div class="w400">@HH.TruncateString(@item.Column2, 125)</div>),
            grid.Column("Column3", "Column 3", format: @<div class="w200"> @HH.FmtDate(@item.Column3)</div>)),
                tableStyle: "simple", headerStyle: "hdr", rowStyle: "odd", alternatingRowStyle: "even", mode: WebGridPagerModes.All, previousText: "previous", nextText: "next", numericLinksCount: 20)
    }
   else
    { 
        <div><b>No User Records Found</b></div>
    }
}

感谢您的反馈。标题是否被重用,所以它们每次都是一样的?如果是这样,您可以将标记分配给变量,并使用@Html.Raw()帮助程序每次都进行渲染。

@{
    string noRecordsTable = "<table><tr> ...";
}

然后在你的其他情况下:

@Html.Raw(noRecordsTable);

这是另一种选择。这样你就不会在剃刀中一遍又一遍地写表。

答案 1 :(得分:0)

这是我如何弄明白的。感谢Pluto建议grid.Table方法:

{
        var grid = new WebGrid(Page.DisplayData, rowsPerPage: 10, defaultSort: "UserName", canSort: true);
        var tfooter = (Page.IncompleteCount > 0 ? Page.IncompleteCount : "No") + " User Record" + (Page.IncompleteCount == 1 ? Page.IncompleteCount : "s") + " Found";

    @grid.Pager(mode: WebGridPagerModes.All, previousText: "previous", nextText: "next", numericLinksCount: 20)
    @grid.GetHtml(columns: grid.Columns(
        grid.Column("UserName", "User"),
        grid.Column("Column1", "Column 1", format: @<div><a href="@Href("~/thispage/", item.UserId)" target="_blank">@item.UserId</a></div>),
        grid.Column("Column2", "Column 2", format: @<div class="w400">@HH.TruncateString(@item.Column2, 125)</div>),
        grid.Column("Column3", "Column 3", format: @<div class="w200"> @HH.FmtDate(@item.Column3)</div>)),
            tableStyle: "simple", headerStyle: "hdr", rowStyle: "odd", alternatingRowStyle: "even", footer: @<div><b>@tfooter</b></div>)
}

我无法在grid.Table方法中使用mode,previousText和nextText属性。我找到了一个关于如何为这种方法添加分页的参考资料,我正在研究它。