我在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
。
任何想法???
答案 0 :(得分:0)
我的第一个想法是你为什么绑定Page_PreRender
而不是让它绑定正常的Page_Load
或事件。我的预感是PreRender中的绑定正在覆盖您在命令处理程序中所做的更改。
通常情况下,我认为流程是在Page_Load
检查!PostBack
进行初始绑定并且永远不再绑定(依赖于viewstate)进行绑定,因为您的命令正在进行更改你需要。如果发生另一个绑定,是不是要清除命令事件所做的更改?