我想使用页面加载时绑定数据,写
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
}
但是运行以后页面上没有数据,什么都不显示于是我用手工的方法设计GridView1
以下是源代码: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" Height="287px" Width="765px" DataSourceID="ObjectDataSource1" AllowPaging="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" GridLines="Vertical" OnRowDeleted="GridView1_RowDeleted" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="DeptID" HeaderText="部门ID" SortExpression="DeptID" />
<asp:BoundField DataField="DeptName" HeaderText="部门名称" SortExpression="DeptName" />
<asp:BoundField DataField="Personnel" HeaderText="人员编制" SortExpression="Personnel" />
<asp:BoundField DataField="Governor" HeaderText="部门总监" SortExpression="Governor" />
<asp:BoundField DataField="Budget" HeaderText="经费预算" SortExpression="Budget" />
<asp:BoundField DataField="DeptDsc" HeaderText="部门详细描述" SortExpression="DeptDsc" />
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get_Dept_ALL"
TypeName="BLL.DeptBLL"></asp:ObjectDataSource>运行就是显示出来了,不知道是为什么,希望大家帮解答下,我刚学。
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
}
但是运行以后页面上没有数据,什么都不显示于是我用手工的方法设计GridView1
以下是源代码: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" Height="287px" Width="765px" DataSourceID="ObjectDataSource1" AllowPaging="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" GridLines="Vertical" OnRowDeleted="GridView1_RowDeleted" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="DeptID" HeaderText="部门ID" SortExpression="DeptID" />
<asp:BoundField DataField="DeptName" HeaderText="部门名称" SortExpression="DeptName" />
<asp:BoundField DataField="Personnel" HeaderText="人员编制" SortExpression="Personnel" />
<asp:BoundField DataField="Governor" HeaderText="部门总监" SortExpression="Governor" />
<asp:BoundField DataField="Budget" HeaderText="经费预算" SortExpression="Budget" />
<asp:BoundField DataField="DeptDsc" HeaderText="部门详细描述" SortExpression="DeptDsc" />
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get_Dept_ALL"
TypeName="BLL.DeptBLL"></asp:ObjectDataSource>运行就是显示出来了,不知道是为什么,希望大家帮解答下,我刚学。
this.GridView1.Databind();少这句
加这个this.GridView1.Databind();就好了
{
this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
this.GridView1.DataBind
}
();//web程序控件的数据绑定和winform程序的控件数据绑定要注意这个区别,控件指定数据源后一定要调用绑定方法
不過既然用到了ObjectDataSource,那么Page_Load里的this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL(); 完全没必要。
{
this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
GridView1.DataBind();
}
If(!IsPostBack)//这个不要忘了
{
this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
this.GridView1.DataBind
}
跟踪看看这个方法是不是有返回值。
this.GridView1.Databind();
最好能写在ispostBack里面,如下:
if(!isPostBack)
{
this.GridView1.DataSource = new DeptBLL().Get_Dept_ALL();
this.GridView1.Databind();
}要不然后面做事件时很容易报错