简体   繁体   English

如何在后面的代码中而不是在aspx设计中向数据列表添加控件?

[英]How to add controls to datalist in code behind, not in aspx design?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Data;
using MyWeb.Common;
using MyWeb.Business;
using AjaxPro;
using System.Text;

<asp:DataList ID="DataList1" runat="server" RepeatLayout="Flow">
                <ItemTemplate>

                    <div class="m_status">
                <div class="ava">
                    <a href="#">
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("AvatarUS") %>' 
                        CssClass="ava_medium"/></a>
                </div>
                <div class="sta_content">
                    <div class="sta_title">

                        <p class="sta_detail">
                            <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Detail") %>'></asp:Literal>
                            </p>
                        <div class="function">
                            <a class="sta_like left" href="#">Thích</a>
                            <a class="sta_cmt left" href="#">Bình luận</a>
                            <a class="sta_share left" href="#">Chia sẻ</a>
                        </div>
                    </div>

                    </div>                                                  
                </div>
                </ItemTemplate>
                </asp:DataList>

I want to add a DataList like that in code behind(Default.aspx.cs) 我想在后面的代码中添加一个像这样的DataList(Default.aspx.cs)
How to make it? 怎么做?

In the row databound event of the data list you can add some control that will hold the place for the control that you want to add, example: 在数据列表的行数据绑定事件中,您可以添加一些控件,该控件将保留要添加的控件的位置,例如:

In the ItemTemplate add one control to hold the control that will be addeded. 在ItemTemplate中添加一个控件以保存将要添加的控件。

    <asp:Panel runat="server" ID="pnlPlaceHolder">
           <!-- the control will be added here -->
    </asp:Panel>

and in code behind, on RowDataBound of the data list do the following 在后面的代码中,在数据列表的RowDataBound上执行以下操作

Panel pnlPlaceHolder = (Panel)e.Item.FindControl("pnlPlaceHolder");
if (pnlPlaceHolder!= null)
{
     // The new control, a button, by example          
     Button btn = new Button();
     btn.Text = "Added dinamically";
     pnlPlaceHolder.Controls.Add(btn);
}

This is it. 就是这个。

Problem when I add in code behide: Default.aspx.cs 当我添加代码隐藏时出现问题:Default.aspx.cs

    namespace KetBanBonPhuong
    {
           [AjaxPro.AjaxNamespace("Default")]

           public partial class Default1 : System.Web.UI.Page
           {
               protected void Page_Load(object sender, EventArgs e)
               {
                    AjaxPro.Utility.RegisterTypeForAjax(typeof(Default1));
                    if(!isPostBack)
                    {
                DataList dl = new DataList();
                dl.DataSource = GetList();
                dl.DataBind();
                this.liststatus.Controls.Add(dl);
                dl.DataSource = GetList();
                dl.RepeatLayout = RepeatLayout.Flow;
                Literal ltr = new Literal();
                ltr.Text = "kaldfs";

                dl.Controls.Add(ltr);//Error here
             }
          }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM