使用ViewState时出现问题 [英] Problem Using ViewState

查看:94
本文介绍了使用ViewState时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数setlocation();这将我的下拉列表绑定到页面加载上.
如果用户选择其他位置,则在selectedindex上使用以下代码:

i have a function setlocation(); which bind my dropdownlist on pageload.
and if user select another location then on selectedindex i use following code:

protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check = "";
                        check = ddlState.SelectedItem.Text;
            ViewState["statename"] = check;
        }


页面加载我使用:


ON page load i use:

if(ViewState["statename"]!=null)
               {
                   ddlState.SelectedItem.Text = ViewState["statename"].ToString();
               }
                else
                   {
                Setlocation();
                   }


在aspx中:


in aspx:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                       <ContentTemplate><asp:DropDownList ID="ddlState" AutoPostBack="true"   runat="server" style="height:28px; width:176px"

                               onselectedindexchanged="ddlState_SelectedIndexChanged">
                       </asp:DropDownList>
                     </ContentTemplate>
                           <Triggers>
                               <asp:AsyncPostBackTrigger ControlID="imgBtnCancel" EventName="Click" />
                           </Triggers>
                     </asp:UpdatePanel>


问题是页面加载其他条件下的编译器总是掉下去.为什么我错了


The problem is that onpage load the complier fall alwayz on else condition.why where i am wrong

推荐答案

好吧,两件事:
1.听起来好像您忘了使用页面"IsPostback"属性.确保您没有在每次回发中都重新绑定下拉列表-如果这样,它将重置选择.

2.一旦IsPostback到位,其他部分将从第3次开始生效.第一次,新鲜的页面加载,viewstate中没有任何内容.第二次,由于selectedindex更改事件尚未发生,再次没有任何结果.一旦命中,它将立即设置视图状态.此后,如果您有任何回发,它将进入该部分.

现在,不确定您要做什么,但它看起来不像预期的行为或执行方式.
Well, 2 things:
1. Sounds like you forgot to use the page ''IsPostback'' property. Make sure that you are not rebinding the dropdown on every postback - if so, it will reset the selection.

2. Once IsPostback is in place, the else part would hit from 3rd time onwards. First time, fresh pageload, nothing in viewstate. 2nd time, again nothing as selectedindex change event is not yet hit. Once hit, it will set the viewstate now.After this, if you have any postback it will go to that part.

Now, not sure what you are trying to do but it does not look like the intended bevaiour or the way to do it.


这篇关于使用ViewState时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆