簡體   English   中英

頁面Page_Load事件與Repeater ItemCommand

[英]The Page Page_Load Event Vs The Repeater ItemCommand

我在Page_Load中運行一些代碼,然后將這些代碼的結果存儲在一些局部變量中。

現在,我使用此局部變量將其分配給中繼器項目模板內的控件

問題是>>該頁面未顯示項目模板,但沒有任何數據綁定。.我認為中繼器無法在Page_Load中加載分配給它的數據,並且它已初始化-與生命周期相關的問題-

知道到底是什么問題嗎? 以及如何解決呢?

編輯

一些示例代碼:

public partial class MyPage: System.Web.UI.Page
    {

        int UserId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);

            SqlCommand comm = new SqlCommand("SELECT * From Users, conn);
            SqlDataReader reader;

            conn.Open();
            reader = comm.ExecuteReader();

//I'm not sure if I need those two lines:
            AllBrandsRptr.DataSource = reader;
            AllBrandsRptr.DataBind();

            while (reader.Read())
            {

                    UserId = (int)reader["UserId"];
            }
                conn.Close();

            }
        }


    protected void AllBrandsRptr_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        Label LabelTest = (Label)e.Item.FindControl("MyTestLabel");
        LabelTest.Text = UserId.ToString();

    }

編輯2

我的SELECT語句

string command1 = "SELECT Brands.BrandId, Brands.BrandName, Brands.BrandLogo, Brands.BrandWebsite, Brands.IsBrandVisible, Cuisines.CuisineType, VenueTypes.VenueTypeName FROM         Brands FULL OUTER JOIN BrandCuisines ON BrandCuisines.BrandId = Brands.BrandId FULL OUTER JOIN Cuisines ON Cuisines.CuisineId = BrandCuisines.CuisineId FULL OUTER JOIN BrandVenueTypes ON BrandVenueTypes.BrandId = Brands.BrandId FULL OUTER JOIN VenueTypes ON VenueTypes.VenueTypeId = BrandVenueTypes.VenueTypeId";

我的過濾代碼

            conn.Open();
            reader = comm.ExecuteReader();
            AllBrandsRptr.DataSource = reader;
            AllBrandsRptr.DataBind();

            while (reader.Read())
            {

                  if (((int)reader["BrandId"]) == BrandId) //this line to pass collecting some info, if I already iterated through the same Id 
                  {
                    BrandId = (int)reader["BrandId"];
                    BrandName = (string)reader["BrandName"];
                    BrandLogo = (string)reader["BrandLogo"];
                    BrandWebsite = (string)reader["BrandWebsite"];
                    IsVisible = (bool)reader["IsBrandVisible"];
                  }

                if (reader["CuisineType"] != DBNull.Value)
                {
                    Cuisines += (string)reader["CuisineType"];
                }

                if (reader["VenueTypeName"] != DBNull.Value)
                {
                    VenueTypes += ", " + (string)reader["VenueTypeName"];
                }

                conn.Close();

我最初的問題

如何在我的應用程序中使用SELECT語句,該語句必須返回多個記錄以顯示特定字段的多個值(m:m關系)

您根本不應該手動遍歷DataReader。 它是僅前進的工具。 只有Repeater while循環可以迭代結果。 我認為您的直接問題是,您的while循環在呈現Repeater之前耗盡了DataReader。

調用DataBind() ,您正在指示Repeater為您分配為其DataSource的集合中的每個項目呈現其模板。 因此,任何過濾都需要先進行。 理想情況下,您可能應該將該過濾邏輯添加為SQL語句的where子句。

您能否更具體地說明您要解決的實際問題? 否則很難給您准確的建議。


更新:

請記住, while(reader.Read())不能像事件處理程序那樣工作,或者與它在語義上的發音類似。 換句話說,您並不是在讀取DataReader時告訴程序執行某些操作,而是在告訴它立即開始讀取數據(與Repeater無關)。

我建議您嘗試以下操作:

string sql = "Your current SQL here";
string connectionString = "Your connection string here";

// The using block ensures that the connection will be closed and freed up,
//  even if an unhandled exception occurs inside the block.
using (SqlConnection conn = new SqlConnection(connectionString))
{
  SqlCommand cmd = new SqlCommand(sql, conn);

  SqlDataAdapter da = new SqlDataAdapter(cmd);

  DataTable dt = new DataTable();

  da.Fill(dt);

  AllBrandsRptr.DataSource = dt;
  AllBrandsRptr.DataBind();

  var cuisineTypes = from row in dt.AsEnumerable()
                     select row["CuisineType"];

  string cuisines = string.Join(", ", cuisineTypes.Distinct());

  var venueTypes = from row in dt.AsEnumerable()
                   select row["VenueTypeName"];

  string venues = string.Join(", ", venueTypes.Distinct());
}

通過使用DataTable而不是DataReader,您可以根據需要多次迭代數據。 如果只需要對數據進行一次僅前向讀取, DataReader的性能會更高,但是DataTable的功能要強大得多,並且在這些情況下,將超出DataReader的功能。


值得一提的是,如果您長期關心此項目並希望使其可維護,則應考慮最終將某些數據訪問代碼移至單獨的層。 您應該努力不要在.aspx.cs文件中包含SQL語句或直接數據訪問代碼。

然后,您的Page_Load中的代碼可以被強類型化並更易於使用:

List<Brand> brands = Brand.GetAllBrands();

AllBrandsRptr.DataSource = brands;
AllBrandsRptr.DataBind();

var cuisineTypes = from brand in brands
                   select brand.CuisineType;

string cuisines = string.join(", ", cuisineTypes.Distinct());

var venueTypes = from brand in brands
                 select brand.VenueType;

string venues = string.join(", ", venueTypes.Distinct());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM