使用.NET Cookie代码显示动态图像 [英] Showing the dynamic Images using .NET Cookies code

查看:139
本文介绍了使用.NET Cookie代码显示动态图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery和C#在页面上显示幻灯片图片。 JQuery来自所有的图像从C#代码加载。



我有下面的三个图像在页面上使用c#动态生成。

 < img id =image1title =享受43000英尺的清凉淋浴alt =享受43000英尺的清凉淋浴src = /english/images/spa.jpgstyle =display:block;> 

< img id =image2title =你自己的私人套房的舒适度alt =你自己的私人套房的舒适度src =/ english / images / suites.jpg style =display:block;>

< img id =image3title =社交,放松和放纵alt =社交,放松,放纵src =/ english / images / lounge.jpgstyle = display:block;>

现在我想编写C#代码,它将读取上述图片,并将它们存储在Cookie中。



因此,如果我看到第一张图片,那么在下次访问该页面时,它会显示第二张图片等等。在显示最后一个图像后,它将首先开始显示。基本上我想实现下面的逻辑。

 每个图像将按顺序显示,并且一个cookie将维护哪个图像应该显示但是当用户登录时,图像不应该改变。同样,如果任何一个图像被改变,cookie将被清除。 

请指导我如何开始,如果我可以有一些示例代码,
$ b

感谢!



.NET CODE FOR GENERATING THE IMAGE AND INPUT HIDDEN:

  protected覆盖void CreateChildControls()
{

//始终以干净的形式开始
Controls.Clear();

//声明图像列表的列表
List< string> imageList = new List< string>();

//声明Collection对象以添加图像列表
NameValueCollection cookiecollection = new NameValueCollection();

int cntShow = 0;
//为prev和next添加DIV4
面板div4 = new Panel();
//添加输入隐藏以从控件获取所有值
HtmlGenericControl inputHidden = new HtmlGenericControl(input);
foreach(base.FeaturedPromos中的FeaturedPromo促销)
{
inputHidden = new HtmlGenericControl(input);
inputHidden.Attributes [id] = promo.ID +Image;
inputHidden.Attributes [type] =hidden;
inputHidden.Attributes [src] = promo.ImageSource;
inputHidden.Attributes [alt] = promo.ImageAlt;
inputHidden.Attributes [title] = promo.ImageTitle;
inputHidden.Attributes [href] = promo.ImageHref;
inputHidden.Attributes [height] = promo.ImageHeight;
inputHidden.Attributes [width] = promo.ImageWidth;
inputHidden.Attributes [header] = promo.ImageHeader;
inputHidden.Attributes [subheader] = promo.ImageSubHeader;
inputHidden.Attributes [color] = promo.ImageColor;

this.Controls.Add(inputHidden);

imageList.Add(promo.ID +Image);

if(cntShow == 0)
{
//为特色推广添加特定div
面板div1 = new Panel();
div1.Attributes [id] = promo.ID;

if(cntShow == 0)
{
div1.Style [display] =block;
}
else
{
div1.Style [display] =无;
}

//添加图片
HtmlGenericControl image = new HtmlGenericControl(image);
image.Attributes [src] = promo.ImageSource;
image.Attributes [alt] = promo.ImageAlt;
image.Attributes [title] = promo.ImageTitle;

div1.Controls.Add(image);

//为导航添加两个HREF
HtmlGenericControl alinkLeft = new HtmlGenericControl(a);
alinkLeft.Attributes [class] =slideshow-control-left;
alinkLeft.Attributes [href] =javascript:void(0);;
alinkLeft.Style [display] =inline;
div1.Controls.Add(alinkLeft);

HtmlGenericControl alinkRight = new HtmlGenericControl(a);
alinkRight.Attributes [class] =slideshow-control-right;
alinkRight.Attributes [href] =javascript:void(0);;
alinkRight.Style [display] =inline;
div1.Controls.Add(alinkRight);

//添加第二个div
面板div2 = new Panel();
div2.CssClass =slideshow-b;
div1.Controls.Add(div2);

//添加第三个div
面板div3 = new Panel();
div3.CssClass =slideshow-bl;
div2.Controls.Add(div3);

//添加A HREF链接
HtmlGenericControl alink = new HtmlGenericControl(a);
alink.Attributes [class] =slideshow-link;
alink.Attributes [href] = promo.ImageHref;
div3.Controls.Add(alink);

//添加第一个span
HtmlGenericControl span1 = new HtmlGenericControl(span);
span1.Attributes [class] =slideshow-header;
span1.InnerHtml = promo.ImageHeader;
alink.Controls.Add(span1);

//添加换行符
alink.Controls.Add(new LiteralControl(< br />));

//添加第二个span
HtmlGenericControl span2 = new HtmlGenericControl(span);
span2.Attributes [class] =slideshow-subheader;
span2.InnerHtml = promo.ImageSubHeader;
alink.Controls.Add(span2);
this.Controls.Add(div1);

if(base.FeaturedPromos.Count> 1)
{
//为prev和next添加DIV4
div4 = new Panel();
div4.CssClass =slideshow-br;

//在DIV4中添加DIV5
面板div5 = new Panel();
div5.CssClass =slideshow-br-controls;
div4.Controls.Add(div5);

//添加PREV A HREF链接
HtmlGenericControl alinkPrev = new HtmlGenericControl(a);
alinkPrev.Attributes [class] =slideshow-br-controls-left;
alinkPrev.Attributes [href] =javascript:void(0);;
alinkPrev.Attributes [title] =上一页
alinkPrev.Style [display] =inline;
alinkPrev.Attributes [CurrentDivID] = promo.ID;
div5.Controls.Add(alinkPrev);

//添加上一个和下一个按钮的跨度
HtmlGenericControl span3 = new HtmlGenericControl(span);
span3.Attributes [class] =slideshow-br-control-buttons;
int count = 0;
foreach(specialPromo allPromo in base.FeaturedPromos)
{
if(count == 0)
{
//为Prev和Next $添加所有HREF链接b $ b HtmlGenericControl aLLlinks = new HtmlGenericControl(a);
aLLlinks.Attributes [class] =+ count +-banner-button;
aLLlinks.Attributes [href] =javascript:void(0);;
aLLlinks.Attributes [title] = allPromo.ImageTitle;
aLLlinks.Style [display] =inline;

//aLLlinks.Attributes[ \"idid] =active-banner-slide;

span3.Controls.Add(aLLlinks);
count ++;
}
}

div5.Controls.Add(span3);

//添加NEXT A HREF链接
HtmlGenericControl alinkNext = new HtmlGenericControl(a);
alinkNext.Attributes [class] =slideshow-br-controls-right;
alinkNext.Attributes [href] =javascript:void(0);;
alinkNext.Attributes [title] =下一步
alinkNext.Style [display] =inline;
alinkNext.Attributes [CurrentDivID] = promo.ID;
div5.Controls.Add(alinkNext);
//div2.Controls.Add(div4);
}
this.Controls.Add(div1);
//this.Controls.Add(div4);
cntShow ++;
}
}
this.Controls.Add(div4);

HttpCookie cookielist = new HttpCookie(ImageListOfCookies);

for(int i = 0; i {
cookiecollection.Add(Image_+ i,imageList [i]);
}
cookielist.Values.Add(cookiecollection);
HttpContext.Current.Response.Cookies.Add(cookielist);


}


解决方案

如果你想用Cookies完成它,因为你是你自己首先显示的图像,所以你知道哪些图像将显示,所以,而不是存储链接到图像(可以很长),这样做:




  • 为<$ c $分配每张图片的 1 c> spa.jpg , 2 for suites.jpg


  • 然后,当您显示图片时,将ID附加到这样的Cookie中(我已经使用|




  //例如,如果spa镜像已存储,存储spa
if(Response.Cookies [Shown_Images]!= null)
{
Response.Cookies [Shown_Images]。Value = Response.Cookies [Shown_Images] +| spa;
}
else
{
Response.Cookies [Shown_Images]。Value =spa;
}

然后如果 spa.jpg 已显示

  if(Response.Cookies [Shown_Images]!= null)
{
string [] images_shown = Response.Cookies [Shown_Images];
if(images_shown.Contains(spa))
{
//如果为True,则处理;
}
else
{
// False then do as required
}
}


I am using jQuery and C# to display the slide Images on the page. JQuery comes when all the images are loaded from C# code.

I have got below three images on the page which are generated dynamically using c#.

<img id="image1" title="Enjoy a refreshing shower at 43,000 feet" alt="Enjoy a refreshing shower at 43,000 feet" src="/english/images/spa.jpg" style="display: block;">

<img id="image2"  title="The comfort of your own Private Suite" alt="The comfort of your own Private Suite" src="/english/images/suites.jpg" style="display: block;">

<img id="image3"  title="Socialize, relax, and indulge" alt="Socialize, relax, and indulge" src="/english/images/lounge.jpg" style="display: block;">

Now I want to write C# code, which will read above images and will store them in cookies. And according to the cookies value the images will be shown.

So if I have seen the first Image, then on next visit of the page, it will show the second image and so on. And after the last images shown, it will start showing first. Basically I am looking to implement below logic.

"Each image will show in order and a cookie will maintain which image should be shown next. But the image should not change when the user logs in. Also the cookie will be cleared in case any one of the images are changed."

Please guide me how to start, and would appreciate if I can have some sample code

Thanks!

.NET CODE FOR GENERATING THE IMAGE AND INPUT HIDDEN:

protected override void CreateChildControls()
        {

            // Always start with a clean form
            Controls.Clear();

            //Declaring the List for image list
            List<string> imageList = new List<string>();

            //Declaring the Collection object to add the image list
            NameValueCollection cookiecollection = new NameValueCollection();           

            int cntShow = 0;
            //Adding DIV4 for prev and next
            Panel div4 = new Panel();
            //Adding Input Hidden to get all the values from control
            HtmlGenericControl inputHidden = new HtmlGenericControl("input");
            foreach (FeaturedPromo promo in base.FeaturedPromos)
            {
                inputHidden = new HtmlGenericControl("input");
                inputHidden.Attributes["id"] = promo.ID + "Image";
                inputHidden.Attributes["type"] = "hidden";
                inputHidden.Attributes["src"] = promo.ImageSource;
                inputHidden.Attributes["alt"] = promo.ImageAlt;
                inputHidden.Attributes["title"] = promo.ImageTitle;
                inputHidden.Attributes["href"] = promo.ImageHref;
                inputHidden.Attributes["height"] = promo.ImageHeight;
                inputHidden.Attributes["width"] = promo.ImageWidth;
                inputHidden.Attributes["header"] = promo.ImageHeader;
                inputHidden.Attributes["subheader"] = promo.ImageSubHeader;
                inputHidden.Attributes["color"] = promo.ImageColor;

                this.Controls.Add(inputHidden);

                imageList.Add(promo.ID + "Image");

                if (cntShow == 0)
                {
                    //Add specific div for Featured Promo
                    Panel div1 = new Panel();
                    div1.Attributes["id"] = promo.ID;

                    if (cntShow == 0)
                    {
                        div1.Style["display"] = "block";
                    }
                    else
                    {
                        div1.Style["display"] = "none";
                    }

                    //Adding an Image
                    HtmlGenericControl image = new HtmlGenericControl("image");      
                    image.Attributes["src"] = promo.ImageSource;
                    image.Attributes["alt"] = promo.ImageAlt;
                    image.Attributes["title"] = promo.ImageTitle;

                    div1.Controls.Add(image);

                    //Adding two HREF for navigation
                    HtmlGenericControl alinkLeft = new HtmlGenericControl("a");
                    alinkLeft.Attributes["class"] = "slideshow-control-left";
                    alinkLeft.Attributes["href"] = "javascript:void(0);";
                    alinkLeft.Style["display"] = "inline";
                    div1.Controls.Add(alinkLeft);

                    HtmlGenericControl alinkRight = new HtmlGenericControl("a");
                    alinkRight.Attributes["class"] = "slideshow-control-right";
                    alinkRight.Attributes["href"] = "javascript:void(0);";
                    alinkRight.Style["display"] = "inline";
                    div1.Controls.Add(alinkRight);

                    //Adding Second div
                    Panel div2 = new Panel();
                    div2.CssClass = "slideshow-b";
                    div1.Controls.Add(div2);

                    //Adding Third div
                    Panel div3 = new Panel();
                    div3.CssClass = "slideshow-bl";
                    div2.Controls.Add(div3);

                    //Adding the A HREF Link
                    HtmlGenericControl alink = new HtmlGenericControl("a");
                    alink.Attributes["class"] = "slideshow-link";
                    alink.Attributes["href"] = promo.ImageHref;
                    div3.Controls.Add(alink);

                    //Adding the first span                
                    HtmlGenericControl span1 = new HtmlGenericControl("span");
                    span1.Attributes["class"] = "slideshow-header";
                    span1.InnerHtml = promo.ImageHeader;
                    alink.Controls.Add(span1);

                    //Adding line break
                    alink.Controls.Add(new LiteralControl("<br/>"));

                    //Adding the second span
                    HtmlGenericControl span2 = new HtmlGenericControl("span");
                    span2.Attributes["class"] = "slideshow-subheader";
                    span2.InnerHtml = promo.ImageSubHeader;
                    alink.Controls.Add(span2);
                    this.Controls.Add(div1);

                    if (base.FeaturedPromos.Count > 1)
                    {
                        //Adding DIV4 for prev and next
                        div4 = new Panel();
                        div4.CssClass = "slideshow-br";

                        //Adding DIV5 inside DIV4
                        Panel div5 = new Panel();
                        div5.CssClass = "slideshow-br-controls";
                        div4.Controls.Add(div5);

                        //Adding the PREV A HREF Link
                        HtmlGenericControl alinkPrev = new HtmlGenericControl("a");
                        alinkPrev.Attributes["class"] = "slideshow-br-controls-left";
                        alinkPrev.Attributes["href"] = "javascript:void(0);";
                        alinkPrev.Attributes["title"] = "Prev";
                        alinkPrev.Style["display"] = "inline";
                        alinkPrev.Attributes["CurrentDivID"] = promo.ID;
                        div5.Controls.Add(alinkPrev);

                        //Adding the span for prev and next buttons
                        HtmlGenericControl span3 = new HtmlGenericControl("span");
                        span3.Attributes["class"] = "slideshow-br-control-buttons";
                        int count = 0;
                        foreach (FeaturedPromo allPromo in base.FeaturedPromos)
                        {
                            if (count == 0)
                            {
                                //Adding the All HREF Link for Prev and Next
                                HtmlGenericControl aLLlinks = new HtmlGenericControl("a");
                                aLLlinks.Attributes["class"] = "" + count + "-banner-button";
                                aLLlinks.Attributes["href"] = "javascript:void(0);";
                                aLLlinks.Attributes["title"] = allPromo.ImageTitle;
                                aLLlinks.Style["display"] = "inline";

                                //aLLlinks.Attributes["id"] = "active-banner-slide";

                                span3.Controls.Add(aLLlinks);
                                count++;
                            }
                        }

                        div5.Controls.Add(span3);

                        //Adding the NEXT A HREF Link
                        HtmlGenericControl alinkNext = new HtmlGenericControl("a");
                        alinkNext.Attributes["class"] = "slideshow-br-controls-right";
                        alinkNext.Attributes["href"] = "javascript:void(0);";
                        alinkNext.Attributes["title"] = "Next";
                        alinkNext.Style["display"] = "inline";
                        alinkNext.Attributes["CurrentDivID"] = promo.ID;
                        div5.Controls.Add(alinkNext);
                        //div2.Controls.Add(div4);
                    }
                    this.Controls.Add(div1);
                    //this.Controls.Add(div4);
                    cntShow++;
                }
            }
            this.Controls.Add(div4);

            HttpCookie cookielist = new HttpCookie("ImageListOfCookies");

            for (int i = 0; i < imageList.Count; i++)
            {
                cookiecollection.Add("Image_" + i, imageList[i]);                
            }
            cookielist.Values.Add(cookiecollection);
            HttpContext.Current.Response.Cookies.Add(cookielist);


        }

解决方案

If you want to accomplish it with Cookies then since you are yourself showing the images in the first place so you know which images are going to be shown, so instead of storing Link to images (which can be pretty long) do this:

  • Assign each image an ID like 1 for spa.jpg , 2 for suites.jpg or more better if the file names are always unique then let the file names be the ID.

  • Then just when you show the image append the ID's in cookies like this (I have used | to facilitate splitting and reducing the cookies count to just One).

.

//For example to store "spa" if spa image has been stored
 if(Response.Cookies["Shown_Images"] != null)
 {
   Response.Cookies["Shown_Images"].Value = Response.Cookies["Shown_Images"] + "|spa";
 }
else
{
   Response.Cookies["Shown_Images"].Value = "spa";
}

Then to retrive if spa.jpghas been shown or not

if(Response.Cookies["Shown_Images"] != null)
{
   string[] images_shown = Response.Cookies["Shown_Images"];
   if(images_shown.Contains("spa"))
   {
       //If True then do processing;
   }
   else
   {
       //False then do as required
   }
}

这篇关于使用.NET Cookie代码显示动态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆