简体   繁体   English

委托,UpdatePanels和ascx控件

[英]Delegates, UpdatePanels and ascx controls

I have built an ascx control that is part of many different components of my application. 我已经构建了一个ascx控件,它是应用程序许多不同组件的一部分。 There is a Previous and Next button on this control, which should be signaled to the parent aspx page. 此控件上有一个“上一个”和“下一个”按钮,应将其发送给父aspx页面。 This is done by having the parent page add some Delegates for postbacks, such as OnPreviousClicked, OnNextClicked etc. 这是通过使父页面为回发添加一些委托来完成的,例如OnPreviousClicked,OnNextClicked等。

Everything in this app is 'ajaxified' with an updatepanel. 此应用程序中的所有内容均已使用updatepanel“ ajaxified”。 Now I notice that my app breaks if I don't set the delegates on every single Page_Load call in the parent. 现在我注意到,如果不对父项中的每个Page_Load调用都设置委托,我的应用程序就会中断。 In other words, if I don't ALWAYS set the delegates in the Page_Load of the parent aspx, then the ascx ends up with null delegates and an exception. 换句话说,如果我不总是在父aspx的Page_Load中设置委托,则ascx最终会出现空委托和一个异常。 Am I coding stuff correctly? 我编码正确吗?

If all you're doing is signaling the clicking of a button, I would have your ASCX control raise a simple event instead. 如果您所做的只是表示要单击按钮,那么我会让您的ASCX控件引发一个简单的事件。 That way each page can listen for the event if they need to and the controls can function regardless if anybody is listening. 这样,每个页面都可以在需要时侦听事件,并且控件可以正常运行,无论是否有人在侦听。

First declare the events in your ASCX codebehind: 首先在您的ASCX代码后面声明事件:

public event System.EventHandler NextSelected;

Then you create your button click events that raise the event. 然后,您创建引发事件的按钮单击事件。

protected void btnNextSelected_Click(object sender, EventArgs e)
{
   if (EmployeeSelected != null) 
   {
      NextSelected(this, new EventArgs());
   }
}

Then in your parent ASP.Net pages you can add your ASCX control (we'll call it NavControl) and create methods that listen for these events. 然后,在您的父ASP.Net页面中,您可以添加ASCX控件(我们将其称为NavControl)并创建侦听这些事件的方法。

NavControl.NextSelected += new EventHandler(NextPageRedirect);

protected void NextPageRedirect(object sender, EventArgs e)
{
    Response.Redirect("~/ViewEmployee.aspx", false);

}

Note that with this you don't have to create the event handler or method and you can still use the nav control on your page. 请注意,您不必创建事件处理程序或方法,仍然可以在页面上使用nav控件。 It should also eliminate the issues you are having with the delegates. 它还应该消除与代表有关的问题。

// inside the control //在控件内部

public event EventHandler OnPreviousClicked;

private void PreviousButton_OnClick(object sender, EventArgs e)
{
  if(OnPreviousClicked != null) {
    OnPreviousClicked(this, e); // or whatever args you want
  }
}

// and inside the Page code-behind //并在Page代码内

private void Page_Load(...)
{
   MyUserControl.OnPreviousClicked += new EventHandler(myHandler);
}

// OR inside the Page aspx, you also could just set the OnPreviousClicked property. //或在Page aspx内部,您也可以只设置OnPreviousClicked属性。

<xx:MyUserControl ID="MyUserControl1" runat="server" OnPreviousClicked="myHandler" />

See http://asp.net-tutorials.com/user-controls/events/ . 参见http://asp.net-tutorials.com/user-controls/events/

remember each postback has to restart your code. 记住每次回发都必须重新启动代码。 The event handlers don't get serialized to viewstate so you have to set them up again. 事件处理程序不会序列化为viewstate,因此您必须再次设置它们。

That is the expected behavior in asp.net. 这是asp.net中的预期行为。

The entire page life cycle occurs even when it is a partial postback (update panel). 即使是部分回发(更新面板),也会发生整个页面生命周期 So in order for the events to fire, you will have to wire them up programmatically during page_load or declaratively in your markup (if those delegates you mentioned are events). 因此,为了触发事件,您将必须在page_load期间以编程方式将其连接起来,或者在标记中以声明方式进行连接(如果您提到的那些委托是事件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM