显示“未找到记录”;在webgrid c#网页

本文关键字:webgrid 网页 未找到记录 记录 显示 | 更新日期: 2023-09-27 18:11:09

是否有一种更容易的方法来显示"未找到记录"在一个空的webgrid?下面是我的代码,为我工作。但是,我不想在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 #,以便正确显示"No records found"的消息(居中对齐)是很繁琐的。有更简单的方法吗?谢谢。

显示“未找到记录”;在webgrid c#网页

当您没有记录时,您可以像这样显示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> ...";
}

然后在else条件中:

@Html.Raw(noRecordsTable);

那是另一种选择。这样你就不用在Razor里一遍又一遍地写表格了。

我是这样想的。感谢冥王星的建议。表法:

{
        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>)
}

我不能在网格中使用mode, preoustext和nextText属性。表的方法。我找到了一个关于如何在这个方法中添加分页的参考,我正在研究它。