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

时间:2011-05-05 01:25:06

标签: 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),我想显示表中的所有记录。

再次感谢!

1 个答案:

答案 0 :(得分:0)

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