简体   繁体   English

ASP.net UpdatePanel 动态使用 C# 代码

[英]ASP.net UpdatePanel dynamically using C# code

I have a custom usercontrol that has some Asp.net code.我有一个自定义用户控件,其中包含一些 Asp.net 代码。 I would like to write the same code but with C#.我想使用 C# 编写相同的代码。 The problem is that i don't know how to put a repeater and some buttons into the ContentTemplate.问题是我不知道如何将中继器和一些按钮放入 ContentTemplate。

The Asp.net Code: Asp.net 代码:

<asp:UpdatePanel runat="server" ID="up">

    <ContentTemplate>

        <n2:Repeater ID="rpt" runat="server">
            <ItemTemplate></ItemTemplate>
        </n2:Repeater>

         <asp:LinkButton runat="server" ID="btnFirst" 
           Visible="false" Enabled="false" Text="<<" OnClick="btnFirst_Click" />

    </ContentTemplate>

</asp:UpdatePanel>

So how could I write this chunk in C# code?那么我怎么能在 C# 代码中写这个块呢? To be precise, how could I insert the repeater and the Linkbutton into the ContentTemplate.准确地说,我如何将转发器和链接按钮插入到 ContentTemplate 中。

Note: I don't want to use the LoadTemplate to do it.注意:我不想使用 LoadTemplate 来做到这一点。

Edit编辑

I have tried ContentTemplateContainer.Controls.Add():我试过 ContentTemplateContainer.Controls.Add():

    private UpdatePanel up = new UpdatePanel();
    private Repeater rpt = Repeater();;

    public Paging{      

    //Add repeater to updatePanel
    up.ContentTemplateContainer.Controls.Add(rpt);


     AsyncPostBackTrigger apb3 = new AsyncPostBackTrigger();
     apb3.ControlID = "btnFirst";
     apb3.EventName = "Click";

     //Add Triggers to updatePanel
     up.Triggers.Add(apb1);

      //Create buttons
      btnFirst = new LinkButton();
      btnFirst.Visible = false;
      btnFirst.Enabled = false;
      btnFirst.Text = "<<";
      btnFirst.Click += new EventHandler(btnFirst_Click);

     //Add buttons to update panel
       up.ContentTemplateContainer.Controls.Add(btnFirst);

     }

     protected void Page_Load(object sender, EventArgs e)
     {
        rpt.ItemTemplate = LoadTemplate("~/UI/Templates/NewsEvent.ascx");
       ....
     }

I have an error caused by the first line on Page_Load: Databinding methods such as Eval(), XPath(), and Bind() can only be used in controls contained in a page.我有一个由 Page_Load 上的第一行引起的错误: Eval()、XPath() 和 Bind() 等数据绑定方法只能在页面中包含的控件中使用。

This is NewsEvent.ascx:这是 NewsEvent.ascx:

<img src='<%# Eval("ImageThumbnail") %>' alt="" />

you cant do this with ITemplate types... i had a similar problem trying to clone a tabpanel in a tabcontainer control... i already had a hidden tabpanel and all i wanted to do was create a new tabpanel and basically instantiate the ITemplate from the hidden one in the new one.你不能用 ITemplate 类型做到这一点......我在尝试在 tabcontainer 控件中克隆一个 tabpanel 时遇到了类似的问题......我已经有一个隐藏的 tabpanel,我想要做的就是创建一个新的 tabpanel 并基本上实例化 ITemplate隐藏的一个在新的。

The problem is ITemplate... it's not very dynamic for code behind interactions i would suggest putting that markup on the page as you already have and setting visible = false on the parent then when you need to databind and show the hidden panel.问题是 ITemplate ......对于交互背后的代码来说,它不是很动态我建议将该标记放在页面上,就像你已经拥有的那样,并在父级上设置 visible = false ,然后当你需要数据绑定并显示隐藏面板时。

getting the initial bind to work isn't the problem... its the postback handling...让初始绑定工作不是问题......它的回发处理......

Ajax TabContainerTabPanels Break postbacks Ajax TabContainerTabPanels 中断回发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM