我在使用UpdatePanel进行部分页面刷新时,动态添加控件遇到了问题。我阅读了许多关于动态控件的文章,并且知道如何在postbacks中添加和维护它们,但是其中大部分信息不适用于并不能用于局部页面刷新。我找不到任何有用的关于如何在UpdatePanels中添加和维护这些控件的信息,如果可能的话,我希望不用创建Web服务就能实现。是否有人有什么想法或参考资料可以提供?
我在使用UpdatePanel进行部分页面刷新时,动态添加控件遇到了问题。我阅读了许多关于动态控件的文章,并且知道如何在postbacks中添加和维护它们,但是其中大部分信息不适用于并不能用于局部页面刷新。我找不到任何有用的关于如何在UpdatePanels中添加和维护这些控件的信息,如果可能的话,我希望不用创建Web服务就能实现。是否有人有什么想法或参考资料可以提供?
我认为这是asp.net程序员常见的陷阱之一,但只要知道发生了什么(始终记得你的ViewState!),就不难做到正确。
下面的代码说明了如何完成这些工作。 这是一个简单的页面,用户可以在菜单上单击,触发一个动作,该动作将在UpdatePanel内向页面添加一个用户控件。
(此代码从此处借用,并且还有更多有关此主题的信息)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SampleMenu1.aspx.cs" Inherits="SampleMenuPage1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Sample Menu</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick">
<Items>
<asp:MenuItem Text="File">
<asp:MenuItem Text="Load Control1"></asp:MenuItem>
<asp:MenuItem Text="Load Control2"></asp:MenuItem>
<asp:MenuItem Text="Load Control3"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
<br />
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Menu1" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
和
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class PlainSampleMenuPage : System.Web.UI.Page
{
private const string BASE_PATH = "~/DynamicControlLoading/";
private string LastLoadedControl
{
get
{
return ViewState["LastLoaded"] as string;
}
set
{
ViewState["LastLoaded"] = value;
}
}
private void LoadUserControl()
{
string controlPath = LastLoadedControl;
if (!string.IsNullOrEmpty(controlPath))
{
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
}
}
protected void Page_Load(object sender, EventArgs e)
{
LoadUserControl();
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
MenuItem menu = e.Item;
string controlPath = string.Empty;
switch (menu.Text)
{
case "Load Control2":
controlPath = BASE_PATH + "SampleControl2.ascx";
break;
case "Load Control3":
controlPath = BASE_PATH + "SampleControl3.ascx";
break;
default:
controlPath = BASE_PATH + "SampleControl1.ascx";
break;
}
LastLoadedControl = controlPath;
LoadUserControl();
}
}
对于后台代码,基本上就是这样。您可以清楚地看到,在用户单击菜单项时(将向服务器发送异步回发),动态将控件添加到页面中(实际上是在updatePanel内的placeHolder内部),同时使用LastLoadedControl保留viewstate。
更多信息也可以在此处找到:
当然,您还可以在这个网站上查看示例代码。
试试这个:
Literal literal = new Literal();
literal.Text = "<script type='text/javascript' src='http://www.googleadservices.com/pagead/conversion.js'>";
UpdatePanel1.ContentTemplateContainer.Controls.Add(literal);
您可以用任何想要的HTML内容替换literal的内容...
我遇到了一个问题,当处理事件时使用上述方法时,LoadUserControl()会被调用两次。我阅读了一些其他文章,并想向您展示我的修改:
1)使用LoadViewstate而不是Page_Load来加载用户控件:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (!string.IsNullOrEmpty(CurrentUserControl))
LoadDataTypeEditorControl(CurrentUserControl, panelFVE);
}
2) 加载用户控件时不要忘记设置控件ID:
private void LoadDataTypeEditorControl(string userControlName, Control containerControl)
{
using (UserControl myControl = (UserControl) LoadControl(userControlName))
{
containerControl.Controls.Clear();
string userControlID = userControlName.Split('.')[0];
myControl.ID = userControlID.Replace("/", "").Replace("~", "");
containerControl.Controls.Add(myControl);
}
this.CurrentUserControl = userControlName;
}