mko
mko

Reputation: 7325

How to update user control in UpdatePanel?

I want to refresh a user control in UpdatePanel, but I would also like to refresh it with different property values.

<asp:UpdatePanel runat=server ID=up1>
<Triggers>
            <asp:AsyncPostBackTrigger controlid="but01" eventname="Click" />
        </Triggers>
<ContentTemplate>
<asp:Button runat="server" Text="Test" ID="but01"  />   
<UC:Uc runat=server ID="Uc1" />
</ContentTemplate>
</asp:UpdatePanel>



Codebehind for but01 click is
void but01_Click(object sender, EventArgs e)
{

this.Uc1.ID = 1;
this.Uc1.Length = 50;
}

I tested this code, and the user control is being refreshed, but new values ID=1, Length=50 are not applied.

Control code behind is rather simple

namespace Admin.Web.Controls
{

    public partial class Uc1 : System.Web.UI.UserControl
    {

        private string p_to;
        private string p_from;
        private string p_subject;
        private string p_body; 
        private string p_priority;
    }


        protected void Page_Load(object sender, EventArgs e)
        {

            this.txtFrom.Text = p_from;
            this.txtTo.Text = p_to;
            this.txtSubject.Text = p_subject;
            this.txtBody.Text = p_body;

        }

        public string Subject
        {
            get
            {
                return p_subject;
            }
            set
            {
                p_subject = value;
            }
        }

        public string From
        {
            get
            {
                return p_from;
            }
            set
            {
                p_from = value;
            }
        }

        public string To
        {
            get
            {
                return p_to;
            }
            set
            {
                p_to = value;
            }
        }

        public string Body
        {
            get
            {
                return p_body;
            }
            set
            {
                p_body = value;
            }
        }

}

ascx header is

<%@ Control Language="c#" Inherits="Admin.Web.Controls.Uc1" AutoEventWireup="true" Codebehind="Uc1.ascx.cs"  %> 

When I initiate user control from aspx page during page load, everything is ok. On postback from control panel, user control is refreshed (checked with Label + time), but no values are passed to user control.

Upvotes: 0

Views: 8345

Answers (1)

Win
Win

Reputation: 62260

Remove Triggers tag

http://msdn.microsoft.com/en-us/library/Bb399001(v=vs.100).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

OR

Move asp:Button to outside of UpdatePanel.

http://msdn.microsoft.com/en-us/library/Bb399001(v=vs.100).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-5

Update:

Please make the setter and getter of the control as -

public string Subject
{
   get  { return txtSubject.Text; }
   set  { txtSubject.Text = value; }
}

Upvotes: 2

Related Questions