简体   繁体   中英

ASP.NET: Resize Image in datalist?

I have a DataList control on a page. Inside ItemTemplate I have an Image and a Label control. The page get the imageurl from an sql query. The images those are shown in the image control have variable width and height. I want to set it to 160x100(wxh). I can calculate the width ratio and height ratio and have it not increased to more than 160x100.

Not sure how can i resize image control width and height programatically, for each item in datalist.

You can resize image on ItemDataBound event. Try below code.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

                      // get image option if in grid or from datasource using DataBinder.Eval()
                    Image im1 = (Image)e.Item.FindControl("Image1");                                     
                    im1.Width = "Your Width";      
                    im1.height = "Your Height";

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM