刚学习asp.net,
对模板列比较模糊,
看到c#高级编程里面对于web部分也是一带而过,
问题补充:模板列时面再用模板是怎么回事,
可用的模板也就那几个么,如itemtemplate,headtemlate等,还是随便自己写呢,刚上找了一大堆,都是关于MVC中的模板的,而不是GridView中的模板,有知道的能提供一下参考文章么,谢谢

解决方案 »

  1.   

    比如GridView的Columns里有个
    <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />通过GridView的SmartTag->编辑列,选择这个ProductID列->'将此字段转换为TemplateFile'。经过这步骤后,上面这个BoundField变成了类似这样的模板列:
    <asp:TemplateField HeaderText="ProductID" InsertVisible="False" SortExpression="ProductID">     
         <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
         </ItemTemplate>
    </asp:TemplateField>再运行,显示没什么区别。如果我在<ItemTemplate>内加上‘产品号码:’:
    <ItemTemplate>
          产品号码:<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
    </ItemTemplate>如果
    <ItemTemplate>
          <span style="font-size:14pt; color:Red">产品号码:<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label></span>
    </ItemTemplate>
    如果
    <ItemTemplate>
         <asp:Panel ID="Panel1" runat="server" BorderColor="Yellow" BorderStyle="Solid" BorderWidth="4">
               产品号码:<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label><br />                            
         </asp:Panel>
    </ItemTemplate>如果。
    就是这么回事,可定制性。相比<asp:BoundField>,你可以随意定制某个列显示什么,增加元素,修改样式随你怎么玩了。FormView, DetailsView, DataList等等的template都是这样子。
      

  2.   

    http://topic.csdn.net/u/20080919/08/fc7314bd-6420-4e79-8c72-57fc3b26a1e0.html我准备在这里总结一下GridView.一起学习.
      

  3.   

    模板列里为什么再用模板?
    列可以用<asp:BoundField DataField="Id" />  一列绑定一个字段.
    也可以一列绑定多个字段,有时候只用一个模板列,然后在这个列里绑定所有字段.
    还是自己用一下,然后仔细想想.
      

  4.   

    通过html设置的gridview的模板,就行设置grdview列绑定的字段以及字段用的控件,如textbox、checkbox等等!!
      

  5.   

    比方說你要在gridview中用到dropdownlist,就要用到模板列了。
    寫個簡單的例子:
    <asp:TemplateField HeaderText="Project">
                    <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "Project")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:Label ID="Label6" runat="server" Visible="false" Text='<%#DataBinder .Eval (Container.DataItem ,"Project" )%>'></asp:Label>
                        <asp:DropDownList ID="DropDownList3" runat="server">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
    CODE:
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
                DropDownList ddl3 = (DropDownList)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("DropDownList3");
                Label lb6 = (Label)GridView1.Rows[e.NewEditIndex].Cells[5].FindControl("Label6");
                DataSet ds2 = new DataSet();
                ds2 = func.DropDownList_TypeBind(1, "itemName");
                dc.Dropdownlist_Bind(ddl3, ds2, 0, 0, 1);
                ddl3.SelectedValue = lb6.Text.Trim();
                ……
                  BindGridView();
        }