[英]add controls to ajax update panel programatically on click of a button in asp.net
我想在按钮上单击以添加标签以更新面板。.aspx文件中有以下代码...
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false" >
<ContentTemplate >
<asp:Panel runat="server" ID="myPanel" >
<label id="ssd" runat="server" >abc</label>
</asp:Panel>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="test"/>
</ContentTemplate>
</asp:UpdatePanel>
然后点击事件。
protected void Button1_Click(object sender, EventArgs e)
{
Label l1 = new Label();
l1.ID = "label1";
l1.Text = "this is it...";
up.ContentTemplateContainer.Controls.Add(l1);
}
但它不起作用.. :-(
由于UpdatePanel
的UpdateMode设置为Conditional
,因此您需要从代码背后手动更新它:
protected void Button1_Click(object sender, EventArgs e)
{
Label l1 = new Label();
l1.ID = "label1";
l1.Text = "this is it...";
up.ContentTemplateContainer.Controls.Add(l1);
up.Update();
}
MSDN UpdatePanel.Update
方法
如果您打算使用Update方法,请将UpdateMode属性设置为Conditional。 如果要决定用服务器逻辑更新面板,请确保ChildrenAsTriggers属性为false,并且没有为面板定义显式触发器。
在典型的页面开发方案中,如果定义触发器或UpdatePanel控件的ChildrenAsTriggers属性为true,则在页面生命周期内将自动调用Update方法。
请注意,即使使用ASP.NET Ajax,也需要重新创建动态控件。 因此,您需要最迟在page_load
的下一次回发中手动创建标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.