在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");
三、动态添加控件(由于动态添加的控件在页面回发时会消失,所以建议在OnPreLoad
重新加载)
<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);
}