【问题标题】:C# Custom GridViewC# 自定义网格视图
【发布时间】:2018-02-17 07:54:28
【问题描述】:

我不是很精通asp.net。我有一个基于 asp 的 Web 应用程序,我想创建一个自定义 GridView 以便在我有搜索框时使用它并减少代码中的冗余。

我希望在我的textbox 下方有这个GridView,并且在更改GridView 的文本上显示大部分搜索结果和一个用于高级搜索的“更多”按钮,这将打开一个新页面。谁能帮助我如何开始?

谢谢。

【问题讨论】:

标签: c# asp.net caching search gridview


【解决方案1】:

这里有一个小例子来说明如何实现这一点。首先将搜索所需的必要项目添加到 aspx 页面。请注意,这些按钮有一个OnCommand,以便您可以随它们一起发送CommandName

<asp:TextBox ID="SearchField" runat="server" MaxLength="50"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ErrorMessage="A search term is required" ValidationGroup="Search" 
    ControlToValidate="SearchField">
</asp:RequiredFieldValidator>

<asp:Button ID="SearchButton" runat="server" Text="Search" 
    OnCommand="DoSearch_Command" CommandName="Search" ValidationGroup="Search" />

<asp:GridView ID="SearchResultsGridView" runat="server" AutoGenerateColumns="true"></asp:GridView>

<asp:Button ID="MoreButton" runat="server" Text="More" 
    OnCommand="DoSearch_Command" CommandName="More"/>

现在在您后面的代码中处理SearchMore 的按钮点击。我使用List 创建了一些虚拟数据,但您需要将其替换为包含搜索结果的正确数据源(列表、数据表等)。

protected void DoSearch_Command(object sender, CommandEventArgs e)
{
    //create a new item to hold search results, in this case a list
    List<string> searchResults = new List<string>();

    //the text from the textbox that contains the search word
    string searchTerm = SearchField.Text.Trim();

    //hide the 'more' button
    MoreButton.Visible = false;

    //add some dummy data for testing
    for (int i = 1; i <= 50; i++)
    {
        searchResults.Add("Search result " + i);
    }

    //if the results are more than 10 and the click is not from the 'more' button take 10 items
    if (searchResults.Count > 10 && e.CommandName == "Search")
    {
        searchResults = searchResults.Take(10).ToList();

        //show the more button
        MoreButton.Visible = true;
    }

    //show results in gridview
    SearchResultsGridView.DataSource = searchResults;
    SearchResultsGridView.DataBind();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多