我有一个非常简单的用户控件,用于在属性设置为true时显示某些文本,这对于完整页面刷新工作正常。我想知道如何通过AJAX更新这个用户控件,我应该从哪里开始。我发布了我当前的来源,但我并不依赖于任何一个。我唯一担心的是不会发生整页刷新,并且我可以增加控件的复杂性。
用户控制:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="basic.ascx.cs" Inherits="ExperimentalPortal.basic" %>
<p>
<%
Response.Write(Text);
if (Show)
{
Response.Write(HiddenText);
}
%>
</p>
后面的用户控制代码:
public partial class basic : System.Web.UI.UserControl
{
public String Text { get; set; }
public String HiddenText { get; set; }
public bool Show { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
}
页:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Basic.aspx.cs" Inherits="ExperimentalPortal.Basic" %>
<%@ Register TagPrefix="customControl" TagName="NavigationBar"
Src="basic.ascx" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<customControl:NavigationBar ID="navBar1" Text="Hello" HiddenText="Hiding" runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
页面代码背后:
public partial class Basic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
Label1.Text = "refreshed";
}
protected void Button1_Click(object sender, EventArgs e)
{
navBar1.Show = true;
UpdatePanel1.Update();
}
}
答案 0 :(得分:0)
访问OnPreRender属性。
用户控制代码 - 背后:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Response.Write(Text);
if (Show)
{
Response.Write(HiddenText);
}
}
答案 1 :(得分:0)
我将测试放入标签控件中。我不确切知道标签控件中的内容是什么,但标签控件使用AJAX进行更新。
public void Update()
{
UpdateLabel();
UpdatePanel1.Update();
}
private void UpdateLabel()
{
String text = "";
// Portal Heading
text += "<div style=" + HeadingStyle + ">";
text += "Portal";
text += "</div>";
Label1.Text = text;
}