簡體   English   中英

將按鈕點擊次數存儲為asp.net(c#)

[英]store the number of button clicks asp.net (c#)

我為用戶提供了下載由提供商上傳的圖像的選項。 單擊下載按鈕后,圖像將下載。 但我也想存儲單擊該下載按鈕的次數。 為此,我鍵入了此代碼,但是它不起作用。

我嘗試了幾種方法,例如,聲明一個int變量並將其遞增。 沒用 然后我嘗試了這個。 還是行不通。 我究竟做錯了什么?

 protected void btn_download_Click(object sender, EventArgs e)
        {


            var res = (byte[])Session["Image"];

           Response.Clear();
            MemoryStream ms = new MemoryStream(res);
            Response.ContentType = "image/jpeg";
            Response.AddHeader("content-disposition", "attachment;filename=Image.jpg");
            Response.Buffer = true;
            ms.WriteTo(Response.OutputStream);
            Response.End();



            try
            {

                SqlCommand command = new SqlCommand("UPDATE [ImageWithTags] SET DownloadCount = DownloadCount + 1 WHERE ImageID='" + DropDownList1.SelectedItem.Text + "'", con);
                con.Open(); 
                command.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }




        }

您也可以為此創建一個會話變量,並進行如下計算:

protected void btn_download_Click(object sender, EventArgs e)
{
    int counter=0;    
    if(Session["counter"]!=null)
     {
        Session["counter"] = ++Convert.ToInt32(Session["counter"]);
        counter = Convert.ToInt32Session["counter"];
     }
    else
      Session["counter"] = 0;
        // do your calculation here with variable counter
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM