C# 如果语句为true,则重定向
您好,我现在正在学习c#,希望如果一个语句是真的,那么就重定向到另一个迄今为止没有运气的页面 下面是我的尝试,代码所做的是检查文本框,看看它们是否有值,如果有值,那么它将以新用户的身份将该记录保存在数据库中。代码工作正常,只是最后的重定向不起作用 任何帮助都会很好C# 如果语句为true,则重定向,c#,entity-framework,validation,if-statement,ascx,C#,Entity Framework,Validation,If Statement,Ascx,您好,我现在正在学习c#,希望如果一个语句是真的,那么就重定向到另一个迄今为止没有运气的页面 下面是我的尝试,代码所做的是检查文本框,看看它们是否有值,如果有值,那么它将以新用户的身份将该记录保存在数据库中。代码工作正常,只是最后的重定向不起作用 任何帮助都会很好 protected void Button2_Click(object sender, EventArgs e) { if (!String.IsNullOrWhiteSpace(txtTitle.Tex
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/");
}
}
}
}您需要重定向到另一个ASPX页面,而不是目录 差不多
Response.Redirect("User/List/UserList.aspx");
请注意,只有当你的应用程序中有url重写/路由时,上面的url格式才会起作用你得到了什么错误?没有错误,只是没有重定向谢谢,因为我得到了预期的类、委托、枚举、接口或结构错误,所以我应该把这段代码放在哪里?你进入了一些不匹配的大括号{}在你的代码里有一些关于类的空白
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.
}
}