Kristof Claes
Kristof Claes

Reputation: 10941

How can I let my custom user control accept a list of properties in ASP.NET WebForms?

Let's say I have a user control that contains a file upload field and a submit button:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ImageUploader.ascx.cs" Inherits="MyProject.ImageUploader" %>

<asp:FileUpload ID="fileInput" runat="server"/>
<asp:Button ID="submitButton" runat="server" Text="Upload" OnClick="UploadImage" />

I would like to pass in multiple sizes to resize an uploaded image to. I know how to do this from code behind, but I would like to be able to use my user control like this:

<%@ Register tagPrefix="uc" tagName="ImageUploader" src="ImageUploader.ascx" %>

<uc:ImageUploader id="uploader" runat="server">
    <Sizes>
        <Size Width="640" Height="480" />
        <Size Width="320" Height="240" />
    </Sizes>
</uc:ImageUploader>

How can achieve I something like this? I have no idea how this concept is called so that makes searching for it a bit difficult.

Upvotes: 1

Views: 634

Answers (1)

Vitaly Zemlyansky
Vitaly Zemlyansky

Reputation: 331

In your user control(ImageUploader) you sholud create property like this one

 public List<Size> Sizes { get; set; }

Of course, you must create class Size

 public class Size
 {
    public int Width { get; set; } 
    public int Height { get; set; } 
 }

After that, you can use this property somethimg like this

<uc:ImageUploader id="uploader" runat="server">
    <Sizes>
        <asp:Size Width="120" Height="340"/>
        <asp:Size Width="170" Height="540"/>
        <asp:Size Width="130" Height="740"/>
    </Sizes>
</uc:ImageUploader>

Upvotes: 1

Related Questions