如果Statement为true,则重定向

时间:2013-09-13 08:56:06

标签: c# entity-framework validation if-statement ascx

你好我现在正在学习c#并希望如果一个陈述是真的那么到目前为止重定向到另一个AS页没有运气。

我的尝试如下,代码所做的是检查文本框以查看它们是否具有值,如果它们这样做,那么它将以新用户的身份将该记录保存在数据库中。代码仅在重定向时运行结束不起作用。

任何帮助都会很棒

   protected void Button2_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrWhiteSpace(txtTitle.Text))
        {
            user.Title = txtTitle.Text;
        }
        if (!String.IsNullOrWhiteSpace(txtFirstName.Text))
        {
            user.Forename = txtFirstName.Text;
        }
        if (!String.IsNullOrWhiteSpace(txtSurname.Text))
        {
            user.Surname = txtSurname.Text;
        }
        if (!String.IsNullOrWhiteSpace(txtUsername.Text))
        {
            user.Username = txtUsername.Text;
        }
        // call save function at end of statements 
        if (!String.IsNullOrWhiteSpace(txtAddress.Text))
        {
            user.Address1 = txtAddress.Text;
        }
        if (!String.IsNullOrWhiteSpace(txtAddress2.Text))
        {
            user.Address2 = txtAddress.Text;
        }
        if (!String.IsNullOrWhiteSpace(txtPostcode.Text))
        {
            user.PostCode = txtPostcode.Text;
        }
        if (!String.IsNullOrWhiteSpace(txtCode.Text))
        {
            user.CountryCode = txtCode.Text;
        }           
        if (!String.IsNullOrWhiteSpace(txtEmail.Text))
        {
            user.Email = txtEmail.Text;
        }
        //if (!string.IsNullOrWhiteSpace(txtDate.Text))
        //{
        //    DateTime _entrydate;
        //    if (DateTime.TryParse(txtDate.Text, out _entrydate))
        //    {
        //        user.EntryDate = _entrydate;
        //    }
        //}
        user.CompanyID = AppSession.Company.ID;
        user.Status = 1;
        user.PasswordHash = "test";
        user.EntryDate = DateTime.Now;
        user.UpdateDate = DateTime.Now;
        user.Deleted = false;
        bool result = userDao.SaveNewUser(user);
       if (result == true)
       {
          Response.Redirect("User/List/");
       }
    }
}

}

2 个答案:

答案 0 :(得分:0)

您需要重定向到另一个ASPX页面,而不是目录。

这样的东西
Response.Redirect("User/List/UserList.aspx");

答案 1 :(得分:0)

Private static string CheckValues(TextBox t)
{
  if(!string.IsnullOrEmpty(t.Text.Trim())
  {
      return t.Text;
  }
}

  protected void Button2_Click(object sender, EventArgs e)
    {
            user.Title =CheckValues(txtTitle.Text);

            user.Forename = CheckValues(txtFirstName.Text);

            user.Surname = CheckValues(txtSurname.Text);

            user.Username = CheckValues(txtUsername.Text);

            user.Address1 = CheckValues(txtAddress.Text);

            user.Address2 = CheckValues(txtAddress.Text);

            user.PostCode = CheckValues(txtPostcode.Text);

            user.CountryCode = CheckValues(txtCode.Text);

            user.Email = CheckValues(txtEmail.Text);
       if(CheckValues(txtDate.Text))
        {
           DateTime _entrydate;
           if (DateTime.TryParse(txtDate.Text, out _entrydate))
            {
                user.EntryDate = _entrydate;
            }
        }
       bool result = userDao.SaveNewUser(user);
       if (result)
       {
          Response.Redirect("~/User/List/somepage"); //~ for root directory , if there is any page use that or use the exact url here.
       }
}

请注意,只有在您的应用中有网址重写/路由时,上述网址格式才有效