2010-12-12 76 views
0

我正在Page_Load中运行一些代码,然后我将这段代码的结果存储在一些局部变量中。页面Page_Load事件与中继器ItemCommand

现在我用这个局部变量将它们分配到控制我的repeater项目模板

问题是>>页面不显示的项目模板,但没有必然it..I数据里面思考中继器无法加载分配给它的数据在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(); 

    } 

EDIT 2

我的SQL 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(); 

我最初的问题

How to use in my application a SELECT statement that must return more than one record to show multiple values for a certain field (m:m relation)

+0

你可以显示你实际上试图做一些代码。很难说明你到目前为止有什么。 – Phill 2010-12-12 03:10:38

+0

@ Phill-我添加了一些代码..好像我不需要所有这些,但我真的在while循环中对我的数据进行了一些过滤,以获取更多信息,您可以查看我以前的问题http://stackoverflow.com/问题/ 4419344 /如何在我的应用程序中使用一个选择语句 - 必须返回多于一个 - – lKashef 2010-12-12 03:20:17

回答

1

你不应该在手动DataReader的迭代都没有。这是一个只有前瞻性的工具。只有中继器您的while循环可能会遍历结果。我相信你的直接问题是你的while循环在Repeater呈现之前耗尽DataReader。

当您拨打DataBind()时,您正在指示中继器为您指定为其DataSource的集合中的每个项目呈现其模板。所以,任何过滤都需要在之前发生。理想情况下,您应该将该过滤逻辑作为where子句添加到您的SQL语句中。

你可以更具体地说明你正试图解决的实际问题吗?否则很难给你准确的建议。


更新:

记住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()); 
} 

通过使用数据表,而不是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()); 
+0

@Dave Ward-我知道这有点难以理解,但我会尽量让它尽可能清楚。这是我有权解决主要问题的最后一个问题http://stackoverflow.com/questions/4419344/how-to-use-in-my-application-a-select-statement-that-must-return-more -than-one-re 并在答案中检查新编辑的确切筛选逻辑和我正在使用的SELECT语句 – lKashef 2010-12-12 03:53:07

+0

根据对问题的更新进行更新。 – 2010-12-12 05:29:22

+0

@Dave Ward-这是一些非常好的信息,谢谢你,标注为答案,但我希望你能帮上忙。如果你已经熟悉了我的问题,我还有其他问题,我会添加我对更新答案的评论在另一个评论 – lKashef 2010-12-12 12:15:31

相关问题