开发者

ImageButton within Listview problem

I've have an ImageButton within a ListView. When a user clicks the button it performs an operation then changes the image used by the button. On selecting the button a second time, it should revert back to its previous state..

However this isn't happening. I can get the button to load the first time/way but not backwards.

The following code is located in the 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
开发者_StackOverflow社区        {//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();
        }
    }

The ListView is bound in the Page_PreRender.

Any ideas???


My first thought is why are you binding on Page_PreRender and not just letting it bind through the normal Page_Load or event. My hunch is that the binding in the PreRender is overwritting the changes you are making in the command handler.

Normally I would think the flow would be to do the binding in the Page_Load checking for !PostBack to do the inital binding and never bind again (relying on viewstate) since your commands are making the changes you require. If another bind happens, won't it be clearing out the changes made by your command event?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜