动态... ..aspx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="xx.ascx.cs" Inherits="xx.layouts.xxSublayout" %>
<%@ Register Assembly="System.Web" Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:LiteralControl runat="server" ID="openingTag" />
<asp:PlaceHolder runat="server" id="contentPanel" />
<asp:LiteralControl runat="server" ID="closingTag" />
后面的代码..将带有文本的 div 添加到 PAGE_LOAD 以外的某些方法中...
var inputWrapper = new HtmlGenericControl("div"){ID = "d1"};
inputWrapper.Attributes.Add("class", "yourcssclass");
inputWrapper.InnerText = "hghjggjg";
contentPanel.Controls.Add(inputWrapper);
坚持标签回发...
//add div ID to list, add list to session so it can be recreated on the page_INIT, this must be done or btn will NOT exist when "Get vCard" is clicked.
PostbackIDs.Add(inputWrapper.ID);
Session["pb"] = PostbackIDs;
在初始化...
//Must recreate controls from session, so label is existing
if (Session["pb"] != null)
{
var pblist = (List<string>)Session["pb"];
foreach (var id in pblist)
{
// label
var inputWrapper= new HtmlGenericControl("div") { ID = id };
inputWrapper.Attributes.Add("class", "yourcssclass");
contentPanel.Controls.Add(inputWrapper);
}
}
由于在页面加载之前触发了 INIT 方法,因此控件的 ID 将就位以向其添加文本。