【问题标题】:ASP.NET Gridview not showingASP.NET Gridview 未显示
【发布时间】:2013-12-08 15:03:00
【问题描述】:

好的,我确定问题出在后面的代码中,有些地方不对..

DAL 是一个从数据库中获取数据的类,它确实有效..

后面的代码:

using DALLib;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FinalProject
{
    public partial class בתים : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MaxRooms.Attributes.Add("onChange", "return OnSelectedIndexChange()");
                MinRooms.Attributes.Add("onChange", "return OnSelectedIndexChange()");

                Cities.DataSource = (DataTable)Application["CitiesTable"];
                Cities.DataTextField = "Name";
                Cities.DataBind();
            }

            DAL findHouses = new DAL(
                "SELECT * FROM Houses WHERE City='" + Cities.Text + "' AND Rooms BETWEEN '" + MinRooms.Text + "' AND '" + MaxRooms.Text + "'",
                ConfigurationManager.AppSettings["ConnectionString"],
                "Houses"
                );

            Application.Lock();
            Application["SearchResultsTable"] = findHouses.GetTable();
            Application.UnLock();

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            SearchResults.PageIndex = e.NewPageIndex;
            BindHousesToGrid();
        } 

        private void BindHousesToGrid()
        {
            SearchResults.DataSource = (DataTable)Application["SearchResultsTable"];
            SearchResults.DataBind();
        }

    }
}

表格:

<div>
    <asp:GridView ID="SearchResults" runat="server" CellPadding="4" GridLines="None" AutoGenerateColumns="False" ForeColor="#333333" AllowPaging="True">
        <AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="מספר נכס" />
            <asp:BoundField DataField="City" HeaderText="עיר" />
            <asp:BoundField DataField="Street" HeaderText="רחוב" />
            <asp:BoundField DataField="Rooms" HeaderText="חדרים" />
            <asp:BoundField DataField="size" HeaderText="גודל" />
            <asp:BoundField DataField="price" HeaderText="מחיר" />
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></FooterStyle>

        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></HeaderStyle>

        <PagerStyle HorizontalAlign="Center" BackColor="#FFCC66" ForeColor="#333333"></PagerStyle>

        <RowStyle BackColor="#FFFBD6" ForeColor="#333333"></RowStyle>

        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy"></SelectedRowStyle>

        <SortedAscendingCellStyle BackColor="#FDF5AC"></SortedAscendingCellStyle>

        <SortedAscendingHeaderStyle BackColor="#4D0000"></SortedAscendingHeaderStyle>

        <SortedDescendingCellStyle BackColor="#FCF6C0"></SortedDescendingCellStyle>

        <SortedDescendingHeaderStyle BackColor="#820000"></SortedDescendingHeaderStyle>
    </asp:GridView>
</div>

我的表单上的按钮还没有做任何事情,但首先我希望 gridview 显示初始值,这意味着整个 db 表.. 我做错了什么?

【问题讨论】:

    标签: c# asp.net gridview webforms


    【解决方案1】:

    网格未绑定到任何数据源。 添加到 Page_Load:

    BindHousesToGrid();
    

    我还将在 inside if(!IsPostBack) { } 块中使用此代码,这样您就不会在每次回发时检索相同的数据。

    【讨论】:

    • 请更新您的来源
    • 感谢您的回答,已解决!现在,当单击按钮时,我的 sql 查询出现了不同的问题。将打开一个新线程
    猜你喜欢
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2020-04-02
    • 2012-06-18
    • 1970-01-01
    • 2021-08-14
    • 2014-01-18
    • 1970-01-01
    相关资源
    最近更新 更多