因此,我正在尝试学习一些使用C#构建页面的内容,而且我确实无法获得这些内容。 我有以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
UpdatePanel mainContainer = new UpdatePanel();
protected void Page_Load(object sender, EventArgs e)
{
Label someText = new Label();
someText.Text = "text";
LinkButton someLink = new LinkButton();
someLink.Text = "first link";
someLink.Click += delegate {
Label someText2 = new Label();
someText2.Text = "text2";
LinkButton someLink2 = new LinkButton();
someLink2.Text = "second link";
someLink2.Click += delegate {
Label someText3 = new Label();
someText3.Text = "text3";
LinkButton someLink3 = new LinkButton();
someLink3.Text = "third link";
mainContainer.ContentTemplateContainer.Controls.Add(someText3);
mainContainer.ContentTemplateContainer.Controls.Add(someLink3);
};
mainContainer.ContentTemplateContainer.Controls.Add(someText2);
mainContainer.ContentTemplateContainer.Controls.Add(someLink2);
};
form1.Controls.Add(mainContainer);
mainContainer.ContentTemplateContainer.Controls.Add(someText);
mainContainer.ContentTemplateContainer.Controls.Add(someLink);
}
}
总而言之,它应该提供一些非常简单的东西,一个UpdatePanel充当包装器,包含:“text”字符串后跟“first link”链接,点击“first link”应附加另一个字符串,最初的旁边的另一个链接。反过来,第二个链接应添加新的第三个字符串和新的第三个链接。但是,单击第二个链接只会导致从DOM中删除第二个文本和第二个链接。我甚至不能在代理中为第二个链接设置断点。我老实说不知道为什么会这样。有什么想法吗?
我的目的是根据以编程方式添加的对象的点击次数继续向网站添加html对象,这不是很容易吗?
答案 0 :(得分:0)
为了更好地实现这个想法:
假设您有两个控件Controls / Control1.ascx和Controls / Control2.ascx
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:LinkButton runat="server" Text="PressMe 1" OnClick="ChangePage_Click" CausesValidation="false" CommandArgument="~/Controls/Control1.ascx" />
<asp:LinkButton runat="server" Text="PressMe 2" OnClick="ChangePage_Click" CausesValidation="false" CommandArgument="~/Controls/Control2.ascx" />
<asp:PlaceHolder runat="server" ID="placeholder" />
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack && !string.IsNullOrEmpty(ActiveControl)) {
placeholder.Controls.Clear();
placeholder.Controls.Add(LoadControl(ActiveControl));
}
}
protected string ActiveControl {
get { return (string)ViewState["ActiveControl"]; }
set { ViewState["ActiveControl"] = value; }
}
protected void ChangePage_Click(object sender, EventArgs e) {
var control = (IButtonControl)sender;
ActiveControl = control.CommandArgument;
placeholder.Controls.Clear();
placeholder.Controls.Add(LoadControl(control.CommandArgument));
}