如何知道在asp.net

时间:2016-08-11 08:31:55

标签: c# jquery html asp.net

我有来自2个文件的12个随机图像。我想在点击图像时更改图像的边框颜色。我还想保存在数据库中单击的图片。 这是我用于2个文件夹中随机图像的代码:"成人"和#34;孩子"

    int i = 1, j=1;

    protected void Page_Load(object sender, EventArgs e)
    {
        string Image2Display1 = GetRandomImageAdult();
        RandomImg1.ImageUrl = Path.Combine("~/adults", Image2Display1);
        i++;
        if (RandomImg1.BorderColor.Equals()
        RandomImg1_click();
        string Image2Display2 = GetRandomImageChildren();
        RandomImg2.ImageUrl = Path.Combine("~/Children", Image2Display2);
        j++;

        string Image2Display3 = GetRandomImageChildren();
        RandomImg3.ImageUrl = Path.Combine("~/Children", Image2Display3);
        j++;

        string Image2Display4 = GetRandomImageAdult();
        RandomImg4.ImageUrl = Path.Combine("~/adults", Image2Display4);
        i++;

        string Image2Display5 = GetRandomImageChildren();
        RandomImg5.ImageUrl = Path.Combine("~/Children", Image2Display5);
        j++;

        string Image2Display6 = GetRandomImageAdult();
        RandomImg6.ImageUrl = Path.Combine("~/adults", Image2Display6);
        i++;

        string Image2Display7 = GetRandomImageChildren();
        RandomImg7.ImageUrl = Path.Combine("~/Children", Image2Display7);
        j++;

        string Image2Display8 = GetRandomImageAdult();
        RandomImg8.ImageUrl = Path.Combine("~/adults", Image2Display8);
        i++;

        string Image2Display9 = GetRandomImageAdult();
        RandomImg9.ImageUrl = Path.Combine("~/adults", Image2Display9);
        i++;

        string Image2Display10 = GetRandomImageChildren();
        RandomImg10.ImageUrl = Path.Combine("~/Children", Image2Display10);
        j++;

        string Image2Display11 = GetRandomImageAdult();
        RandomImg11.ImageUrl = Path.Combine("~/adults", Image2Display11);
        i++;

        string Image2Display12 = GetRandomImageChildren();
        RandomImg12.ImageUrl = Path.Combine("~/Children", Image2Display12);
        j++;


    }
    public string GetRandomImageAdult()
    {

        Random rnd = new Random();
        for (int ii = 1; ii <= i; ii++)
            rnd.Next();
        string[] images = Directory.GetFiles(MapPath("~/adults"));
        string Image2Display = images[rnd.Next(images.Length)];
        return Path.GetFileName(Image2Display);
    }

    public string GetRandomImageChildren()
    {
        Random   rnd = new Random();
        for (int jj = 1; jj <= j; jj++)
            rnd.Next();
        string[] images = Directory.GetFiles(MapPath("~/Children"));
        string Image2Display = images[rnd.Next(images.Length)];
        return Path.GetFileName(Image2Display);
    }

以下是来源:

<div>
    <asp:Image ID="RandomImg1" runat="server" Width="200px" />
    <asp:Image ID="RandomImg2" runat="server" Width="200px" />
    <asp:Image ID="RandomImg3" runat="server" Width="200px" />
    <asp:Image ID="RandomImg4" runat="server" Width="200px" />
    <asp:Image ID="RandomImg5" runat="server" Width="200px" />
    <asp:Image ID="RandomImg6" runat="server" Width="200px" />
    <asp:Image ID="RandomImg7" runat="server" Width="200px" />
    <asp:Image ID="RandomImg8" runat="server" Width="200px" />
    <asp:Image ID="RandomImg9" runat="server" Width="200px" />
    <asp:Image ID="RandomImg10" runat="server" Width="200px" />
    <asp:Image ID="RandomImg11" runat="server" Width="200px" />
    <asp:Image ID="RandomImg12" runat="server" Width="200px" />

</div>

请大概帮帮我&#34; ImageButton_Click&#34;。我不知道该怎么做。 感谢。

2 个答案:

答案 0 :(得分:0)

为所有图像提供一个公共类,如“prodImage”,然后在其上应用此jquery代码:

$('.prodImage').click(function(){
   var imageId = $(this).attr('id');   // imageId is the id of clicked image
   // push these image in an array and save that array in db or save individually in db
});

答案 1 :(得分:0)

首先使用可以点击的ImageButton,你可以知道他们点击过。

<asp:ImageButton runat="server" ID="imbBtn1" ImageUrl="~/img/Logos/promidea.png" OnClick="imbBtn1_Click" />

,第二个不要使用IsPostBack

重新初始化永久回发的按钮
 protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
           string Image2Display1 = GetRandomImageAdult();
           RandomImg1.ImageUrl = Path.Combine("~/adults", Image2Display1);
            i++;

     //// rest of the code