按钮单击事件不ASP .NET中使用控制范围之内发射 [英] Button click event not firing within use control in ASP .Net

查看:152
本文介绍了按钮单击事件不ASP .NET中使用控制范围之内发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个asp网页中,我有一个下拉组合框,并低于一个占位符。当用户选择从下拉组合框中的项目,回发是为了在服务器端和服务器加载一个asp的用户控制在这个父页面的占位符。一切都高达现在工作正常。

在用户控件我有一个按钮,用户控制code后面的实施是为处理按钮单击事件。问题是,当我点击这个按钮,我可以看到,回发是发送到服务器端(即父页面的Page_Load()在调试模式下调用),但两者)用户控件的Page_Load中(或按钮单击事件处理程序不被调用。

请帮助..

一些额外的信息,


  1. 我的父页面是不是一个asp母版页。只是一个简单的ASP页。

  2. 我使用VS2008和.Net 3.5 SP1和C#。


解决方案

您需要确保用户控件存在,所以当ViewState是重建被触发按钮单击事件。

在Page_Load加载你的用户控件将工作的第一次。当您单击按钮,post_back时,Page_Load中尚未发生。这意味着用户控件将不存在,这意味着该按钮不存在被有线备份事件。所以在它的按钮,用户控件不能连接到Click事件和click事件不会火。

建议您的用户控件在此事件中加载。

 保护覆盖无效的OnLoad(EventArgs的发送)
{
    base.OnLoad(E);
    // - 创建您的控件在这里
}

尝试使用沙盒测试。在Page_Load中,动态地创建在Page_Load单击事件的按钮。你会看到,单击事件不火。现在移动的按钮OnLoad事件。单击事件将触发。还要注意,click事件将在Page_Load事件发生之前。进一步证明了按钮不会在正确的时间存在。

另一个想法......

您在页面上的按钮在事件发生之前重新加载用户控件。确保您的LoadControl方法是如果块内

 如果(!的IsPostBack)
{
    //加载用户控件
}

I am developing an asp web page in which I have a drop down combobox and a place holder below that. When the user selects an item from the drop down combobox, a postback is done to the server side and server loads an asp user control to the place holder in this parent page. Everything upto now is working fine.

In the user control I have a button and the user control code behind is implemented to handle the button click event. The problem is, when I click this button, I can see that the postback is send to the server side (i.e. parent page Page_Load() is invoked in debug mode), but both the user control's Page_Load() or button click event handler is not invoked.

Please help..

Some additional information,

  1. My parent page is not an asp master page. Just a simple asp page.
  2. I am using VS2008 and .Net 3.5 SP1 and C#.

解决方案

You need to ensure that you UserControl exists so the button click event is triggered when viewstate is rebuilt.

Loading your UserControl in the Page_Load will work the first time. When you click the button and the post_back occurs, Page_Load has not occurred yet. This means the UserControl will not exist, which mean the button does not exist for the event to be wired back up. So the UserControl with the button in it cannot be connected to the click event and the click event wont fire.

Recommend that your user control is loaded in this event.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    //-- Create your controls here
}

Try a sandbox test. In page_load, dynamically create a button with a click event in the Page_Load. You will see that the click event does not fire. Now move the button to the OnLoad event. The click event will fire. Also note, the click event will occur before the Page_Load event. Further proof that the button does not exist at the right time.

Another idea...

You are reloading the usercontrol on the page before the button event occurs. Ensure your LoadControl method is inside the If block

if (!IsPostBack)
{
    //load usercontrol
}

这篇关于按钮单击事件不ASP .NET中使用控制范围之内发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆