图片按钮和回发问题
我有两个分配有 Click 事件的图像按钮。每个事件都有代码。单击它们时,它会在运行单击事件中的代码之前运行页面加载函数。我怎样才能让它首先运行点击事件中的代码?
节日快乐!
图像按钮。
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你问的问题是非法的。 ASP.NET 中的执行顺序是有一定顺序的
正如你所看到的,“回发事件处理”发生在“加载”之后,并且试图欺骗它,之后会让你头疼。这是一种非常糟糕的做法,而且这是来自经历过这条路的人。您不想扰乱页面生命周期。
查看您的代码,我建议您将
// Run this code
行中应包含的内容放入方法中,并从 Click 处理程序调用该方法,而不是从else< /代码> 块。
无论如何,您应该始终牢记页面生命周期,并据此修改代码逻辑。
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 theelse
block.Anyhow, you should always have the page lifecycle in mind, and revise your code logic according to that.