简体   繁体   English

使用AJAX动态加载ASCX控件

[英]Load ASCX controls dynamically using AJAX

I'm developing an ASP.NET application and I'm trying to do the following: 我正在开发ASP.NET应用程序,并且尝试执行以下操作:

I'm going to have only one ASPX page splitted into two columns. 我将只有一个ASPX页面分为两列。 On the left column is going to be a TreeView, and on the right column is going to be something to edit treeview's nodes. 左列将是TreeView,右列将是编辑treeview的节点。

When the user can select a treeview's node to edit on the right column. 当用户可以选择树视图的节点以在右列上进行编辑时。 Depending on the node's depth fields on right column will vary. 取决于节点的深度,右侧列将有所不同。

I wondering to use ASCX controls and load on right column dynamically using AJAX, for example. 我想知道如何使用ASCX控件并使用例如AJAX在右列上动态加载。 Is there a better choice? 有更好的选择吗? Can I do that? 我可以这样做吗?

EDIT: 编辑:

I don't want to reload the entire page when the user wants to edit a treeview's node. 当用户想要编辑树视图的节点时,我不想重新加载整个页面。 Maybe I'm going to need an UpdatePanel on the right column, isn't it? 也许我会在右列上需要一个UpdatePanel,不是吗?

In general, yes this can be done and is not so hard to accomplish with the different .NET ajax frameworks. 通常,是可以做到的,并且使用不同的.NET ajax框架很难完成。

It is difficult to suggest a "better choice" because that depends on how you build your application and the different requirements for it. 很难提出“更好的选择”,因为这取决于您如何构建应用程序以及对它的不同要求。

Wrap your treeview inside an UpdatePanel, and add the following code in the code-behind. 将树形视图包装在UpdatePanel内,并在后面的代码中添加以下代码。 (assuming your right panel is called 'PanelOnTheRight', and you have a usercontrol 'MyEditControl' with a property 'IdToEdit'). (假设您的右面板名为“ PanelOnTheRight”,并且您有一个用户控件“ MyEditControl”,其属性为“ IdToEdit”)。

void MyTreeView_SelectedNodeChanged(Object sender, EventArgs e)
{
    PanelOnTheRight.Controls.Clear();

    MyEditControl editControl = LoadControl("~/usercontrols/mycontrol.ascx");
    editControl.IdToEdit = ((TreeView)sender).SelectedNode.Value;

    PanelOnTheRight.Controls.Add(editControl);
}

You can use Page.LoadControl method to load user controls. 您可以使用Page.LoadControl方法加载用户控件。 But I am not sure whether it works with Ajax 但我不确定它是否适用于Ajax

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

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