简体   繁体   中英

Why does my dynamically created user control doesn't fire button click event

i have a problem with user control. i create it dynamically on my aspx page after clicking on a button:

protected void btnAddRules_Click(object sender, EventArgs e)
    {          
        RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");         
        MyPanel.Controls.Add(Control);
    }

when i click on a button of my user control, the button event wont fire and the user control will disappear. here is the button event:

protected void btnAdd_Click1(object sender, EventArgs e)
    {
        WowzaRule rule = GetRuleFromGUI();
        RuleList.Add(rule);
        //Session["RuleList"] = RuleList;
        //List<WowzaRule> test = new List<WowzaRule>();
        SaveToXMLFiles(txtdbnum.Text, RuleList);
    }

i understand that after pressing the button on mypage the usercontrol is released and if its not created on pag_init or page Load it wont stay, but i need to create it on my button click event and find a way for it not to disapper.

thanks in advance, Daniel

您可能需要添加一个事件处理程序,该事件处理程序可以触发click事件并调用您的委托

Control.Click += btnAdd_Click1;

Dynamically created controls, once added, have to be on a page on every page load in order to work correctly. What happens in your case:

  1. RuleProperty is added after the button click
  2. Page loads with this control
  3. User clicks on the button within RuleProperty
  4. Control is not added to the control tree during the page load (corresponding code is only in the button click handler, and that button was not clicked)
  5. ASP.NET does not know which control triggered the event, so the event is not processed

To go around this issue you need to add you control on every page loading, for example using some flag stored in ViewState :

protected void Page_Load(object sender, EventArgs e)
{
    if (ViewState["AddRuleProperty"] != null && (bool)ViewState["AddRuleProperty"])
    {
        AddRulePropertyControl();
    }
}

protected void btnAddRules_Click(object sender, EventArgs e)
{
    AddRulePropertyControl();          
    ViewState["AddRuleProperty"] = true;
}

private void AddRulePropertyControl()
{
    RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");         
    MyPanel.Controls.Add(Control);
}

Update.

If you want to remove the control from the page later on in the control's click handler, you need to remove corresponding ViewState key. This is not possible from the control directly, since property Page.ViewState is protected, and also this would have created an unwanted dependency.

What seems as the right way to do this is to subscribe to the very same event from the Page (you might need to make this event visible from the controller) and reset the key in there. Like this:

private void AddRulePropertyControl()
{
    RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");         
    Control.ButtonClick += RuleProperty_ButtonClick;
    MyPanel.Controls.Add(Control);
}

private void RuleProperty_ButtonClick()
{
    ViewState["AddRuleProperty"] = false;
}

Please note that event name here is not real, this is just a sketch of what can be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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