Question

I am writing a web app in asp.net. I have an aspx page that call a class (Test) that Generates a button and return the button. The class constructor get the function that the button click event should activate (userClickOnButton) and insert to the button.click += EventHandler("the name of the function (userClickOnButton)");

the problem is that in the aspx behind code i use IsPostBack (cant take it off , need this Condition) and when i click on the button the progrem does not go to the event i created for the button, but when i take off the IsPostBack Condition the program does go to the event i created (the function userClickOnButton).

my code is: aspx code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Test test = new Test(userClickOnButton);
        Button button = test.AddButton();
        cell.Controls.Add(button);
    }
}

private void userClickOnButton(object sender, EventArgs e)
{
    this.ModalPopupExtenderSelectFilds.Show();
}

my class

public class Test
{

Action<object, EventArgs> m_ButtonClickActivateFunction;

public Test(Action<object, EventArgs> func) 
{
    m_ButtonClickActivateFunction = func;
}

public Button AddButton()
{
    Button button = new Button();
    button.Text = "select";
    button.ID = "buttonID";
    button.Click += new EventHandler(m_ButtonClickActivateFunction);

    return button;
}

need help to activate the event without taking out the IsPostBack Condition

thanks

Was it helpful?

Solution

I think asp.net doesn't handle clicks on buttons that no longer exist. Since on postback you don't recreate the button and the button therefore does no longer exist in the controls collection of the page, the event is not handled.

Handling of the event happens after Page_Load, so Page_Load should have recreated the button.

Have a look here as well: Asp.Net Page Life Cycle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top