c#中Gridview控件写判断语句

来源:互联网 发布:virtualbox拖拽文件mac 编辑:程序博客网 时间:2024/06/07 03:39

前段时间跟着老师做了一个.net小项目其中一个页面涉及了用GridView控件 绑定数据,要求是从不同的数据表中查出数据,绑定到GridView中,我就想着在前台页面写判断语句,查了好久,终于弄明白了,这是我前台的一部分代码。    

   <table width="99%" height="30px" border="0" align="center" cellpadding="0" cellspacing="0">

                                <tr>
                                    <td width="10">&nbsp;</td>
                                    <td>  


                                     <%if (flag == "a")
                                        {%>
                                        <asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False" 
                                            GridLines="None" ShowHeader="False" Height="100%" Width="100%" CssClass="listA">
                                            <RowStyle Height="25px" />
                                            <Columns>
                                                             
                                             <asp:TemplateField HeaderText="标题">
                                              <ItemTemplate>                                                                                      
                                                <table align="left" border="0" cellpadding="0" cellspacing="0" >
                                                 <tr >                                                                           
                                              
                                                       <td width="25px"></td>                    
                                                       <td width="450px" align="left">
                                                           //其他内容
                                                       </td>
                                                  </tr>
                                               </table>    
                                            </ItemTemplate>
                                            <ItemStyle  CssClass="listA" Height="25px"  HorizontalAlign="Left" />
                                             </asp:TemplateField>
                                        
                                            </Columns>
                                        </asp:GridView>
                                    <%} %>




                                    <%if (flag == "b")
                                    {%>
                       
                                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                            GridLines="None" ShowHeader="False" Height="100%" Width="100%" CssClass="listA">
                                            <RowStyle Height="25px" />
                                            <Columns>
                                                             
                                             <asp:TemplateField HeaderText="标题">
                                              <ItemTemplate>                                                                                      
                                                <table align="left" border="0" cellpadding="0" cellspacing="0" >
                                                 <tr >                                                                           
                                              
                                                       <td width="25px">  </td>                    
                                                       <td width="450px" align="left">
                                                           //其他内容
                                                        </td>
                                                  </tr>
                                               </table>    
                                            </ItemTemplate>
                                            <ItemStyle  CssClass="listA" Height="25px"  HorizontalAlign="Left" />
                                             </asp:TemplateField>
                                        
                                            </Columns>
                                        </asp:GridView>
                                   
                                   <%} %>


                                    <%if (flag == "c")
                                    {%>
                       
                                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                                            GridLines="None" ShowHeader="False" Height="100%" Width="100%" CssClass="listA">
                                            <RowStyle Height="25px" />
                                            <Columns>
                                                             
                                             <asp:TemplateField HeaderText="标题">
                                              <ItemTemplate>                                                                                      
                                                <table align="left" border="0" cellpadding="0" cellspacing="0" >
                                                 <tr >                                                                           
                                              
                                                       <td width="25px"> </td>                    
                                                       <td width="450px" align="left">
                                                            //其他内容
                                                       </td>
                                                  </tr>
                                               </table>    
                                            </ItemTemplate>
                                            <ItemStyle  CssClass="listA" Height="25px"  HorizontalAlign="Left" />
                                             </asp:TemplateField>
                                        
                                            </Columns>
                                        </asp:GridView>
                                   
                                   <%} %>                       

                                    </td>
                                     <td width="10">&nbsp;</td>
                                </tr>

                            </table>


切记:这种

<%if (flag == "A") {%>  <%} %>   

语句要放在GridView外面,否则就会报错。


flag中的值是要从后台获得的。