当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - RaisePostBackEvent: raise custom or default postback event method based upon the user control that caused the postback

问题描述:

I have a ASP.NET page that contains both javascript and ASP.NET user controls. When a postback is executed, I use the RaisePostBackEvent method to determine which method should be called. That way, it's possible to trigger a postback via javascript. For example, I load a User Control via a postback that was triggered via javascript. However, the events of such a user control can't be raised via postback anymore, because the RaisePostBackEvent method is called, rather than de default postback behaviour. So how can I call the default postback behaviour on a user control, from within the RaisePostBackEvent method?

public void RaisePostBackEvent (string eventArgument) {

string eventTarget = Request ["__EVENTTARGET"];

if (eventTarget == UniqueID) {

eventArgument = Request ["__EVENTARGUMENT"];

Action action = JsonConvert.DeserializeObject<Action>(eventArgument);

if (action.name == "display") Display(action.argument);

else if (action.name == "edit") Edit(action.edit);

}

else {

// Control control = GetPostBackControl(this.Page);

// !!! Raise the postback event on the control.

}

}

网友答案:

You could put postback logic into a separate method:

void Button_Click(object sender, EventArgs e)
{
   DoButtonLogic();
}

so you could call it also from RaisePostback:

...
else 
{
   DoButtonLogic();
}
网友答案:

I had this statement in the OnInit event method of the user control containing the javascript:

protected override void OnInit (EventArgs e) {
    Page.RegisterRequiresRaiseEvent(this);
}

When this statement is removed, it works.

分享给朋友:
您可能感兴趣的文章:
随机阅读: