<asp:RadioButtonList id="rdWenHua" runat="server" Width="164px" RepeatColumns="3" Height="1px" RepeatDirection="Horizontal"
CellSpacing="1" CellPadding="1">
<asp:ListItem Value="小学">小学</asp:ListItem>
<asp:ListItem Value="初中">初中</asp:ListItem>
<asp:ListItem Value="高中">高中</asp:ListItem>
<asp:ListItem Value="大学" Selected="True">大学</asp:ListItem>
<asp:ListItem Value="硕士">硕士</asp:ListItem>
<asp:ListItem Value="博士">博士</asp:ListItem>
</asp:RadioButtonList>
用下面代码取值,不管RadioButtonList选中的是哪项,得到的都是“大学”,怎么回事?
private void btnAdd_Click(object sender, System.EventArgs e)
{
Response.Write (rdWenHua.SelectedValue);
}

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="false" debug="true"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
    <html>
      <head>
        <title>CSharpTemp</title>
        <meta name=vs_defaultClientScript content="JavaScript">
        <script runat="server">
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
    Response.Write (rdWenHua.SelectedValue);
    } </script>  </head>
      <body>
        <form id="Form1" method="post" runat="server">
    <asp:RadioButtonList id="rdWenHua" runat="server" Width="164px" RepeatColumns="3" Height="1px" RepeatDirection="Horizontal"
    CellSpacing="1" CellPadding="1">
    <asp:ListItem Value="小学">小学</asp:ListItem>
    <asp:ListItem Value="初中">初中</asp:ListItem>
    <asp:ListItem Value="高中">高中</asp:ListItem>
    <asp:ListItem Value="大学" Selected="True">大学</asp:ListItem>
    <asp:ListItem Value="硕士">硕士</asp:ListItem>
    <asp:ListItem Value="博士">博士</asp:ListItem>
    </asp:RadioButtonList>
    <asp:Button id="Button1" runat="server" Text="测试按钮"/>
        </form>
      </body>
    </html>
      

  2.   

    rdWenHua.items[rdWenHua.selectedindex]
    这样如何
      

  3.   

    要保证SelectedValue在page_load之前被检测。
      

  4.   

    我想可能是在page_load的时候没有加入if not ispostback()吧,因为你提交的时候总是先重新加载一遍控件,如果你没加if not ispostback(),那么你的radiobuttonlist又被重新加载一遍,所以服务器就当你一直选中"大学"这一项。