如题,请给出例子,谢谢!

解决方案 »

  1.   

    1、创建一个新的文本文件,使用 .ascx 扩展名(最简单的办法)。
    2、向该模板文件添加模板定义语句并保存该文件(使用的标记应与所有声明性模板中使用的相同)。下面的示例显示一个具有模板标记的 .ascx 文件的内容,该模板包含数据绑定 Label 控件,假定单项绑定(Eval)。
    ------- myTemplate.ascx 内容----------
    <asp:Label ID="Label1" runat="server" 
         Text='<%# Eval("CompanyName") %>'>
    </asp:Label>3、使用 LoadTemplate 方法加载模板。
    --------------------------------------
    protected void Page_Load(object sender, EventArgs e)
    {
        //动态加入一模版列,绑定CompanyName字段
        TemplateField tf = new TemplateField();
        tf.HeaderText = "动态添加列";
        tf.ItemTemplate = Page.LoadTemplate("myTemplate.ascx");
        GridView1.Columns.Add(tf);
    }
    ========上面示例使用的数据源为NorthWind中的[Customers]表=========动态添加非模版列方法简单些:
    DataControlField 类用作所有数据控件字段类型的基类,你实际使用时,最多的是使用其派生的类,包括:
    BoundField 
     以文本形式显示数据源中某个字段的值。 
    ButtonField 
     显示数据绑定控件中的命令按钮。根据控件的不同,这允许您显示带有自定义按钮控件的行或列,如“添加”或“移除”按钮。 
    CheckBoxField 
     显示数据绑定控件中的复选框。此数据控件字段类型通常用于显示带有布尔值的字段。 
    CommandField 
     显示数据绑定控件中要执行编辑、插入或删除操作的内置命令按钮。 
    HyperLinkField 
     将数据源中某个字段的值显示为超链接。此数据控件字段类型允许您将第二个字段绑定到超链接的 URL。 
    ImageField 
     显示数据绑定控件中的图像。 
    TemplateField 
     根据指定的模板,显示数据绑定控件中的用户定义内容。例如:往一个正常显示的GridView里末尾增加一列:
    protected void Button1_Click(object sender, EventArgs e)
    {
        BoundField bf = new BoundField();
        bf.HeaderText = "newColumn";
        bf.DataField = "Address";
        GridView1.Columns.Add(bf);
        //GridView1.DataBind(); //如果GridView通过指定其DataSourceID方式进行绑定,可不用此句。
    }
    ==== 
    ~~~~ 我的Blog:http://blog.csdn.net/quou2002 
      

  2.   

    写在模版文件里最简单==== 
    ~~~~ 我的Blog:http://blog.csdn.net/quou2002 
      

  3.   

    因为我想的是一个gridview来显示不同的表(主要的通过xml),而且这些表是有关联的,所以不知道怎么写模板!望高人答之!
      

  4.   

    页面一旦postback操作,动态添加的都没了
      

  5.   

    <asp:GridView ID="StoreList" runat="server" AutoGenerateColumns="False" OnRowDataBound="StoreList_RowDataBound"  BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnDataBound="StoreList_DataBound" OnPageIndexChanging="StoreList_PageIndexChanging" AllowPaging="true">
                <Columns>
                    <asp:TemplateField HeaderText="收藏编号">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PS_ID") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("PS_ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="收藏名称">    
                        <ItemTemplate>
                        <asp:LinkButton ID="LB_Name" runat="server" Text='<%# Bind("PS_TiTle") %>' CommandArgument='<%# Eval("PS_ID", "{0}") %>' OnCommand="LB_Name_Command"></asp:LinkButton>
                           <%-- <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PS_TiTle") %>'></asp:TextBox>
                        </EditItemTemplate><asp:Label ID="Label2" runat="server" Text='<%# Bind("PS_TiTle") %>'></asp:Label>--%>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="收藏类型">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("PS_Type") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("PS_Type") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="收藏地址">
                        <ItemTemplate> 
                            <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("PS_URL")%>' CommandArgument='<%# Eval("PS_ID", "{0}") %>' OnCommand="LinkButton1_Command">LinkButton</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="删除">
                     <ItemTemplate>
                         <asp:Button ID="delete" runat="server" CausesValidation="False"
                                                OnClientClick="javascript:return confirm('真的要删除吗?');" Text="删除" CommandArgument='<%# Eval("PS_ID", "{0}") %>' CommandName="MyBtnClicked"  OnCommand="delete_Command"/>
                     </ItemTemplate>
                     </asp:TemplateField>
                </Columns>     
                </asp:GridView>后台代码:
    ds_StoreList = bg.GetPersonalStore(userid);
                StoreList.DataSource = ds_StoreList;
                StoreList.DataBind();