如何在加载页面时显示网格视图数据

时间:2014-04-28 13:37:10

标签: asp.net c#-4.0

我有一个gridview,它有sql源并显示一些信息。除此之外,我有一个文本框和搜索按钮来控制网格视图中显示的内容。

我需要知道如何在页面加载时同时显示文本框控制器和gridview。

目前,只有搜索框被显示,并且在满足某些搜索条件后,网格视图数据才会被显示。

这是我的标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdateDivision.aspx.cs" Inherits="Attendance_UpdateDivision" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    </head>
<body>
    <form id="form1" runat="server">
    <div dir="ltr">

        &nbsp;Search Division&nbsp;
        <asp:TextBox ID="txtDivision" runat="server" style="margin-left: 0px" 
            Width="198px"></asp:TextBox>
&nbsp;
        <asp:Button ID="btnSearch" runat="server" Text="Search" Width="61px" 
            onclick="btnSearch_Click" style="height: 26px" />
&nbsp;&nbsp;&nbsp;

        <asp:GridView ID="gvUpdateDivision" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
            DataKeyNames="DivisionId" DataSourceID="SqlDataSource1" ForeColor="#333333" 
            GridLines="None" ShowFooter="True" Width="448px">
            <RowStyle BackColor="#EFF3FB"/>                 
            <Columns>            
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="DivisionName" HeaderText="Search DivisionName" 
                SortExpression="DivisionName"/>
          <asp:TemplateField>
          <HeaderTemplate>

          </HeaderTemplate>
          </asp:TemplateField>          
            </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="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ATCF %>" 
            DeleteCommand="DELETE FROM [tblDivisions] WHERE [DivisionId] = @DivisionId" 
            InsertCommand="INSERT INTO [tblDivisions] ([DivisionName]) VALUES (@DivisionName)" 
            SelectCommand="SELECT * FROM [tblDivisions] WHERE ([DivisionName] = @DivisionName)" 



            UpdateCommand="UPDATE [tblDivisions] SET [DivisionName] = @DivisionName WHERE [DivisionId] = @DivisionId">
            <SelectParameters>
                <asp:ControlParameter ControlID="txtDivision" Name="DivisionName" 
                    PropertyName="Text" Type="String" />
            </SelectParameters>
            <DeleteParameters>
                <asp:Parameter Name="DivisionId" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="DivisionName" Type="String" />
                <asp:Parameter Name="DivisionId" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="DivisionName" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        <br />

    </div>
    </form>
</body>
</html>

代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Attendance_UpdateDivision : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        gvUpdateDivision.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        gvUpdateDivision.Visible = true;
    }
}

请快速支持

0 个答案:

没有答案