下拉菜单重置自我页面加载

时间:2016-04-19 13:31:47

标签: c# asp.net

我在.net中遇到一个非常奇怪的问题,由于某种原因导致下拉控制失去保存的值,并且在加载时恢复到其原始值,现在我确实有我的if(!IsPostBack)但我认为我在这里有另一个问题。

Comments

现在唯一的区别是我在db中处理已保存的guid作为唯一标识,我认为它不是检索保存值的正确方法

protected void Page_Load(object sender, EventArgs e)
    {
        tblApertureNetShiftPattern _shifts = new tblApertureNetShiftPattern();
        string timeCode = "";

        string id = Request.QueryString["id"];

        Guid _guid = string.IsNullOrEmpty(id) ? new Guid() : new Guid(id);

        _shifts = _dal.GetShiftPatternById(_guid);

        if (Request.Browser.Browser == "Firefox")
            Form.Attributes.Add("autocomplete", "off");



            var dlManagersSource = _dal.GetManagers();
            rdManagers.DataSource = dlManagersSource;
            rdManagers.DataValueField = "LookupValue";
            rdManagers.DataTextField = "LookupDescription";
            rdManagers.DataBind();

            // Then add your first item
            var ddlDaysOfWeek = _dal.GetDaysOfWeek().OrderBy(o => o.Order);
            rdDayOfWeek.DataSource = ddlDaysOfWeek;
            rdDayOfWeek.DataValueField = "LookupValue";
            rdDayOfWeek.DataTextField = "LookupDescription";
            rdDayOfWeek.DataBind();

            var ddlShiftPatterns = _dal.GetShiftPatternTypes();
            rdAppointmentType.DataSource = ddlShiftPatterns;
            rdAppointmentType.DataValueField = "LookupValue";
            rdAppointmentType.DataTextField = "LookupDescription";
            rdAppointmentType.DataBind();
        if (!IsPostBack)
        {

            txtDescription.Text = _shifts.Description;
            rdStartShift.SelectedDate = _shifts.startdate;
            rdShiftEnd.SelectedDate = _shifts.endDate;
            rdDayOfWeek.SelectedValue = _shifts.dayOfWeek.ToString();
            rdAppointmentType.SelectedValue = _shifts.appointmentType.ToString();
            rdManagers.SelectedValue = _shifts.manager_Id.ToString();
        }

    }

这让我疯了,我想知道是否有人知道如何解决这个问题

按ID

获取班次模式
protected void rdSaveShfit_Click(object sender, EventArgs e)
    {
        try
        {
   tblApertureNetShiftPattern _shifts = new tblApertureNetShiftPattern();

            string id = Request.QueryString["id"];

            Guid _guid = string.IsNullOrEmpty(id) ? new Guid() : new Guid(id);
            _shifts = _dal.GetShiftPatternById(_guid);

            DateTime dt1 = Convert.ToDateTime(rdStartShift.SelectedDate);
            DateTime dt2 = Convert.ToDateTime(rdShiftEnd.SelectedDate);

            TimeSpan span = dt2.Subtract(dt1);
_shifts.appointmentDuration = Convert.ToInt32(span.TotalMilliseconds.ToString());
_shifts.Description = txtDescription.Text;
_shifts.startdate = rdStartShift.SelectedDate;
_shifts.endDate = rdShiftEnd.SelectedDate;
_shifts.appointmentType = new Guid(rdAppointmentType.SelectedValue.ToString());
_shifts.manager_Id = new Guid(rdManagers.SelectedValue.ToString());
_shifts.dayOfWeek = new Guid(rdDayOfWeek.SelectedValue.ToString());
_shifts.isDeleted = false;
 _shifts.manager_name = rdManagers.SelectedText;
      _dal.apertureNetEntities.SaveChanges();
        }
        catch (Exception ex)

        {
            logger.Error("Error in rdSaveShfit_Click function Edit.aspx module " + ex.ToString());
        }
        Response.Redirect("default.aspx");
    }   

修改1       下拉受影响的是约会类型,并且没有这个页面被称为edit.aspx它当我测试以确保值在刷新时保持正确但是如你所见,我有代码那里来计算。

2 个答案:

答案 0 :(得分:0)

您必须如下所示包围下拉列表数据绑定。如果不是每个回发数据都将重新绑定到控件,它会导致重置您的数据。

 if (!IsPostBack)
 {
            var dlManagersSource = _dal.GetManagers();
            rdManagers.DataSource = dlManagersSource;
            rdManagers.DataValueField = "LookupValue";
            rdManagers.DataTextField = "LookupDescription";
            rdManagers.DataBind();

            // Then add your first item
            var ddlDaysOfWeek = _dal.GetDaysOfWeek().OrderBy(o => o.Order);
            rdDayOfWeek.DataSource = ddlDaysOfWeek;
            rdDayOfWeek.DataValueField = "LookupValue";
            rdDayOfWeek.DataTextField = "LookupDescription";
            rdDayOfWeek.DataBind();

            var ddlShiftPatterns = _dal.GetShiftPatternTypes();
            rdAppointmentType.DataSource = ddlShiftPatterns;
            rdAppointmentType.DataValueField = "LookupValue";
            rdAppointmentType.DataTextField = "LookupDescription";
            rdAppointmentType.DataBind();
}

答案 1 :(得分:0)

Page_Load在每次加载页面时执行,并在您的点击事件(rdSaveShfit_Click)之前执行。

在我看来,rdAppointmentType控件正在重新绑定到每个页面加载(以及其他一些控件,但这是你要问的那个)。

这意味着您在点击事件之前即使有机会保存它也会消除所选值。

我认为你应该在Page_Load和rdSaveShfit_Click的断点处设置一个断点,然后亲眼看看。

您还可以在以下链接中查看页面生命周期: https://msdn.microsoft.com/en-us/library/ms178472.aspx?f=255&MSPPError=-2147217396