在Asp.net中動態新增css檔案,js檔案,控制元件
一、動態新增CSS檔案
HtmlLink link = new HtmlLink();
link.Href = cssUrl; //路徑
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
Page.Header.Controls.Add(link);
二、動態新增JS檔案(通過jquery實現)
$.getScript("test.js");
三、動態新增控制元件(由於動態新增的控制元件在頁面回發時會消失,所以建議在
重新載入)
<asp:Panel ID="pnlAddressContainer" runat="server">
</asp:Panel>
protected void AddAdress(string id)
{
Label lb = new Label();
lb.Text = "Address" + id + ": ";
TextBox tb = new TextBox();
tb.ID = "TextBox" + id;
tb.Text = Request.Form[tb.ID];
Button btn = new Button();
btn.Text = "Check";
btn.ID = "Button" + id;
btn.Click += new EventHandler(ClickEvent);
pnlAddressContainer.Controls.Add(lb);
pnlAddressContainer.Controls.Add(tb);
pnlAddressContainer.Controls.Add(btn);
}
四