Uroš Hrastar
Uroš Hrastar

Reputation: 69

Calling Page_Load on button click

Is it a good practice to call Page_Load function in another function like this. It's working but I don't know if i should do this

public bool myButtonClick(object sender, EventArgs e){
Page_Load(this, null);
}

Upvotes: 0

Views: 7512

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157116

You can do this, but you shouldn't. And it is very easy to prevent this:

In your Page_Load, call a method that does the actual work in the method now. Then call that method from myButtonClick:

public void Page_Load(object sender, EventArgs e)
{
    this.SomeMethod();
}

public void myButtonClick(object sender, EventArgs e)
{
    this.SomeMethod();
}

private void SomeMethod()
{
    // the actual code now in Page_Load
}

You see, nice and clean and reusable too.

Upvotes: 2

Related Questions