2015-02-09 42 views
1

我的代碼中有以下兩個圖像按鈕事件處理程序,對於動態添加的圖像按鈕,第一個在頁面加載事件中調用,事件處理程序正在觸發(雖然我知道我將需要將其移出頁面加載事件),第二個在預渲染事件中被調用,並且當點擊按鈕時事件處理程序不會觸發。 下面是代碼,第一個(工作):asp.net,c#,圖像按鈕事件處理程序

protected void Page_Load(object sender, EventArgs e) 
{ 
    // check if user logged in 
    if (Session["userID"] == null) 
     Server.Transfer("login.aspx"); 

    else 
    { 
     try 
     { 
      // connect to db and get event info for user events 
      using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["UsersConnectionString1"].ConnectionString)) 
      { 
       using (SqlCommand command = new SqlCommand()) 
       { 
        command.Connection = connection; 
        command.CommandType = System.Data.CommandType.StoredProcedure; 
        command.CommandText = "GetUserEvents"; 
        command.Parameters.AddWithValue("@UserID", Session["UserID"]); 
        connection.Open(); 
        using (SqlDataReader reader = command.ExecuteReader()) 
        { 
         while (reader.Read()) 
         { 
          //System.Diagnostics.Debug.Write(reader[1].ToString()); 

          ImageButton anEvent = new ImageButton(); 
          String eventid = reader[0].ToString(); 
          anEvent.ImageUrl = ""; 
          anEvent.Click += delegate(object sender2, ImageClickEventArgs e2) { anEvent_Click(sender, e, eventid); }; 

          anEvent.ToolTip = (reader[1].ToString()) + "\n" + (reader[2].ToString()) + "\n" + (reader[3].ToString()) + "\n\n"; 

          Panel3.Controls.Add(anEvent); 
          Panel3.Controls.Add(new LiteralControl("&nbsp &nbsp")); 
         } 
        } 
       } 
      } 
     } 

     catch (Exception ex) 
     { 
      //error handling... 
     } 
    } 
} 

protected void anEvent_Click(object sender, EventArgs e, string eventid) 
{ 
    // create session variable to identify event info for event page for specific event user clicks on 
    Session["eventID"] = eventid;  
    Server.Transfer("Event.aspx");  
} 

第二(不工作):

protected override void OnPreRender(EventArgs e) 
{ 
    UpdateNewsFeed(); 
    LoadUserEvents(); 
} 


private void LoadUserEvents() 
{ 
    try 
    { 
     // connect to db and get event info for user events 
     using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["UsersConnectionString1"].ConnectionString)) 
     { 
      using (SqlCommand command = new SqlCommand()) 
      { 
       command.Connection = connection; 
       command.CommandType = System.Data.CommandType.StoredProcedure; 
       command.CommandText = "GetUserEvents"; 
       command.Parameters.AddWithValue("@UserID", Session["UserID"]); 
       connection.Open(); 
       using (SqlDataReader reader = command.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         //System.Diagnostics.Debug.Write(reader[1].ToString()); 

         ImageButton anEvent = new ImageButton(); 
         String eventid = reader[0].ToString(); 
         anEvent.Click += delegate(object sender2, ImageClickEventArgs e2) { anEvent_Click(sender2, e2, eventid); }; 
         anEvent.ImageUrl = reader[4].ToString(); 
         anEvent.ToolTip = (reader[1].ToString()) + "\n" + (reader[2].ToString()) + "\n" + (reader[3].ToString()) + "\n\n"; 

         EventsPanel.Controls.Add(anEvent); 
         EventsPanel.Controls.Add(new LiteralControl("&nbsp &nbsp")); 
        } 
       } 
      } 
     } 
    } 

    catch (Exception ex) 
    { 
     //error handling... 
    } 
} 

protected void anEvent_Click(object sender, EventArgs e, String eventid) 
{ 
    Session["eventID"] = eventid; 
    Server.Transfer("Event.aspx"); 
} 

我假設它的東西做的對象,而不是傳遞發件人正確的,但我不知道如何做到這一點,沒有在頁面加載事件中調用的方法,我不想這樣做,因爲這意味着按鈕在回發消失。

任何意見將不勝感激,謝謝所有!

+0

對不起,我應該說得更清楚,我想知道的是爲什麼事件處理程序沒有在第二部分代碼中觸發。謝謝! – Lamp 2015-02-09 10:01:31

回答

0

您在第二個示例中的page life cycle中添加的控件太晚(OnPreRender)。 。嘗試PreInit作爲MSDN推薦或InitLoad在更糟糕的情況下,您需要訪問會話。

+0

謝謝jlvaquero。當我在頁面加載事件中添加控件時,像第一部分代碼那樣,imagebutton click事件起作用,但我似乎無法在頁面加載事件之外的任何地方添加控件,因爲我似乎需要從發件人對象頁面加載事件傳遞到buttonclick事件當我創建它...有沒有辦法從頁面加載事件之外訪問此發件人對象?再次感謝! – Lamp 2015-02-10 12:23:19

+0

據我所知,發件人在三個事件中是一樣的 – jlvaquero 2015-02-10 13:27:36