Page11中的ASP.NET代码未在Post Post上调用

时间:2016-03-20 13:41:59

标签: c# asp.net

我有一个简单的Web应用程序,我使用Entity Framework将一些数据添加到数据库,然后在下拉列表中填充它,我可以从中选择要删除的元组。我面临的问题是,当我添加新记录时,下拉列表不会被填充。我必须重新加载页面才能看到。以下是Page_Load方法背后的代码

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        int token = Int32.Parse(Request.Cookies["EmcLabs"]["authToken"]);
        EMCLabCustomerDataEntities context = new EMCLabCustomerDataEntities();
        User u = new User();
        u = context.Users.SingleOrDefault(x => x.Token == token);
        lblJumboUsername.Text = u.Name;
        lblJumboPost.Text = u.Post;
        context.Dispose();
        InitializePage();                                   
    }
    catch (Exception)
    {
        Response.Cookies["EmcLabs"].Expires = DateTime.Now;
        Response.Redirect("Login.aspx");
    }

}
public void InitializePage()
{
    EMCLabCustomerDataEntities context = new EMCLabCustomerDataEntities();
    User usr = new User();
    usr = context.Users.SingleOrDefault(x => x.Name == lblJumboUsername.Text);
    if (usr.IsAdmin != 1)
    {
        divAdmin.Visible = false;
    }     
    //Users
    try
    {
        ddlUserList.DataSource = (from x in context.Users
                                  where x.Name != lblJumboUsername.Text
                                  select new
                                  {
                                      x.Name,
                                      x.Id
                                  }).ToList();
        ddlUserList.DataTextField = "Name";
        ddlUserList.DataValueField = "Id";
        ddlUserList.DataBind();
        ddlUserList.Items.Insert(0, new ListItem("Select User", "default"));
    }
    catch (Exception ex)
    {
        divMessages.Attributes.Add("class", "alert alert-danger");
        lblMessage.Text += ex.Message;
    }
}

以下是我必须添加和删除记录的代码。

rotected void btnAddUser_Click(object sender, EventArgs e)
{
    try
    {
        User u = new User();
        u.Email = txtEmail.Text.Trim();
        u.Name = txtName.Text.Trim();
        u.Password = txtRandomPass.Text;
        u.Post = txtPost.Text.Trim();
        u.Token = 0;
        if (chkAdmin.Checked == true)
        {
            u.IsAdmin = 1;
        }
        else
        {
            u.IsAdmin = 0;
        }
        EMCLabCustomerDataEntities context = new EMCLabCustomerDataEntities();
        if (true)
        {

        }
        context.Users.Add(u);
        context.SaveChanges();
        divMessages.Attributes.Add("class", "alert alert-success");
        lblMessage.Text = "New user added successfully.";
    }
    catch (Exception ex)
    {
        divMessages.Attributes.Add("class", "alert alert-danger");
        lblMessage.Text = ex.Message;
    }

}

protected void btnDeluser_Click(object sender, EventArgs e)
{
    if (chkConfirmDelete.Checked == true)
    {
        try
        {
            if (ddlUserList.SelectedItem.Value != "default")
            {
                EMCLabCustomerDataEntities context = new EMCLabCustomerDataEntities();
                User u = context.Users.Single(x => x.Name == ddlUserList.SelectedItem.Text);
                context.Users.Remove(u);
                context.SaveChanges();
                divMessages.Attributes.Add("class", "alert alert-success");
                lblMessage.Text = "User deleted successfully.";
            }
            else
            {
                divMessages.Attributes.Add("class", "alert alert-warning");
                lblMessage.Text = "Select an user to remove first.";
            }
        }
        catch (Exception ex)
        {
            divMessages.Attributes.Add("class", "alert alert-danger");
            lblMessage.Text = ex.Message;
        }
    }
    else
    {
        divMessages.Attributes.Add("class", "alert alert-warning");
        lblMessage.Text = "Confirm before deleting.";
    }
}

0 个答案:

没有答案