当前位置: 首页 > 编程语言 > C#

ASP.NETAutoPostBack然后后退按钮很奇怪Share

时间:2023-04-10 20:58:44 C#

ASP.NETAutoPostBack然后后退按钮很奇怪下拉菜单有一个自动回发功能,它调用DropDownList1_SelectedIndexChanged并将页面重定向到某个地方(对于本例www.google.com),按钮有一个onclick转到Button1_Click1,页面重定向到www.yahoo。com。问题:如果我单击该按钮,我会转到Yahoo,这正是您所期望的。如果我单击浏览器中的后退按钮并选择下拉菜单,我会转到Google,这也是正确的,但如果我单击后退按钮然后单击该按钮,我将被重定向到Google。出色地?为什么不去雅虎?这是我的代码:TestingAuto-PostbackCodeBehind:usingSystem;公共部分类测试:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidDropDownList1_SelectedIndexChanged(objectsender,EventArgse){Response.Redirect("http://www.google.com");}protectedvoidButton1_Click1(objectsender,EventArgse){Response.Redirect("http://www.yahoo.com");如果有人能帮助我,我将不胜感激。好吧,经过一些挖掘,我发现了以下内容:当我们单击按钮时,页面生命周期直到引发Button1_Click1事件发生如下:BeginPreLoadEndPreLoadBeginLoadEndLoadBeginProcessPostDataSecondTryEndProcessPostDataSecondTryBeginRaiseChangedEventsEndRaiseChangedEventsBeginRaisePostBackEventRaisedButton1_Click1//Buttoneventhere现在,当我们更改DropDownList,直达DropDownList1_SelectedIndexChanged事件被下发事件的封面:BeginPreInitEndPreInitBeginInitBeginInitCompleteEndInitCompleteBeginLoadStateEndLoadStateBeginProcessPostDataEndProcessPostDataBeginPreLoadEndPreLoadBeginLoadEndLoadBeginProcessPostDataSecondTryEndProcessPostDataSecondTryBeginRaiseChangedEventsRaisedDropDownList1_SelectedIndexChanged//DropDownList事件在这里通过分析页面生命周期,我们看到DropDownList1_SelectedIndexChanged事件在Page’;在RovedEvents过程中引发,此方法早于引发Button1_Click1事件的Page'PostBackEvent'过程现在,当您更改DropDownListSelectedIndex时,您将被重定向到Google。当您点击后退按钮时,浏览器将检索该页面的最后状态,这意味着DropDownList将保留您之前更改的值。如果在该阶段单击按钮,则根据请求值发送DropDownList和Button。当首先引发DropDownList事件时,页面再次重定向到Google。更新:一项工作可以在后面的代码上实现以下内容:publicpartialclasstest:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedoverridevoidOnInit(EventArgse){base.OnInit(e);if(IsPostBack){//如果表单正在发布按钮,则表示您单击了它boolisButtonPostBackEvent=Request.Form.AllKeys.Contains(Button1.UniqueID);//获取DropDownList字符串的发布值selectedValue=Request.Form[DropDownList1.UniqueID];//检索DropDownListpostedValue的索引intvalueIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(selectedValue));//验证下拉列表的发布值是否与服务器不同(将引发SelectedIndexChangedEvent事件)boolwillRaiseSelectedIndexChangedEvent=DropDownList1.SelectedIndex!=valueIndex;//验证是否会触发两个事件,因此应用按钮//行为,否则让asp.net执行其//magic并自动引发事件盟友如果(isButtonPostBackEvent&&willRaiseSelectedIndexChangedEvent){RedirectToYahoo();}}}protectedvoidDropDownList1_SelectedIndexChanged(objectsender,EventArgse){RedirectToGoogle();}protectedvoidButton1_Click1(objectsender,EventArgse){RedirectToYahoo();}privatevoidRedirectToGoogle(){Response.Redirect("http://www.google.com");}privatevoidRedirectToYahoo(){Response.Redirect("http://www.yahoo.com");在OnInit事件中,代码标识将由asp.net引发的事件。当两个事件都存在时,我们应用按钮单击行为,因为在这种情况下它具有优先级(单击它)。如果不介意的话,还可以再简单点:以上就是C#学习教程:ASP.NETAutoPostBack然后返回BackButton很奇怪的分享所有内容,如果对大家有用需要了解一下更多C#学习教程,希望大家多多关注——protectedoverridevoidOnInit(EventArgse){base.OnInit(e);如果(IsPostBack){boolisButtonPostBackEvent=Request.Form.AllKeys.Contains(Button1.UniqueID);如果(isButtonPostBackEvent){RedirectToYahoo();}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: