views:

60

answers:

2

I have a very simple user control that is meant to show certain text if a property is set to true and this is working fine for full page refreshes. I would like to know how I can update just this user control through AJAX, where should I go from here. I am posting my current source but I am not attached to any of it. My only concerns are that a full page refresh not occur and that I would be able to add complexity to the control.

user control:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="basic.ascx.cs" Inherits="ExperimentalPortal.basic" %>    
<p>    
<%
    Response.Write(Text);
    if (Show)
    {
        Response.Write(HiddenText);
    }
    %>    
</p>

user control code behind:

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:

<%@ 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"&gt;

<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>

page code behind:

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();            
    }
}
A: 

Access the properties OnPreRender.

User Control Code-Behind:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    Response.Write(Text);
    if (Show)
    {
        Response.Write(HiddenText);
    }   

 }
rick schott
A: 

I wound up putting the test into a label control. I don't know exactly what is in a label control that does it, but the label control uses AJAX to update.

public void Update()
        {
            UpdateLabel();
            UpdatePanel1.Update();
        }

 private void UpdateLabel()
        {
            String text = ""; 
            // Portal Heading
            text += "<div style=" + HeadingStyle + ">";
            text += "Portal";
            text += "</div>";
            Label1.Text = text;
        }
ProgrammingPope