<asp:ImageButton>

时间:2019-12-27 10:13:37

标签: c# asp.net webforms

我试图在我的项目中添加一个愿望清单选项,我遵循了一个在线教程来实现它,并且它的工作效果还不错。我正在使用中继器控件来通过图像按钮显示数据。在此图像按钮上单击,应将该书添加到愿望清单中,并且正在将数据存储在数据库中,但是在数据库中存储了该数据之后,希望按钮图像更改以向用户显示已添加的图像。这是我的图片按钮以及所有隐藏代码:

<asp:ImageButton ID="addfavourite" runat="server" CommandArgument='<%# Eval("bookId")%>' CommandName="addtofavbooks" Height="25" /> 


 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.SqlClient;
 using System.Web.UI.WebControls;

 namespace EReader.EReader
 {
  public partial class Books : System.Web.UI.Page
  {
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\EReader.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true");
    private int PageSize = 9;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        if (!IsPostBack)
        {
            this.GetCustomersPageWise(1);
        }

        if (Request.QueryString["addtofavbooks"] != null && Session["LoggedIn"] != null)
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into Favourite (username,bookId) values('" + Session["LoggedIn"].ToString() + "'," + Request.QueryString["addtofavbooks"].ToString() + ")";
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Product Added in Wishlist');</script>");
        }

        if (Session["LoggedIn"] == null)
        {
            if (Request.QueryString["addtofavbooks"] != null)
            {
                Response.Write("<script>alert('Login to Your Account First. No Product Added in WishList');</script>");
            }
        }
        else
        {
            fullnametop.Text = Session["LoggedIn"].ToString();
        }
    }
    protected void books_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        if (Session["LoggedIn"] != null)
        {
            Label bookid = e.Item.FindControl("bookid") as Label;
            ImageButton addfavourite = e.Item.FindControl("addfavourite") as ImageButton;
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Favourite where bookId=@bookid and username=@username";
            cmd.Parameters.AddWithValue("@bookid", bookid.Text);
            cmd.Parameters.AddWithValue("@username", Session["LoggedIn"]);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                addfavourite.ImageUrl = "/app-assets/images/liked.svg";
                addfavourite.Enabled = true;
            }
            else
            {
                addfavourite.ImageUrl = "/app-assets/images/like.svg";
            }
        }
        else
        {
            ImageButton addfavourite = e.Item.FindControl("addfavourite") as ImageButton;
            addfavourite.ImageUrl = "/app-assets/images/like.svg";
        }

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Web.UI.HtmlControls.HtmlContainerControl edit = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("edit");
            System.Web.UI.HtmlControls.HtmlContainerControl delete = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("delete");
            if (Session["LoggedIn"] == null || Session["role"].Equals("User"))
            {
                edit.Visible = false;
                delete.Visible = false;
            }
            else
            {
                edit.Visible = true;
                delete.Visible = true;
            }
        }
    }
    protected void books_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Response.Redirect("Books.aspx?addtofavbooks=" + e.CommandArgument.ToString());
    }

    private void GetCustomersPageWise(int pageIndex)
    {
        using (SqlCommand cmd = new SqlCommand("GetBooksPageWise", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
            cmd.Parameters.AddWithValue("@PageSize", PageSize);
            cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
            cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
            IDataReader idr = cmd.ExecuteReader();
            books.DataSource = idr;
            books.DataBind();
            idr.Close();
            int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
            this.PopulatePager(recordCount, pageIndex);
        }
    }
    private void PopulatePager(int recordCount, int currentPage)
    {
        double dbPageCount = (double)((decimal)recordCount / Convert.ToDecimal(PageSize));
        int pageCount = (int)Math.Ceiling(dbPageCount);
        List<ListItem> pages = new List<ListItem>();
        if (pageCount > 0)
        {
            for (int i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }
            if (currentPage == 1)
            {
                prev.Visible = false;
            }
            else
            {
                prev.CommandArgument = (currentPage - 1).ToString();
                prev.Visible = true;
            }
            if (currentPage >= dbPageCount)
            {
                next.Visible = false;
            }
            else
            {
                next.CommandArgument = (currentPage + 1).ToString();
                next.Visible = true;
            }
        }
        pager.DataSource = pages;
        pager.DataBind();
    }
    protected void Page_Changed(object sender, EventArgs e)
    {
        int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
        this.GetCustomersPageWise(pageIndex);
    }
 }
}

在这种情况下,您可以单击图像按钮并且该书不在心愿单表中,然后它将显示like.svg图像,但是如果它在某些用户的心愿单中,则应该显示liked.svg,但是当我运行时该项目并将一本书添加到愿望清单,它仍然显示like.svg相同的图像。 请在可能的情况下帮助我。

0 个答案:

没有答案