如何创建的UpdatePanel触发动态动态、UpdatePanel

2023-09-10 14:19:52 作者:Possessiveness(占有欲)

我有我的Default.aspx页一个UpdatePanel,并在UpdatePanel有asp的占位符,我也有一个ascx控件,它是网站的导航,它是动态创建的基础上,在数据库中的数据,每个导航产品一个ImageButton的,我的DataList控件中的每一个循环已经HiddenField URL的值相同值=〜/控制/ somecontrol.ascx

I have an UpdatePanel in my default.aspx page and the UpdatePanel has asp placeholder, also I have an ascx control that is the navigation of the site and it is created dynamically based on the data in the database, each navigation item is an ImageButton, and my each loop in DataList has HiddenField value of the URL for each corresponded ascx control like Value="~/controls/somecontrol.ascx"

下面是我想/需要做的: 我需要动态地创建触发器为我的UpdatePanel即Default.aspx中我ASCX导航控制,所以什么,我究竟希望做的是,我的每个导航项目是一个ImageButton的要为这个UpdatePanel的时候触发你点击它,它会引用占位符的UpdatePanel并加载基于NavigateUrl路径上的控制,做placeholder.Controls.Add(mycontrol)。

Here is what I want/need to do: I need to create triggers dynamically for my UpdatePanel that is in default.aspx in my ascx navigation control, so what I am exactly looking to do is that my each navigation item that is an "ImageButton" to be a trigger for this UpdatePanel and when you click on it, it will reference the placeholder in UpdatePanel and load the control based on the NavigateUrl path and do placeholder.Controls.Add(mycontrol).

Default.aspx页面:

 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:PlaceHolder ID="phMainHolder" runat="server"></asp:PlaceHolder>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

导航ascx控件:

<asp:DataList ID="dlnavigations" runat="server"  RepeatDirection="Horizontal" 
                onitemcommand="dlnavigations_ItemCommand" OnItemDataBound="dlnavigations_ItemDataBound">
                <ItemTemplate>                      
                    <asp:HiddenField ID="hfURL" Value='<%#Eval("strUrl")%>' runat="server" />     

                        <asp:ImageButton ID="imgTab" Width="20"   CommandArgument='<%#Eval("ID")%>'  
                            ImageUrl='<%#Eval("strIcon")%>' runat="server" />                        
                </ItemTemplate>
            </asp:DataList>


protected void dlnavigations_ItemCommand(object source, DataListCommandEventArgs e)
            {
                HiddenField hfURL = (HiddenField)e.Item.FindControl("hfURL");
                Control ctrl = LoadControl(hfURL.Value);
                myplaceholderinUpdatePanel.Controls.Clear();
                //here i need to reference my placeholder in UpdatePanel then
                myplaceholderinUpdatePanel.Controls.Add(ctrl);

            }

我不是很确定这是否是真的有可能做的UpdatePanel请任何帮助拨付,如果我不能做到这一点与UpdatePanel的是有这样做同一个概念的任何其他方式?

I am not very sure if this is really possible to do with UpdatePanel please any help appropriated, if i cant do it with UpdatePanel are there any other way of doing this same concept?

感谢。

推荐答案

如果我已经正确地理解你的要求,我会建议另一种方法:

If i've understood your requirement correctly i would suggest another approach:

设置 UpdatePanel1的的 UpdateMode属性以条件

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:PlaceHolder ID="phMainHolder" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

在你的用户控件,例如名为导航的 引发此事件中的LinkBut​​ton的命令事件 处理的用户控件的导航的 - 活动在您的网页 致电 UpdatePanel1的更新方法编程

Create a custom event in your UserControl, for example called Navigated Raise this event in the LinkButton's Command-Event Handle the UserControl's Navigated-Event in your page Call the UpdatePanel1's Update method programmatically

要澄清的事件驱动的方法的一点,以为这是你的用户控件的codebehind:

To clarify the event driven approach a bit, assume this is your UserControl's codebehind:

public partial class Navigation : System.Web.UI.UserControl
{
   public delegate void NavigationHandler(int ID);
    public event NavigationHandler Navigated;

    void LinkButton_Command(Object sender, CommandEventArgs e)
    {
        int ID=int.Parse(e.CommandArgument.ToString());
        Navigated(ID);
    }
}

和你的页面可以处理此事件以下列方式:

And your page can handle this event in the following way:

protected void Page_Init(object sender, EventArgs e) {
    this.Navigation1.Navigated += UserNavigated;
}

protected void UserNavigated(int ID){
    //do whatever you need to do and then call...
    UpdatePanel1.Update();
}