Asp.Net下拉列表在页面空闲后回发后显示空白

时间:2013-11-08 12:09:47

标签: c# asp.net updatepanel

我的页面上有2个下拉列表。一个用于活动,一个用于子活动。当活动下拉列表更改时,我将检索子活动下拉列表的值。我使用updatepanel作为第二个下拉列表,触发器设置为第一个下拉列表。这是神奇的工作,直到我离开页面空闲最多1-2分钟。当我更改活动下拉列表(触发器)的值时,它会导致完整的回发,并且第二个下拉列表的项目不会被填充。

以下是我的代码。

.aspx的

   <div class="form-line">
    <label>Activity</label>

    <asp:DropDownList ID="ddlActivity" required="required" CssClass="chosen-select" runat="server" Width="200px" AppendDataBoundItems="true" OnSelectedIndexChanged="SelectedActivity_Changed" AutoPostBack="true" CausesValidation="false">
    </asp:DropDownList>

</div>
<div class="form-line">
    <label>Sub Activity</label>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddlActivity" EventName="SelectedIndexChanged" />
        </Triggers>
        <ContentTemplate>
            <asp:DropDownList ID="ddlSubActivity" runat="server" Width="200px" required="required" CssClass="chosen-select" AppendDataBoundItems="true" CausesValidation="false">
            </asp:DropDownList>
        </ContentTemplate>
    </asp:UpdatePanel>

</div>

背后的代码

 [ToolboxItemAttribute(false)]
    public partial class MyEstimatesAdd : WebPart
    {
        static string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        ChronoLogDataContext dc = new ChronoLogDataContext(connStr);
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public MyEstimatesAdd()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Page.Request.QueryString["Issue_ID"] != "" && Page.Request.QueryString["Issue_ID"] != null)
                {
                    Int64 issueid = Int64.Parse(Page.Request.QueryString["Issue_ID"]);
                    string sCurrentUserEmail;
                    SPUser cuser = SPControl.GetContextWeb(Context).CurrentUser;

                    sCurrentUserEmail = cuser.Email;
                    dc = new ChronoLogDataContext(connStr);

                    var resource = (from r in dc.RESOURCEs
                                    where r.Email == sCurrentUserEmail
                                    select r).FirstOrDefault();

                    ddlActivity.DataSource = (from a in dc.ACTIVITY_CATEGORies
                                              select a).ToList();

                    ddlActivity.DataValueField = "Activity_Category_ID";
                    ddlActivity.DataTextField = "Description";
                    ddlActivity.DataBind();
                    ddlActivity.Items.Insert(0, new ListItem("", ""));
                }


            }
        }

        //RUN AGAIN PRIMARY SET
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int hours;
            if (!int.TryParse(txtHours.Text, out hours))
            {
                throw new SPException("Hours must be a numeric value");
            }
            string sCurrentUserEmail;
            SPUser cuser = SPControl.GetContextWeb(Context).CurrentUser;
            sCurrentUserEmail = cuser.Email;
            dc = new ChronoLogDataContext(connStr);
            var resource = (from r in dc.RESOURCEs
                            where r.Email == sCurrentUserEmail
                            select r).FirstOrDefault();
            Int64 issueid = Int64.Parse(Page.Request.QueryString["Issue_ID"]);
            decimal time = decimal.Parse(hours + "." + ddlMinutes.SelectedValue.ToString(), CultureInfo.InvariantCulture);
            ESTIMATE estimate = new ESTIMATE { Activity_ID = int.Parse(ddlActivity.SelectedItem.Value), Date_Captured = DateTime.Now, Estimated_Time = time, Features = txtFeatures.Text, Issue_ID = issueid, Resource_ID = resource.Resource_ID, Sub_Activity_ID = int.Parse(ddlSubActivity.SelectedItem.Value) };
            dc.ESTIMATEs.InsertOnSubmit(estimate);
            dc.SubmitChanges();
            Close_Save();
        }
        protected void Close_Save()
        {
            HttpContext context = HttpContext.Current;
            if (HttpContext.Current.Request.QueryString["IsDlg"] != null)
            {
                context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup()</script>");
                context.Response.Flush();
                context.Response.End();
            }


        }

        protected void SelectedActivity_Changed(object sender, EventArgs e)
        {
            int activityid = int.Parse(ddlActivity.SelectedItem.Value);
             dc = new ChronoLogDataContext(connStr);
             var subactivities = (from s in dc.ACTIVITY_SUBCATEGORies
                                  where s.Activity_Category_ID == activityid
                                  select s).ToList();
             ddlSubActivity.Items.Clear();
             ddlSubActivity.DataSource = subactivities;
             ddlSubActivity.DataValueField = "Activity_SubCategory_ID";
             ddlSubActivity.DataTextField = "Description";
             ddlSubActivity.DataBind();
             ddlSubActivity.Items.Insert(0, new ListItem("", ""));
             UpdatePanel1.Update();

        }
    }

1 个答案:

答案 0 :(得分:1)

将两个下拉列表放在同一个UpdatePanel中,或者放在他们自己的UpdatePanel中,如下所示:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlActivity" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <div class="form-line">
            <label>Activity</label>
            <asp:DropDownList ID="ddlActivity" required="required" CssClass="chosen-select" runat="server" Width="200px" AppendDataBoundItems="true" OnSelectedIndexChanged="SelectedActivity_Changed" AutoPostBack="true" CausesValidation="false">
            </asp:DropDownList>
        </div>
        <div class="form-line">
            <label>Sub Activity</label> 
            <asp:DropDownList ID="ddlSubActivity" runat="server" Width="200px" required="required" CssClass="chosen-select" AppendDataBoundItems="true" CausesValidation="false">
            </asp:DropDownList>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>