Reputation: 101
I am new to Using ASP.net WebForms and I'm trying to dynamically update a UserControl which is added to a placeholder. The example I'm working on doesn't update, although the event 'onTextChanged' is getting triggered. Any pointers/suggestions welcome.
WebForm1.aspx
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
MasterPageFile="~/Site.Master" CodeBehind="WebForm1.aspx.cs" Inherits="FFUC.WebForm1" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:Panel runat="server" ID="configPanel" Visible="true">
Book Title:<asp:TextBox ID="tbxBookTitle" runat="server" OnTextChanged="updateBookTitle" AutoPostBack="true"></asp:TextBox>
Book Author:<asp:TextBox ID="tbxBookAuthor" runat="server" OnTextChanged="updateBookAuthor" AutoPostBack="true"></asp:TextBox>
</asp:Panel>
<asp:Panel ID="UpdatePanel1" runat="server">
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
</asp:Panel>
</asp:Content>
WebForm1.aspx.cs
namespace FFUC
{
public partial class WebForm1 : System.Web.UI.Page
{
private UC.WebUserControl1 ctrl1;
protected void Page_Load(object sender, EventArgs e)
{
// Add the control to the page
ctrl1 = (UC.WebUserControl1)Page.LoadControl("UC/WebUserControl1.ascx");
PlaceHolder1.Controls.Add(ctrl1);
}
protected void updateBookTitle(object sender, EventArgs e)
{
ctrl1.BookTitle = tbxBookTitle.Text;
}
protected void updateBookAuthor(object sender, EventArgs e)
{
ctrl1.BookAuthor = tbxBookAuthor.Text;
}
}
}
WebUserControl1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
Inherits="FFUC.UC.WebUserControl1" %>
<h>Books:</h><br />
<asp:Literal runat="server" ID="lblBookTitle" Text="default" Visible="true"></asp:Literal>
<asp:Literal runat="server" ID="lblBookAuthor" Text="default" Visible="true"></asp:Literal>
WebUserControl1.ascx.cs
namespace FFUC.UC
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
private string bookTitle = "book title";
public string BookTitle { get; set; }
private string bookAuthor = "book author";
public string BookAuthor { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
lblBookAuthor.Text = BookAuthor;
lblBookTitle.Text = BookTitle;
}
}
}
Upvotes: 0
Views: 1977
Reputation: 1026
The reason you are not seeing your changed values in the user control is that any change you make only does a partial postback. This means the page is not rendered completely, hence your user control is not rendered. To make a scenario like this work with pure ASP.NET webforms is to use an updatepanel and trigger an update on that.
Upvotes: 1