简体   繁体   English

具有许多用户控件的sharepoint Webpart

[英]sharepoint webpart with many usercontrols

I have created sharepoint 2010 visual webpart in VisualStudio2010 with three user controls (.ascx). 我已经在VisualStudio2010中使用三个用户控件(.ascx)创建了sharepoint 2010可视Web部件。 I want to dynamically change usercontrol in the webpart by clicking some button at currently loaded usercontrol. 我想通过单击当前加载的用户控件上的某些按钮来动态更改Webpart中的用户控件。 The main problem consist in the fact that buttonClick event is handled only after execution CreateChildControls method (where I try to get needed usercontrol using ViewData). 主要问题在于,仅在执行CreateChildControls方法(在此我尝试使用ViewData获取所需的用户控件)后才处理buttonClick事件。 Could anyone please help me to solve this problem? 谁能帮我解决这个问题?

Lee's response is basically right and may work well for you. Lee的回答基本上是正确的,可能对您来说效果很好。 However, you should not just use __doPostBack and rely that it will be always "magically" there for you. 但是,您不应该仅使用__doPostBack并依靠它始终为您“神奇地”存在。 This method and variables mentioned by Lee are internal to ASP.NET and they are not meant to be used directly. Lee提到的这种方法和变量是ASP.NET的内部对象,并不意味着可以直接使用它们。 Also, if you do not place any postback-ing control on your page this method will actually not be generated and your code calling it would fail. 另外,如果您没有在页面上放置任何回发控件,则实际上不会生成此方法,并且调用该方法的代码将失败。

Luckily, the code to cause and handle a generic postback is very simple. 幸运的是,引起和处理通用回发的代码非常简单。 Instead of using built-in event handlers of input controls (which need to be constructed before being triggered - hence the call to CreateChildControls before your handler is called) you would target the postback to the Web Part itself: 代替使用输入控件的内置事件处理程序(需要在触发之前构造该事件处理程序,因此需要在调用处理程序之前调用CreateChildControls ),而是将回发定位到Web部件本身:

public class MyWebPart : WebPart, IPostBackEventHandler {
    protected override void CreateChildControls() {
        Control clickable = ...; // Create a clickable control.
        // Get JavaScript expression to send postback "test" to "this" web part.
        var postBack = Page.ClientScript.GetPostBackEventReference(this, "test");
        clickable.Attributes["onclick"] = postBack + "; return false";
        Controls.Add(clickable);
    }

    void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
        if (eventArgument == "test") { // Recognize and handle our postback.
           ...
        }
    }
}

The GetPostBackEventReference will generate the necessary JavaScript expression for you. GetPostBackEventReference将为您生成必要的JavaScript表达式。 (And actually, just calling it makes the __doPostBack "magically" appear on the page.) The RaisePostBackEvent will be called between OnLoad and OnPreRender . (实际上,只是调用它使__doPostBack “魔术”般地出现在页面上。)的RaisePostBackEvent将与被称为OnLoadOnPreRender Make sure not to cause child controls be created before that (by calling EnsureChildControls , for example, or by any other means). 确保不要在此之前创建子控件(例如,通过调用EnsureChildControls或通过任何其他方式)。 If you need multiple postback-ing controls the eventArguments parameter will let you differ among them. 如果您需要多个回发控件,则eventArguments参数将使您在其中有所不同。

You need the postback triggers in your user controls and not directly in the Web Part. 您需要在用户控件中而不是直接在Web部件中使用回发​​触发器。 I showed it in the Web Part just to keep it simple. 我在Web部件中显示了它只是为了保持简单。 You can put the result of GetPostBackEventReference to any control providing you use the right Page and Web Part instances when calling it. 如果调用时使用正确的Page和Web部件实例,则可以将GetPostBackEventReference的结果放置到任何控件中。

--- Ferda -费尔达

I had that problem too. 我也有这个问题。

Add this to the event handler (after executing your code inside the handler) 将其添加到事件处理程序中(在处理程序中执行代码之后)

this.Page.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri, true);

Regards, 问候,

Pedro 佩德罗

A way to do this would be have the button call a javascript function that in turn calls the following: 一种方法是让按钮调用javascript函数,然后依次调用以下函数:

__doPostBack('LoadControl', 'ControlName');

You can then use the server variables __EVENTTARGET and __EVENTARGUMENT to find out which control to load within your CreateChildControls event handler. 然后,您可以使用服务器变量__EVENTTARGET和__EVENTARGUMENT来查找要在CreateChildControls事件处理程序中加载的控件。

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

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