0

我想刷新 UpdatePanel 中的用户控件,但我也想用不同的属性值刷新它。

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

我测试了这段代码,并且正在刷新用户控件,但未应用新值 ID=1、Length=50。

后面的控制代码比较简单

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 标头是

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

当我在页面加载期间从 aspx 页面启动用户控件时,一切正常。在从控制面板回发时,刷新用户控件(使用标签 + 时间检查),但没有值传递给用户控件。

4

1 回答 1

2

删除触发器标签

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

或者

将 asp:Button 移到 UpdatePanel 之外。

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

更新:

请将控件的 setter 和 getter 设置为 -

public string Subject
{
   get  { return txtSubject.Text; }
   set  { txtSubject.Text = value; }
}
于 2013-01-23T22:14:24.427 回答