在asp.net中如何获取asp:DataList中子控件asp:RadioButtonList的值

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zhengzhuang.aspx.cs" Inherits="search_zhengzhuang" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1_TMB_TestQuestion" runat="server" BackColor="White"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
            GridLines="Both">
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <ItemStyle ForeColor="#000066" />
            <SelectedItemStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <ItemTemplate>
                <table>
                    <tr>
                        <td><asp:Label ID="Label2" runat="server" Text=<%# ((System.Data.DataRowView)Container.DataItem).Row["FTQID"].ToString().Substring(7, 3)%>></asp:Label>、</td>
                       

                        <td><asp:Label ID="Label1" runat="server" Text=<%# Eval("FTQName") %> ></asp:Label></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><asp:RadioButtonList ID="RadioButtonList1" runat="server" Font-Size="9pt" RepeatDirection="Horizontal">
                                <asp:ListItem Value="0">从无</asp:ListItem>
                                <asp:ListItem Value="1">轻度</asp:ListItem>
                                <asp:ListItem Value="2">中度</asp:ListItem>
                                <asp:ListItem Value="3">偏重</asp:ListItem>
                                <asp:ListItem Value="4">严重</asp:ListItem>
                            </asp:RadioButtonList></td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
        <asp:Button ID="Button1" runat="server" Text="交卷" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

后台:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
public partial class search_zhengzhuang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    public void DataBind()
    {
        string strProvider = ConfigurationManager.ConnectionStrings["PhysiqueCheckConnectionString"].ConnectionString;
        SqlConnection MyConn = new SqlConnection(strProvider);

        MyConn.Open();

        DataSet dataSet = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(" select TMB_TestQuestion.* from TMB_TestQuestion where TMB_TestQuestion.FTQID like '000001%'", MyConn);

        adapter.Fill(dataSet, "TMB_TestQuestion");

        DataList1_TMB_TestQuestion.DataSource = dataSet;
        DataList1_TMB_TestQuestion.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strProvider = ConfigurationManager.ConnectionStrings["PhysiqueCheckConnectionString"].ConnectionString;
        SqlConnection MyConn = new SqlConnection(strProvider);
        MyConn.Open();

        string strSel = "delete TMG_Result where FIndex='0009'";
        SqlCommand cmd = new SqlCommand(strSel, MyConn);
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Dispose();

        //SqlCommand cmd = new SqlCommand();
        //cmd.Connection = MyConn;
        //SqlDataReader reader = null;
        string getvalue=null;    
       for(int i=0;i <this.DataList1_TMB_TestQuestion.Items.Count;i++)
       {

           getvalue = ((RadioButtonList)this.DataList1_TMB_TestQuestion.Items[i].FindControl("RadioButtonList1")).SelectedValue.ToString();   //重要的是这句话
           if (getvalue == null || getvalue.Equals(""))
           {
              
               Response.Write("<script>alert('第" + (i+1) + "题未填!')</script>");
               break;
           }


           strSel = "insert into TMG_Result(FRecord,FOrder,FQuesID,FItemID,FIndex,FExported) values("+i+","+i+","+i+"," + getvalue + ",0009,3)";
           cmd.CommandText = strSel;
           reader = cmd.ExecuteReader();
           reader.Dispose();
       }
    }


}
posted on 2011-06-30 16:53  carekee  阅读(3030)  评论(0编辑  收藏  举报