Prop Webcontrol Image Imageurl
## ASP.NET Web Control: Image.ImageUrl Property
The `ImageUrl` property is a core property of the ASP.NET `Image` Web Server Control. It is used to get or set the URL (path) of the image to be displayed on the web page.
---
## Definition and Usage
The `ImageUrl` property specifies the location of the image file that the `Image` control will render. This path can be:
* **Relative to the current page** (e.g., `images/logo.png`)
* **Application-relative** using the tilde (`~`) operator (e.g., `~/images/logo.png`), which resolves to the root of the web application.
* **Absolute** (e.g., `https://www.example.com/images/logo.png`)
---
## Syntax
### Declarative Syntax
```xml
```
### Property Values
| Value | Description |
| :--- | :--- |
| `URL` | A string representing the path to the image file. |
---
## Code Examples
### Example 1: Basic Declarative Usage
The following example demonstrates how to set the `ImageUrl` property statically in the `.aspx` markup.
```xml
```
### Example 2: Dynamic Usage in Code-Behind (C#)
You can also set or change the `ImageUrl` dynamically at runtime using server-side code.
**ASPX Markup:**
```xml
```
**Code-Behind (C#):**
```csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set the image URL dynamically using an application-relative path
DynamicImage.ImageUrl = "~/images/banner_summer.jpg";
}
}
```
---
## Key Considerations
1. **Application-Relative Paths (`~`)**:
When working with nested directories, it is highly recommended to use the `~` operator (e.g., `~/images/pic.jpg`). ASP.NET automatically resolves this to the application root, preventing broken image links when pages are moved to different subfolders.
2. **Alternate Text**:
Always pair the `ImageUrl` property with the `AlternateText` property. If the image specified in `ImageUrl` fails to load, or if the user is using a screen reader, the `AlternateText` will be displayed/read instead.
3. **Supported Formats**:
The `ImageUrl` can point to any standard web-compatible image format supported by modern browsers, such as `.png`, `.jpg`, `.jpeg`, `.gif`, `.svg`, or `.webp`.
YouTip