asp.net,c#,图像按钮事件处理程序

时间:2015-02-09 10:00:12

标签: c# asp.net events imagebutton eventhandler

我的代码中有以下两个图像按钮事件处理程序,用于动态添加图像按钮,第一个在页面加载事件中调用,事件处理程序正在触发(虽然我知道我需要将它移出页面加载事件),第二个在预渲染事件中调用,单击按钮时不触发事件处理程序。 这是代码,第一个(工作):

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");
}

我假设它与对象有关,发件人没有正确传递,但我不知道如何在不加载页面加载的方法的情况下执行此操作我不想做的事件,因为这意味着按钮会在回发时消失。

非常感谢任何建议,谢谢大家!

1 个答案:

答案 0 :(得分:0)

您在第二个示例中的page life cycle中添加控件太晚了(OnPreRender)。 。在您需要访问会话的最糟糕情况下,请尝试PreInit作为MSDN推荐或InitLoad