我尝试创建动态添加控件的应用程序。我有主页,我的asp:内容在这里:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server">
</asp:ScriptManager>
<div style="margin: 10px">
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="myPlaceHolder" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
<asp:Button ID="btnAdd" runat="server" Text="Add" />
在btnAdd中单击后,我想添加两个文本框。我试着像在http://jagdeepmankotia.wordpress.com/2010/01/30/dynamically-add-controls-in-asp-net-c/里那样做
这是我的代码:
static int myCount = 1;
private TextBox[] color;
private TextBox[] text;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
color = new TextBox[myCount];
text = new TextBox[myCount];
for (int i = 0; i < myCount; i++)
{
TextBox tbColor = new TextBox();
tbColor.ID = "colorTextBox" + i.ToString();
myPlaceHolder.Controls.Add(tbColor);
color[i] = tbColor;
TextBox tbText = new TextBox();
tbText.ID = "textTextBox" + i.ToString();
myPlaceHolder.Controls.Add(tbText);
text[i] = tbText;
LiteralControl literalBreak = new LiteralControl("<br />");
myPlaceHolder.Controls.Add(literalBreak);
}
}
public Control GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control mycontrol = page.FindControl(ctl);
if (mycontrol is System.Web.UI.WebControls.Button)
{
control = mycontrol;
// This gives you ID of which button caused postback
break;
}
}
}
return control;
}
protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
if (myControl != null)
if (myControl.ClientID.ToString() == "btnAdd")
myCount = myCount + 1;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//handled in PreInit
}当在loap foreach中的函数GetPostBackControl()中查找我的btnAdd时,例如在ctr "ctl00$MainContent$scriptManager1“的第一次迭代中,myControl为null...在下一个迭代中也是如此。所以我的函数总是返回null。什么是理由呢?
发布于 2011-12-21 20:42:33
FindControl只搜索容器的直接子容器。由于您是从页面级别开始的,因此需要递归子UpdatePanel控件才能到达btnAdd控件。
看一看here,看看如何做到这一点的示例。
编辑:我不太明白为什么你要以这种方式“寻找”你的按钮,因为屏幕上只有一个静态按钮--在这种情况下,你不需要使用FindControl。
<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />(或者在代码中,btnAdd.OnClick += new EventHandler(btnAdd_Click);)
即使在窗体中动态添加了多个按钮,也可以将所有这些按钮连接到相同的Button Click处理程序,在这种情况下,sender将包含被单击的Button控件。您通常会使用FindControl从动态添加的输入控件(文本框等)中抓取数据,而不是查看是哪个控件导致了回发(因为在适当的事件处理程序中使用“发送者”会更容易)
编辑2:您可以像其他控件一样动态添加按钮
Button myButton = new Button();
myButton.Text = "Click Me";
myButton.Click += new EventHandler(btnAdd_Click);
myPlaceHolder.Controls.Add(myButton); 如果您希望已添加的所有控件在回发之间“停留”,则在页面和控件上启用视图状态,然后确保在OnInit中只添加一次没有回发的控件:
base.OnInit(e);
if (!IsPostBack)
{ // ... Add controls here你可以将'mycount‘的状态保存在一个隐藏的字段中(在同一个updatepanel中,并且启用了viewstate )--每次你都需要把它解析成一个int。或者你可以使用SessionState来追踪它。
https://stackoverflow.com/questions/8589950
复制相似问题