如何把RadioButtonList的文字轉換為圖片

RadionButtonList

直接進入主題

.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindType();
    }
    public void BindType()
    {
        //將資料填入泛型
        List<Product> infos = new List<Product>
        {
            new Product{ id=1, img="01.jpg", name="01"},
            new Product{ id=2, img="02.jpg", name="02"},
            new Product{ id=3, img="03.jpg", name="03"},
            new Product{ id=4, img="04.jpg", name="04"},
            new Product{ id=5, img="05.jpg", name="05"},
            new Product{ id=6, img="06.jpg", name="06"},
            new Product{ id=7, img="07.jpg", name="07"}
        };
        
        RadioButtonList1.DataSource = infos;
        RadioButtonList1.DataBind();
    }
    //準備一個calss
    public class Product
    {
        public int id { get; set; }
        public string img { get; set; }
        public string name { get; set; }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //取值的方法一樣
       Label1.Text = RadioButtonList1.SelectedValue;
    }

.aspx

 

    <asp:RadioButtonList ID="RadioButtonList1" runat="server" DataTextField="img" 
        DataValueField="id" DataTextFormatString="<img src='images/icon/{0}' />" 
        RepeatDirection="Horizontal" >
    </asp:RadioButtonList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

重點就是在DataTextFormatString這一段