Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net、c、图像按钮事件处理程序_C#_Asp.net_Events_Imagebutton_Eventhandler - Fatal编程技术网

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

C# asp.net、c、图像按钮事件处理程序,c#,asp.net,events,imagebutton,eventhandler,C#,Asp.net,Events,Imagebutton,Eventhandler,我的代码中有以下两个图像按钮事件处理程序,对于动态添加的图像按钮,第一个在页面加载事件中调用,事件处理程序正在启动,尽管我知道我需要将其移出页面加载事件,第二个在pre-render事件中调用,当单击该按钮时,事件处理程序不会触发。 以下是代码,第一个工作: protected void Page_Load(object sender, EventArgs e) { // check if user logged in if (Session["userID"] == null

我的代码中有以下两个图像按钮事件处理程序,对于动态添加的图像按钮,第一个在页面加载事件中调用,事件处理程序正在启动,尽管我知道我需要将其移出页面加载事件,第二个在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");
}
第二个不起作用:

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");
}
我假设这与对象和发送者没有被正确传递有关,但是我不知道如果没有在页面加载事件中调用该方法,该怎么做,我不想这样做,因为这意味着按钮在回发时消失


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

在第二个示例中的中,在PreRender上添加控件太晚了。在需要访问会话的最坏情况下,请尝试使用MSDN recomend或Init或Load。

对不起,我应该说得更清楚一些,我想找出的是,为什么在代码的第二部分中没有触发事件处理程序。谢谢谢谢你。imagebutton click事件在我在页面加载事件中添加控件时起作用,就像在代码的第一部分中一样,但我似乎无法在页面加载事件之外的任何位置添加控件,因为我似乎需要页面加载事件中的sender对象在我创建buttonclick事件时传递到buttonclick事件中。。。是否有方法从页面加载事件外部访问此发件人对象?再次感谢!据我所知,发送者在这三个事件中是相同的