如何知道在asp.net中单击了哪个随机图像
本文关键字:随机 图像 单击 何知道 asp net | 更新日期: 2023-09-27 18:01:48
我有来自2个文件的12个随机图像。我想改变边界颜色的图像,每当它被点击。我还想在数据库中保存点击的图片。这是我使用的代码从2个文件夹随机图像:"成人"answers"儿童"
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>
请帮助我可能"ImageButton_Click"。我不知道该怎么做。谢谢。
为所有像"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
});
首先使用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