【发布时间】:2015-04-09 00:48:21
【问题描述】:
我的代码中有以下两个图像按钮事件处理程序,对于动态添加的图像按钮,第一个在页面加载事件中调用并且事件处理程序正在触发(尽管我知道我需要将其移出页面加载事件),第二个在预渲染事件中调用,并且在单击按钮时不会触发事件处理程序。 这是代码,第一个(工作):
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");
}
我假设这与对象有关,并且发送者未正确传递,但如果没有在页面加载事件中调用的方法,我不知道该怎么做这样做意味着按钮在回发时消失。
任何建议将不胜感激,谢谢大家!
【问题讨论】:
-
对不起,我应该说得更清楚一点,我试图找出的是为什么事件处理程序没有在第二部分代码中触发。谢谢!
标签: c# asp.net events imagebutton eventhandler