如何用ASP.NET中的RadioButton、RadioButtonList、CheckBox和CheckBoxList控件动态地改变字体的颜色

我目前做了很简单的一步,就是列出了四种控件。。属于脑残行为。那么我该怎么做添加代码才能实现题目中的功能呢?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="4.aspx.cs" Inherits="动态地改变字体显示效果._4" %>

<!DOCTYPE html>









    <asp:RadioButtonList ID="RadioButtonList1" runat="server">

    </asp:RadioButtonList>

    <asp:CheckBox ID="CheckBox1" runat="server" />

    <asp:CheckBoxList ID="CheckBoxList1" runat="server">                </asp:CheckBoxList>
    </div>

</form>


给你写了一个

 <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="Q702913._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"
        onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" 
        AutoPostBack="True">
        <asp:ListItem>red</asp:ListItem>
        <asp:ListItem>blue</asp:ListItem>
        <asp:ListItem>black</asp:ListItem>
    </asp:RadioButtonList>
    <script type="text/javascript">
        var para = document.getElementsByTagName("p");
        for (var i = 0; i < para.length; i++) {
            para[i].setAttribute("style", "color:<% =bgcolor %>");
        }
    </script>
</asp:Content>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Q702913
{
    public partial class _Default : System.Web.UI.Page
    {
        public string bgcolor = "red";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && RadioButtonList1.SelectedValue != "")
            {
                bgcolor = RadioButtonList1.SelectedValue;
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bgcolor = RadioButtonList1.SelectedValue;
        }
    }
}




图片说明

代码下载:https://download.csdn.net/download/caozhy/10728187