Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We are reading pictures from the database using datalist and stored in a image button.
we want to get the id of the picture when you click on which ever image button, the controls you use inside the datalist itemtemplate aren't recognised in the code section.
is there anyway you can help?
Posted

1 solution

This is a realy simple way to do it.
When you click on an image the postback will fire and in the top right hand corner of the page you will see the image id.

Here is the HTML that I used.
Paste this in your designer.

XML
<asp:DataList ID="dlImages" runat="server" DataKeyField="imageID" RepeatColumns="3" RepeatDirection="Horizontal" >
        <ItemTemplate>
            <asp:ImageButton ID="ibImages" runat="server"
                ImageUrl='<%# Eval("ImagePath") %>' CommandArgument='<%# Eval("ImageID") %>'
                onclick="ibImages_Click" />
        </ItemTemplate>



paste this code in your code behind.

private List<Images> listOfImages = new List<Images>();

protected void Page_Load(object sender, System.EventArgs e)
{
	if (!IsPostBack) {
		LoadImages();
		this.dlImages.DataSource = listOfImages;
		this.dlImages.DataBind();
	}


}


private void LoadImages()
{
	for (i = 1; i <= 10; i++) {
		Images newImage = new Images();
		newImage.ImageID = i;

		switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 9:
				newImage.ImagePath = ADD YOUR IMAGE PATH HERE;

				break;
			case 2:
			case 4:
			case 6:
			case 8:
			case 10:
				newImage.ImagePath = ADD YOUR IMAGE PATH HERE;
				break;

		}
		listOfImages.Add(newImage);

	}
}

public class Images
{
	private int _imageID;
	public int ImageID {
		get { return _imageID; }
		set { _imageID = value; }
	}

	private string _imagePahh;
	public string ImagePath {
		get { return _imagePahh; }
		set { _imagePahh = value; }
	}


}

private void dlImages_SelectedIndexChanged(object sender, System.EventArgs e)
{
	Response.Write("IMAGE ID = ");
}

protected void ibImages_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
	Response.Write("IMAGE ID = " + ((ImageButton)sender).CommandArgument);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900