0

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

3
  • without postback how the server know about it? However you can do it in a manner that it seems without postback with the help of Ajax Commented Feb 20, 2012 at 15:47
  • I can only show you the asp.net/ajax. You're the one that has to walk through it. Commented Feb 20, 2012 at 15:53
  • delete the if(!IsPostBack) in the Page_Load Commented Feb 20, 2012 at 15:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.