被人遗忘的控件(一)

.net自己已经提供了许多非常强大的服务器控件,方便我们在开发时使用。有许多控件我们简直是天天和它们打交道,也有些控件被我们遗忘了(尽管它们那么的好用)。

准备开始讲解了,希望各位一起讨论。

我们知道web窗体中有类控件叫PlaceHolder,它好像一个空白的容器,具体展现在客户端的浏览器中是看不到它的。可能正因为如此,我们很少注意到它的存在,也很少使用这个控件。实际上它是非常好用的,我们举例来说明。

关于网站调查的功能实现,相信对大家都是很简单的,单选多选。下面的代码就是其中的一种实现(为了方便浏览,仅显示其中关键的一段)。
                                                <ItemTemplate>
                                                    
<%# Qtype==0 ?"<input type='radio'":"<input type='checkbox'" %>
                                                    
<%" name='question_"+ Qindex +"'" %>
                                                    
<%" value='"+ DataBinder.Eval(Container.DataItem, "fdoptionid"+"'" %>
                                                    
<%#" max='"+ QmaxSelect +"'>" %>
                                                    
<a href='<%= scripturl %><%# DataBinder.Eval(Container.DataItem, "fdphotoid") %>' class='link-yellow' target=_blank>
                                                        
<%# DataBinder.Eval(Container.DataItem, "fdtitle"%>
                                                    
</a>
                                                
</ItemTemplate>

对于加粗这行的小技巧,应该很多人都明白,也这么使用过,用来实现单选或者多选。或许还有很多人都在使用这种从asp开发中直接应用过来的小技巧。如果调查的类型更丰富,出现了问答题的时候,这样做还行吗?在.net中有没有更好的实现方式呢?

答案是肯定的,使用PlaceHolder控件之后,我们可以更轻松地实现,程序也更容易阅读。下面是aspx的代码以及cs代码(部分)。
                                                                                    <asp:repeater id="Repeater1" runat="server">
                                                                                        
<ItemTemplate>
                                                                                            
<table width="100%" border="0" cellpadding="2" cellspacing="0" class="content">
                                                                                                
<tr>
                                                                                                    
<td><strong><%# Container.ItemIndex+1 %>.<%# DataBinder.Eval(Container.DataItem, "title"%></strong>
                                                                                                        
<asp:Label id="QType" runat="server" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem, "qtype") %>'>
                                                                                                        
</asp:Label>
                                                                                                        
<asp:Label id="QClass" runat="server" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem, "qclass") %>'>
                                                                                                        
</asp:Label>
                                                                                                    
</td>
                                                                                                
</tr>
                                                                                                
<tr>
                                                                                                    
<td>
                                                                                                        
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder></td>
                                                                                                
</tr>
                                                                                            
</table>
                                                                                            
<br>
                                                                                        
</ItemTemplate>
                                                                                    
</asp:repeater>

                    if(Qtype==0//如果是单选题
                    {
                        
//----------------添加RadioButtonList控件-------------------
                        RadioButtonList radiolist = new RadioButtonList();
                        radiolist.ID 
= "Question";
                        radiolist.RepeatLayout 
= RepeatLayout.Table;
                        radiolist.CellPadding 
= 0;
                        radiolist.CellSpacing 
= 0;
                        radiolist.CssClass 
= "content";
                        
if(maxlen>=20)
                        
{
                            radiolist.RepeatDirection 
= RepeatDirection.Vertical;
                        }

                        
else
                        
{
                            radiolist.RepeatDirection 
= RepeatDirection.Horizontal;
                            
if(maxlen>=15) radiolist.RepeatColumns = 3;
                            
else if(maxlen>=10) radiolist.RepeatColumns = 4;
                            
else if(maxlen>=6) radiolist.RepeatColumns = 5;
                            
else radiolist.RepeatColumns = 6;
                            
if(objItem.propRowCount> radiolist.RepeatColumns)
                            
{
                                radiolist.Width 
= new Unit(100, UnitType.Percentage);
                            }

                        }

                        
//----------------绑定数据-------------------
                        for(int j=0;j<objItem.propRowCount;j++)
                        
{
                            ListItem myItem 
= new ListItem();
                            myItem.Text 
= objItem.propDset.Tables[0].Rows[j]["title"].ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;";
                            myItem.Value 
= objItem.propDset.Tables[0].Rows[j]["score"].ToString();
                            radiolist.Items.Add(myItem);
                        }

                        placeh.Controls.Add(radiolist);
                        
//----------------添加校验控件-------------------
                        RequiredFieldValidator rfv = new RequiredFieldValidator();
                        rfv.ID 
= "RequiredFieldValidator1";
                        rfv.ControlToValidate 
= radiolist.ID;
                        rfv.ErrorMessage 
= "请选择你第"+(i+1)+"题的答案";
                        rfv.Display 
= ValidatorDisplay.Dynamic;
                        placeh.Controls.Add(rfv);
                    }

                    
else if(Qtype==1//如果是多选题
                    {
                        
//----------------添加CheckBoxList控件-------------------
                        CheckBoxList checklist = new CheckBoxList();
                        checklist.ID 
= "Question";
                        checklist.RepeatLayout 
= RepeatLayout.Table;
                        checklist.CellPadding 
= 0;
                        checklist.CellSpacing 
= 0;
                        checklist.CssClass 
= "content";
                        
if(maxlen>=20)
                        
{
                            checklist.RepeatDirection 
= RepeatDirection.Vertical;
                        }

                        
else
                        
{
                            checklist.RepeatDirection 
= RepeatDirection.Horizontal;
                            
if(maxlen>=15) checklist.RepeatColumns = 3;
                            
else if(maxlen>=10) checklist.RepeatColumns = 4;
                            
else if(maxlen>=6) checklist.RepeatColumns = 5;
                            
else checklist.RepeatColumns = 6;
                            
if(objItem.propRowCount> checklist.RepeatColumns)
                            
{
                                checklist.Width 
= new Unit(100, UnitType.Percentage);
                            }

                        }

                        
//----------------绑定数据-------------------
                        for(int j=0;j<objItem.propRowCount;j++)
                        
{
                            ListItem myItem 
= new ListItem();
                            myItem.Text 
= objItem.propDset.Tables[0].Rows[j]["title"].ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;";
                            myItem.Value 
= objItem.propDset.Tables[0].Rows[j]["score"].ToString();
                            checklist.Items.Add(myItem);
                        }

                        placeh.Controls.Add(checklist);
                    }

可以看出,使用PlaceHolder之后,我们可以在后置代码中方便地实现各种效果,即使是问答题我们也可以通过向PlaceHolder添加子控件来实现。

以上仅仅是举例说明了这个控件的一个具体应用,如何更好的使用这些控件来欢迎大家多多探讨。
posted on 2005-02-28 11:30  湘南和也  阅读(2296)  评论(6编辑  收藏  举报