繁体   English   中英

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

[英]asp.net, c#, Image Button Event Handler

我的代码中有以下两个图像按钮事件处理程序,对于动态添加的图像按钮,第一个在页面加载事件中调用,并且事件处理程序正在触发(尽管我知道我需要将其移出页面加载事件),则在pre render事件中调用第二个事件,并且单击按钮时事件处理程序不会触发。 这是代码,第一个(有效):

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

我假设这与对象和发送方未正确传递有关,但是我不知道如何在没有页面加载事件中调用方法的情况下执行此操作,而我不想这样做表示按钮在回发时消失。

任何建议将不胜感激,谢谢大家!

在第二个示例中,您在页面生命周期中添加控件的时间太晚了(OnPreRender)。 尝试PreInit为MSDN建议更换或InitLoad在您需要访问会话的最坏情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM