日期:2014-05-18  浏览次数:20448 次

Repeater同时嵌RadioButtonList和CheckBoxList
一个考试需求系统用Repeater同时嵌RadioButtonList和CheckBoxList通过数据库一个字段判断是单选还是多选显示,如下:
<asp:repeater id="rptTitle" Runat="server" OnItemDataBound="rptTitle_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:RadioButtonList ID="rblst" runat="server" />
<asp:CheckBoxList ID="chklst" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:repeater>
现在数据已经读出来了,但是我想加个题目的类型如:


单选 (上边所指的题目类型)
1. 1+1=?
A、1 B、2 
2. 2+2=?
A、1 B、4 
多选题:(上边所指的题目类型)

3.The hopes goals,fears and desires widely between men and women,between the rich and the poor.
A、alter 
B、shift 
C、transfer 
D、vary 
因为数据是从repeater中循环读出来的我现在只会在每个题目上都加上题目类型,我想实现上边的效果有什么好方法

------解决方案--------------------
C# code

            if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
            {
           
                Question qst = (Question)e.Item.DataItem;
                           {
                RadioButtonList rblst = (RadioButtonList)e.Item.FindControl("rblst");
                CheckBoxList chklst = (CheckBoxList)e.Item.FindControl("chklst");
                ListControl lc = null;
                if (qst.Typeid == 1)
                {
                    
                    if (e.Item.ItemIndex=0)
                    {
                        HtmlTableRow r = new HtmlTableRow();
                        HtmlTableCell c = new HtmlTableCell(); 
                        c.Controls.Add(new LiteralControl("单选题"));
                        r.Cells.Add(c);
                        e.Item.Controls.AddAt(0,r);
                    }

                    lc = rblst;
                    chklst.Visible = false;
                   
                }
                else
                {
 Question qst2= (Question) rptTitle.Items[e.Item.ItemIndex-1].DataItem;

                    if (qst.Typeid !=qst2.Typeid)
                    {
                        HtmlTableRow r = new HtmlTableRow();
                        HtmlTableCell c = new HtmlTableCell(); 
                        c.Controls.Add(new LiteralControl("多选题"));
                        r.Cells.Add(c);
                        e.Item.Controls.AddAt(0,r);
                    }

                    lc = chklst;
                    rblst.Visible = false;
                    
                
                    }

                }
            }