c# - 使用数据 ASP.NET C# 填充 Gridview EmptyDataTemplate

标签 c# asp.net gridview

我有一个 gridview,它显示的数据取决于文本框和按钮控件。由于 gridview 不显示任何内容(除非用户在文本框中键入输入),我如何才能用表格中的所有数据填充它?我正在考虑在 EmptyDataTemplate 中插入另一个 gridview,但是有什么方法可以在没有用户输入的情况下显示所有记录吗?

刚开始使用 ASP.NET,所以我真的需要你们的帮助。

提前致谢;)

这是我的代码示例:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="lenid" DataSourceID="returningDataSource" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:CommandField HeaderStyle-Width="120px" ButtonType="Button" ShowEditButton="True" ShowDeleteButton="True" />
            <asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN" 
                SortExpression="bookid" />
            <asp:BoundField DataField="booktitle" HeaderText="Title" 
                SortExpression="booktitle" />
            <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="department" HeaderText="Department" 
                SortExpression="department" />
            <asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed" 
                SortExpression="dateborrowed" />
            <asp:BoundField DataField="datereturned" HeaderText="Date returned" 
                SortExpression="datereturned" NullDisplayText="-- not yet returned --" />
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <EmptyDataTemplate>
            <asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
                AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
                DataKeyNames="lenid" DataSourceID="returningDataSource" ForeColor="#333333" 
                GridLines="None">
                <RowStyle BackColor="#EFF3FB" />
                <Columns>
                    <asp:CommandField HeaderStyle-Width="120" ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True" />
                    <asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN" 
                        SortExpression="bookid" />
                    <asp:BoundField DataField="booktitle" HeaderText="Title" 
                        SortExpression="booktitle" />
                    <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" 
                        SortExpression="EmployeeID" />
                    <asp:BoundField DataField="department" HeaderText="Department" 
                        SortExpression="department" />
                    <asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed" 
                        SortExpression="dateborrowed" />
                    <asp:BoundField DataField="datereturned" HeaderText="Date returned" 
                        SortExpression="datereturned" NullDisplayText="-- not yet returned --" />
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
            <asp:SqlDataSource ID="returningDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
                DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = @lenid" 
                InsertCommand="INSERT INTO [LendTable] ([bookid], [EmployeeID], [department], [dateborrowed], [datereturned]) VALUES (@bookid, @EmployeeID, @department, @dateborrowed, @datereturned)" 
                SelectCommand="SELECT dbo.LendTable.lenid, dbo.LendTable.bookid, dbo.LendTable.EmployeeID, dbo.LendTable.department, dbo.LendTable.dateborrowed, dbo.LendTable.datereturned, dbo.TblBooks.booktitle FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid" 

                UpdateCommand="UPDATE [LendTable] SET [bookid] = @bookid, [EmployeeID] = @EmployeeID, [department] = @department, [dateborrowed] = @dateborrowed, [datereturned] = @datereturned WHERE [lenid] = @lenid">
                <DeleteParameters>
                    <asp:Parameter Name="lenid" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="bookid" Type="Int64" />
                    <asp:Parameter Name="EmployeeID" Type="String" />
                    <asp:Parameter Name="department" Type="String" />
                    <asp:Parameter Name="dateborrowed" Type="DateTime" />
                    <asp:Parameter Name="datereturned" Type="DateTime" />
                    <asp:Parameter Name="lenid" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="bookid" Type="Int64" />
                    <asp:Parameter Name="EmployeeID" Type="String" />
                    <asp:Parameter Name="department" Type="String" />
                    <asp:Parameter Name="dateborrowed" Type="DateTime" />
                    <asp:Parameter Name="datereturned" Type="DateTime" />
                </InsertParameters>
            </asp:SqlDataSource>
        </EmptyDataTemplate>
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

如果用户没有键入任何输入(或用数据库中的记录填充 EmptyDataTemplate),我想显示表中的所有记录。

再次感谢!

最佳答案

执行返回数据源的 Select 命令,因为它会返回表格的整行并绑定(bind)到 page_load 的 (!IsPostBack) 中的原始网格,下次需要基于文本框绑定(bind)数据时输入执行查询并将数据绑定(bind)到同一个网格。

关于c# - 使用数据 ASP.NET C# 填充 Gridview EmptyDataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891691/

相关文章:

c# - 使用 AutoMapper 将字符串映射到枚举

c# - 为什么 Resources.Load 在没有 systemTypeInstance 的情况下不起作用

asp.net - 表单例份验证票是否足够安全?

c# - 使用 GridView1_SelectedIndexChanged 时出错

asp.net - 如何使用 jQuery 设置表格单元格的背景颜色

c# - 用 MVC3 创建的第二个数据库,如何防止?

c# - Button 的 ControlTemplate 的 ContentPresenter 的 Textblock 的前景没有改变

c# - 使用 CodeBehind 中的数据填充 GridView 中模板字段内的 DropDownList

javascript - Asp .Net Mvc Bundle错误的脚本路径

javascript - "Floating"Gridview标题