我有一个问题,页面上有很多个RadioButtonList 但是这些RadioButtonList是通过数据库生成的,所以我想问的问题是,ASP.NET的后台怎么去把这些RadioButtonList所选的值 以及RadioButtonList的值 存数据库。<asp:Panel ID="checkboxlist" runat="server">
        <asp:Label runat="server" ID="11">办公装备</asp:Label>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
            RepeatDirection="Horizontal">
            <asp:ListItem Value="0">良好</asp:ListItem>
            <asp:ListItem Value="1">一般</asp:ListItem>
            <asp:ListItem Value="2">尚可</asp:ListItem>
        </asp:RadioButtonList>
    </asp:Panel>就如上面代码所示 可能会有很多个这样的label和RadioButtonList  我要把 label所对应的ID 和 RadioButtonList  所对应的选项都写进数据库,但是在后台我不知道一共有多少个这样的label和RadioButtonList  。因为他是通过数据库自动生成的。所以有什么办法去存呢???求高手解答 谢谢啦~

解决方案 »

  1.   

    这些都是RadioButtonList 和 label 都是通过数据库里面存的模板生成的(比如说数据库里面规定了这个表单里面有12个这种RadioButtonList)
      

  2.   

    放到一个容器中如DateList,循环显示,循环存储
      

  3.   

     <asp:Label runat="server" ID="11">办公装备</asp:Label>
            <asp:RadioButtonList 既然Label和RadioButtonList 是1对1的关系,这个就得看成一个块,如果你全部的label和radioButtonList都堆到<asp:Panel ID="checkboxlist">里面,那你的label和RadioButtonList生成的时候, ID之间就得有一个关联约定
      

  4.   

    放到一个容器中如DateList,循环显示,循环存储+1 
      

  5.   

    能不能说明白一点呢?放到Datelist里面?能否写个简单的实例?
      

  6.   


        <form id="form1" runat="server">
        <asp:Panel ID="checkboxlist" runat="server">
            <asp:Label runat="server" ID="lbl_1">办公装备1</asp:Label>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Value="0">良好</asp:ListItem>
                <asp:ListItem Value="1">一般</asp:ListItem>
                <asp:ListItem Value="2">尚可</asp:ListItem>
            </asp:RadioButtonList>
            <asp:Label runat="server" ID="lbl_2">办公装备2</asp:Label>
            <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Value="0">良好</asp:ListItem>
                <asp:ListItem Value="1">一般</asp:ListItem>
                <asp:ListItem Value="2">尚可</asp:ListItem>
            </asp:RadioButtonList>
        </asp:Panel>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </form>
    protected void Button1_Click(object sender, EventArgs e)
        {
            ControlCollection col = checkboxlist.Controls;
            foreach (Control c in col)
            {
                if (c != null && c is Label)
                {
                    Label lblObj = (Label)c;
                    string[] arr = lblObj.ID.Split('_');
                    if (arr.Length > 1)
                    {
                        Control cl = Page.FindControl("RadioButtonList" + arr[1]);
                        if (cl != null && cl is RadioButtonList)
                        {
                            RadioButtonList radlObj = (RadioButtonList)cl;
                            Response.Write(string.Format("title:{0} select:{1}<br>", lblObj.Text, radlObj.SelectedItem));
                        }
                    }
                }
            }
        }
    result:title:办公装备1 select:良好
    title:办公装备2 select:一般
      

  7.   

    还想一个问题,这种数据库 模板应该怎么建比较好
    表1 lable表
    labelID 名字
    1 办公装备 
    2 办公环境表2 选项表
    ID   labelID  选项Text 选项Value
    1     1       良好      1
    2     1       一般      2
    3     2       优秀      1
    4     2       一般      2表3 模板表
    ID  模板ID lableID
    1    1     1
    2    1     2表4 具体对象表
    ID  lableID 选项Value 模板ID 对象ID
    1              1        1     审核表1各位觉得这样设计行吗?求评价~求帮忙。
      

  8.   

    然后如果模板要变化 比如模板要多加个几个 radiobuttonlist只要在模板表里面修改就行是吗?
      

  9.   

    你可以把你的这些数据放入到数据库中,然后把你想要的数据显示在radiobuttonlist,
    绑定数据源的时候直接将查询的结果绑定上,然后在设置一下DataValueField 和DataTextField,
    这个样子在存入数据库中的时候也好获取值,而且操作比较方便,                    RadioButtonList radio = new RadioButtonList();
                        radio.EnableViewState = true;
                        radio.ID = dr["id"].ToString();
                        radio.DataSource = ds.Tables[0];
                        radio.DataTextField = "title";
                        radio.DataValueField = "id";
                        radio.DataBind();
                        this.Panel1.Controls.Add(radio);自己去创建控件,也可以用用户控件呀,想要多少个,就循环多少次,变一下里面的参数就可以了呀
      

  10.   


    protected void Button1_Click(object sender, EventArgs e)
        {
            ControlCollection col = checkboxlist.Controls;
            foreach (Control c in col)
            {
                if (c != null && c is Label)
                {
                    Label lblObj = (Label)c;
                    string[] arr = lblObj.ID.Split('_');
                    if (arr.Length > 1)
                    {
                        Control cl = Page.FindControl("RadioButtonList" + arr[1]);
                        if (cl != null && cl is RadioButtonList)
                        {
                            RadioButtonList radlObj = (RadioButtonList)cl;
                            //添加数据的方法
                            Response.Write(string.Format("title:{0} select:{1}<br>", lblObj.Text, radlObj.SelectedItem));
                        }
                    }
                }
            }
        }
      

  11.   


    aspx(或者ascx、master page等)本来就是模板。你是在设计窗口上写定义,在代码部分写其它的、仅用声明而不能实现的功能嘛。模板的文本跟代码是封装在一起相互协调的。那种只考虑一个简单文本替换的所谓“模板”,功能是最初级的,没有考虑你续写代码以及各种高级编程的需求。
      

  12.   

    大师,我是说比如 我这个页面 可能有12个radiobuttonlist,然后我就把信息存在数据库,然后动态生成页面。然后选完radio以后在存在具体对象表。您说的模板是Master page那种吧?
      

  13.   

    表3 模板表
    ID 模板ID lableID
    1 1 1
    2 1 2表4 具体对象表
    ID lableID 选项Value 模板ID 对象ID
    1 1 1 审核表1
    改为表3 模板表
    ID lableID
    1   1
    2   2表4 具体对象表
    ID 选项表ID 模板表ID 对象ID
    1  1       1        审核表1你的选项表ID是唯一值,已经可以标识某一条labelid和其value,模板表一个id就好了,虽然我不知道你的具体需求是什么
      

  14.   

    我的需求是 一个页面有很多的radiobuttonlist,然后这些都是根据数据库的模板生成的,然后可能以后会换模板,比如说表单1 他的模板是模板1 然后模板1 现在有12个radiobuttonlist,那么下一个模板可能会有15个模板,就是这个意思,然后前台的radiobuttonlist是通过后台数据库的模板生成的。然后这个表单1里面每个raidobuttonlist选取的值就存在了
    表4 具体对象表
    ID lableID 选项Value 模板ID 对象ID
    1 1 1 审核表1
    具体对象表里面的一行