【问题标题】:Updating an Update Panel in a usercontrol from a control in another user control从另一个用户控件中的控件更新用户控件中的更新面板
【发布时间】:2012-10-16 03:22:31
【问题描述】:

首先,为这个问题的长度道歉!

我有一个页面,上面有几个 Web 用户控件,其中两个有些相互依赖。在理想的世界中,它们可以是一个控件,但出于各种原因,它们必须是两个控件。

我需要根据另一个控件中下拉列表的操作来更新其中一个控件中的更新面板,如下所述。

为此,我们将控件称为 JobControlCallControl。 JobControl 包含一个下拉列表,它是 AJAXControlToolkit 中级联下拉列表的一部分。当此下拉列表具有选定的索引更改时,我想更新 CallControl 中的控制面板。

CallControl 公开它的更新面板:

    public UpdatePanel UpdatePanel
    {
        get { return updCall; }
    }

JobControl 有一个公共成员 AssociatedCallControl

private ServiceCallControl associatedCallControl;

public ServiceCallControl AssociatedCallControl
{
    get { return associatedCallControl; }
    set { associatedCallControl = value; }
}

然后在包含控件的页面的 OnLoad 事件中将两者关联在一起。

这个 SO 问题:Update Panel error: Control with the ID "xxx" could not be found in the UpdatePanel 让我在 JobControl 的 onload 事件中尝试了这个:

if (associatedCallControl != null)
{
    AsyncPostBackTrigger trig = new AsyncPostBackTrigger();
    string s = ddCallGroup.ClientID;
    //ddCallGroup is the dropdown I want to trigger the update of the CallControl
    trig.ControlID = ddCallGroup.ClientID; //Also Tried ddCallGroup.ID
    trig.EventName = "CallGroupChanged";
    associatedCallControl.UpdatePanel.Triggers.Add(trig);
}

以下也添加到 JobControl 中

public void CallGroupChanged(object sender, EventArgs e)
{
     //Stuff to update the CallControl panel including calling update();
     associatedCallControl.RefreshMehods(int.Parse(ddCallGroup.SelectedValue));        
}

悬停在这一切之后我仍然得到A control with ID 'TabContainer1_tabJob_ctrlJob_ddCallGroup' could not be found for the trigger in UpdatePanel 'updCall'.

我在尝试不可能的事情吗?我是不是走错了路,还是我错过了什么?

【问题讨论】:

    标签: c# asp.net updatepanel webusercontrol


    【解决方案1】:

    如果可以的话,试试这个, - 在 CallControl 中创建和调用 EventHandler 委托; - 将其指向当前页面中的方法; - 在这个方法中,调用简单

    JobCtrl.UpdatePanel.Update();

    希望对您有所帮助!

    编辑:代码示例

    CallControl.ascx.cs:

    public partial class JobControl
    {
        public void CallGroupChanged(object sender, EventArgs e)
        {
            // do your work
    
            if (this.MyEventDelegate != null) // check if the event is not null
                this.MyEventDelegate(this, null); // invoke it
        }
    
        public event EventHandler MyEventDelegate;
    }
    

    页面.aspx:

    <controls:CallControl runat="server" ID="CallControl1" OnMyEventDelegate="RefreshMethod" />
    

    Page.aspx.cs:

    public partial class Page_aspx : System.Web.UI.Page
    {
        protected void RefreshMethod(object sender, EventArgs e)
        {
            this.CallControl1.UpdatePanel.Update();
        }
    }
    

    希望这很清楚..!

    【讨论】:

    • 您能否添加一个创建和调用事件处理程序委托的示例。这也将实际进入 JobControl,因为这是具有我想用作触发器的下拉列表的那个。更新面板在 CallControl 中?如果可能的话,我还想尝试将方法保留在控件本身中。感谢您迄今为止的帮助。
    • 我已编辑您的答案以将包含更新的控件更改为 CallControl。实际上,我在上面的代码中搞砸了其他领域的其他一些发现,所以我将发布完整的解决方案作为答案。但是非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    相关资源
    最近更新 更多