我的代碼中有以下兩個圖像按鈕事件處理程序,對於動態添加的圖像按鈕,第一個在頁面加載事件中調用,事件處理程序正在觸發(雖然我知道我將需要將其移出頁面加載事件),第二個在預渲染事件中被調用,並且當點擊按鈕時事件處理程序不會觸發。 下面是代碼,第一個(工作):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("   "));
}
}
}
}
}
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("   "));
}
}
}
}
}
catch (Exception ex)
{
//error handling...
}
}
protected void anEvent_Click(object sender, EventArgs e, String eventid)
{
Session["eventID"] = eventid;
Server.Transfer("Event.aspx");
}
我假設它的東西做的對象,而不是傳遞發件人正確的,但我不知道如何做到這一點,沒有在頁面加載事件中調用的方法,我不想這樣做,因爲這意味着按鈕在回發消失。
任何意見將不勝感激,謝謝所有!
對不起,我應該說得更清楚,我想知道的是爲什麼事件處理程序沒有在第二部分代碼中觸發。謝謝! – Lamp 2015-02-09 10:01:31