简体   繁体   English

按钮单击事件不在ASP .Net中的使用控件内触发

[英]Button click event not firing within use control in ASP .Net

I am developing an asp web page in which I have a drop down combobox and a place holder below that. 我正在开发一个asp网页,其中我有一个下拉组合框和一个占位符。 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. 当用户从下拉组合框中选择项目时,对服务器端进行回发,并且服务器将asp用户控件加载到该父页面中的占位符。 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 (ie 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. 问题是,当我单击此按钮时,我可以看到回发被发送到服务器端(即在调试模式下调用父页面Page_Load()),但是用户控件的Page_Load()或按钮单击事件处理程序都是没有被调用。

Please help.. 请帮忙..

Some additional information, 一些额外的信息,

  1. My parent page is not an asp master page. 我的父页面不是asp母版页。 Just a simple asp page. 只是一个简单的asp页面。
  2. I am using VS2008 and .Net 3.5 SP1 and C#. 我正在使用VS2008和.Net 3.5 SP1和C#。

You need to ensure that you UserControl exists so the button click event is triggered when viewstate is rebuilt. 您需要确保UserControl存在,以便在重建viewstate时触发按钮单击事件。

Loading your UserControl in the Page_Load will work the first time. 在Page_Load中加载UserControl将首次运行。 When you click the button and the post_back occurs, Page_Load has not occurred yet. 单击按钮并发生post_back时,尚未发生Page_Load。 This means the UserControl will not exist, which mean the button does not exist for the event to be wired back up. 这意味着UserControl将不存在,这意味着该事件无需连接备份按钮。 So the UserControl with the button in it cannot be connected to the click event and the click event wont fire. 因此,带有按钮的UserControl无法连接到click事件,并且click事件不会触发。

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. 在page_load中,在Page_Load中动态创建一个带有单击事件的按钮。 You will see that the click event does not fire. 您将看到click事件不会触发。 Now move the button to the OnLoad event. 现在将按钮移动到OnLoad事件。 The click event will fire. 点击事件将触发。 Also note, the click event will occur before the Page_Load event. 另请注意,click事件将发生在Page_Load事件之前。 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. 您正在按钮事件发生之前在页面上重新加载usercontrol。 Ensure your LoadControl method is inside the If block 确保LoadControl方法位于If块内

if (!IsPostBack)
{
    //load usercontrol
}

Default.aspx Default.aspx的

<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>

Default.aspx.cs Default.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
 {
     var ctl = LoadControl("Controls/UserControl.ascx");
     ph1.Controls.Add(ctl);
}

UserControl.ascx UserControl.ascx

<h3>User control</h3>
<asp:Button ID="btn1" runat="server" OnClick="btn1_Click" Text ="Click me" />

UserControl.ascx.cs UserControl.ascx.cs

protected void btn1_Click(object s, EventArgs e)
{
    Response.Write("You clicked me, yay");
}

All works like a charm. 一切都像魅力。 I see the "You clicked me, yay" written when I click the button 当我点击按钮时,我看到“你点击了我,yay”

Point of attention . 关注点 If you try to load the controls dynamically in your example in the handler for SelectedItemChanged event of the dropdown control, it will fail, because of the way that lifecycle works for ASP.Net page. 如果您尝试在下拉控件的SelectedItemChanged事件的处理程序中动态加载控件,则它将失败,因为生命周期对ASP.Net页面起作用的方式。 Instead you should handle such control creation in the PageLoad event of the page, like this example below Default.aspx 相反,您应该在页面的PageLoad事件中处理此类控件创建,例如Default.aspx下面的示例

<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true">
    <asp:ListItem Value="0" Text="--select a value--" />
    <asp:ListItem Value="1" Text="User control 1" />
    <asp:ListItem Value="2" Text="User control 2" />
</asp:DropDownList>
<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>   

Default.aspx.cs Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        switch (ddl1.SelectedValue)
        {
            case "1":
                var ctl = LoadControl("Controls/UserControl.ascx");
                ph1.Controls.Add(ctl);
                break;
            case "2":
                ctl = LoadControl("Controls/UserControl2.ascx");
                ph1.Controls.Add(ctl);
                break;
        } 
    }
}

In my particular case, I found that the problem was the UserControl ID (or rather the lack of). 在我的特定情况下,我发现问题是UserControl ID(或者更确切地说是缺少)。

When the UserControl was first instantiated, my button ID was ctl00$ctl02$btnContinue, but after the postback it had changed to ctl00$ctl03$btnContinue, therefore the button event handler didn't fire. 首次实例化UserControl时,我的按钮ID为ctl00 $ ctl02 $ btnContinue,但在回发后它已更改为ctl00 $ ctl03 $ btnContinue,因此按钮事件处理程序未触发。

I instead added my UserControl with a fixed ID and the button now always loads with the ID ctl00$myUserControl$btnContinue. 我改为使用固定ID添加我的UserControl,现在按钮总是加载ID为ctl00 $ myUserControl $ btnContinue。

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

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