Reputation: 1773
I have two image buttons with Click events assigned to them. There is code in each event. When they are clicked it runs the page load function before it runs the code in the click events. How can i get it to run the code in the click events first?
Happy Holidays!
Image buttons.
protected void Page_Load(object sender, EventArgs e)
{
//YesID = "1";
if (!IsPostBack)
{
Session["FirstyesID"] = 0;
Session["FirstnoID"] = 0;
Session["SecondnoId"] = 0;
Session["SecondyesId"] = 0;
Session["yesID"] = 0;
Session["noId"] = 0;
}
else
{
//Run this code
}
}
protected void FirstPicLink_Click(object sender, ImageClickEventArgs e)
{
Session["yesID"] = Session["FirstyesID"];
Session["noId"] = Session["FirstnoID"];
//FirstPicLink.PostBackUrl = "default.aspx";
//FirstPicLink.PostBackUrl = "GadgetFS.aspx?yesId=" + firstYesPicId + "&noId=" + firstNoPicId;
}
protected void SecondPicLink_Click(object sender, ImageClickEventArgs e)
{
Session["yesID"] = Session["SecondyesId"];
Session["noId"] = Session["SecondnoId"];
//SecondPicLink.PostBackUrl = "default.aspx";
//SecondPicLink.PostBackUrl = "GadgetFS.aspx?yesId=" + secondYesPicId + "&noId=" + secondNoPicId;
}
Upvotes: 0
Views: 492
Reputation: 12366
What you are asking is illegal. There's an order in which things are executed in ASP.NET
As you can see "Postback event handling" happens after "Load" and trying to trick this, will cause you headaches afterwards. It's a very bad practice and this is coming from someone who's been down that road. You don't want to mess with the page lifecycle.
Looking at your code, I could suggest you put whatever should go in the // Run this code
lines into a method, and call that method from the Click handlers, but not from the else
block.
Anyhow, you should always have the page lifecycle in mind, and revise your code logic according to that.
Upvotes: 2