【问题标题】:asp.net, c#, Image Button Event Handlerasp.net, c#, 图片按钮事件处理程序
【发布时间】: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("&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");
}

我假设这与对象有关,并且发送者未正确传递,但如果没有在页面加载事件中调用的方法,我不知道该怎么做这样做意味着按钮在回发时消失。

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

【问题讨论】:

  • 对不起,我应该说得更清楚一点,我试图找出的是为什么事件处理程序没有在第二部分代码中触发。谢谢!

标签: c# asp.net events imagebutton eventhandler


【解决方案1】:

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

【讨论】:

  • 感谢 jlvaquero。当我在页面加载事件中添加控件时,imagebutton单击事件起作用,就像在代码的第一部分一样,但我似乎无法在页面加载事件之外的任何地方添加控件,因为我似乎需要来自的发送者对象页面加载事件在我创建它时传递给 buttonclick 事件......有没有办法从页面加载事件之外访问这个发送者对象?再次感谢!
  • 据我所知,三个事件的发送者是一样的
猜你喜欢
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多