Dropbox的值改变后,按钮点击由于页面加载

本文关键字:于页面 加载 按钮 改变 Dropbox | 更新日期: 2023-09-27 18:17:25

我使用asp dropbox并从数据库中填充dropbox。但是dropbox的值在点击按钮时发生了变化,因为按钮调用的when事件首先是pageload代码调用,在pageload中我从数据库中填充了dropbox代码是

 protected void Page_Load(object sender, EventArgs e)
        {
        var q = from u in db.tbl_Subject select u;
        Paper_Subject.DataSource=q.ToList();
        Paper_Subject.DataTextField = "Subject_Name";
        Paper_Subject.DataValueField = "Subject_Id";
        Paper_Subject.DataBind();

        for (int i = 0; i <= 12;i++)
        {
            if(i<10)
                Paper_Duration_Hour.Items.Add("0"+i.ToString());
            else
                Paper_Duration_Hour.Items.Add(i.ToString());
        }
        for (int i = 0; i <= 60; i=i+5)
        {
            if(i<10)
                Paper_Duration_Minute.Items.Add("0"+i.ToString());
            else
                Paper_Duration_Minute.Items.Add(i.ToString());
        }
    }
protected void SaveData()
    {
        int PaperDurationHour;
        int PaperDurationMinute;
        int TeacherId;
        int SubjectId;
        tb.Paper_Name = PaperName.Text;
        if (int.TryParse(Paper_Duration_Hour.Text, out PaperDurationHour))
        {
            tb.Paper_Time_Hours = PaperDurationHour;
        }
        if (int.TryParse(Paper_Duration_Minute.Text, out PaperDurationMinute))
        {
            tb.Paper_Time_Minute = PaperDurationMinute;
        }
        string s1;
        s1 = Paper_Subject.SelectedItem.Text;
        var q = from u in db.tbl_Subject where u.Subject_Name.Equals(s1) select u;
        if (q.Count() > 0)
        {
            SubjectId = q.FirstOrDefault().Subject_Id;
            tb.Subject_Id = SubjectId;
        }
        if (int.TryParse(Session["TeacherID"].ToString(), out TeacherId))
        {
            tb.Teacher_Id = TeacherId;
        }
        db.tbl_Paper.Add(tb);
        db.SaveChanges();
    }
}

当按钮调用时我想获得dropbox的选定值但每次我都得到第一个值因为pagload

Dropbox的值改变后,按钮点击由于页面加载

我建议您只在页面首次加载时填充您的Dropbox,并且当一些数据发生变化时。要在页面首次加载时填充您的Dropbox,您可以使用属性IsPostBack,该属性仅在您第一次加载页面时为false,因此语句(!IsPostBack)将在您第一次加载页面时返回true。因此你可以这样做:

protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {
         //code here will be only executed the first time the page is loaded
         var q = from u in db.tbl_Subject select u;
         Paper_Subject.DataSource=q.ToList();
         Paper_Subject.DataTextField = "Subject_Name";
         Paper_Subject.DataValueField = "Subject_Id";
         Paper_Subject.DataBind();
         for (int i = 0; i <= 12;i++)
         {
             if(i<10)
                 Paper_Duration_Hour.Items.Add("0"+i.ToString());
             else
                 Paper_Duration_Hour.Items.Add(i.ToString());
         }
         for (int i = 0; i <= 60; i=i+5)
         {
             if(i<10)
                 Paper_Duration_Minute.Items.Add("0"+i.ToString());
             else
                 Paper_Duration_Minute.Items.Add(i.ToString());
         }
     }
    //code here will be executed every time page loaded
}

和代码将只在您第一次加载页面时执行。