Listview问题中的ImageButton

时间:2011-03-16 17:10:56

标签: c# asp.net data-binding c#-4.0 c#-3.0

我在ImageButton内有一个ListView。当用户单击该按钮时,它会执行操作,然后更改按钮使用的图像。在第二次选择按钮时,它应该恢复到之前的状态..

然而,这并没有发生。我可以让按钮第一次加载,但不能倒退。

以下代码位于OnItemDataBinding

 if (e.CommandName == "fave")
    {//save to favourites list - is logged in
        //http://www.avbuyer.com/dealers/Detailed.asp?Id=102&AId=25633&Add=true

        if (Session["MemberId"] != null && Session["MemberId"].ToString().Trim() != "")
        {
            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["AvBuyerConnectionString2"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand("saveFavourites", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@memberId", Session["MemberId"].ToString());
                    cmd.Parameters.AddWithValue("@aircraftId", e.CommandArgument);
                    cmd.Parameters.AddWithValue("@dateAdded", DateTime.Now);
                    conn.Open();
                    try
                    {//try saving
                        cmd.ExecuteNonQuery();
                        //update button
                        ((ImageButton)e.Item.FindControl("ibtnSave")).ImageUrl = "../_img/aircraftsales/acresults_savebt_remove.png";
                        ((ImageButton)e.Item.FindControl("ibtnSave")).ToolTip = "Remove from Favorites";
                        ((ImageButton)e.Item.FindControl("ibtnSave")).CommandName = "removefave";
                        //Response.Write("CLICKED: Fave");
                        //pnlGeneralSuccessExtender.Show();
                    }
                    catch { pnlGeneralErrorExtender.Show(); }
                }
            }
        }
        else
        {//not logged in or other problem
            //please log-in
            pnlSaveLogInExtender.Show();
        }
    }

    else if (e.CommandName == "removefave")
    {//remove listed aircraft from favourites
        if (Session["MemberId"] != null && Session["MemberId"].ToString().Trim() != "")
        {
            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["AvBuyerConnectionString2"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand("DELETE FROM MembersAircraftShortList WHERE MemberId=" + Session["MemberId"].ToString().Trim() + "AND AircraftId =" + e.CommandArgument, conn))
                {
                    conn.Open();
                    try
                    {
                        cmd.ExecuteNonQuery();
                        //pnlGeneralSuccessExtender.Show();
                        ((ImageButton)e.Item.FindControl("ibtnSave")).ImageUrl = "../_img/aircraftsales/acresults_savebt.png";
                        ((ImageButton)e.Item.FindControl("ibtnSave")).ToolTip = "Save to Favorites";
                        ((ImageButton)e.Item.FindControl("ibtnSave")).CommandName = "fave";
                        //lvResults.DataBind();
                    }
                    catch { pnlGeneralErrorExtender.Show(); }
                }
            }
        }
        else
        {//please log-in
            pnlSaveLogInExtender.Show();
        }
    }

ListView绑定了Page_PreRender

任何想法???

1 个答案:

答案 0 :(得分:0)

我的第一个想法是你为什么绑定Page_PreRender而不是让它绑定正常的Page_Load或事件。我的预感是PreRender中的绑定正在覆盖您在命令处理程序中所做的更改。

通常情况下,我认为流程是在Page_Load检查!PostBack进行初始绑定并且永远不再绑定(依赖于viewstate)进行绑定,因为您的命令正在进行更改你需要。如果发生另一个绑定,是不是要清除命令事件所做的更改?