【发布时间】:2015-11-01 22:10:39
【问题描述】:
我对 ASP.NET 比较陌生,但在使用简单的 Web 表单时遇到了问题。我有一个页面需要根据存储过程的结果重定向。我收集用户 ID(域名\名称),与 MS SQL 表进行比较以获取要显示哪个报告页面的角色(这只会带回单个角色,例如销售),然后我想重定向到不同的 aspx基于结果的页面。我有一个通用报告链接页面,其中包含管理员角色的所有报告。然后我有销售、营销、客户服务页面,用于报告某些用户只应该看到的报告。我正在恢复角色,因为它正在填充文本框和标签,但我无法让它重定向到备用页面。这是我的代码的副本。目前,我已经注释掉了“if else”重定向,因为它们不起作用。任何帮助将不胜感激。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Security.Principal;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
namespace MonogramFoods
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//var username = User.Identity.Name;
var username = "MSHOLDINGS\\efarney";
SqlConnection MyConnection = new SqlConnection("server=mmsmv-sql1\\sql2008;database=Express;Trusted_Connection=True;");
SqlDataAdapter MyDataAdapter = new SqlDataAdapter("SP_WEB_Check_User", MyConnection);
MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar, 40));
MyDataAdapter.SelectCommand.Parameters["@username"].Value = (username);
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@userrole", SqlDbType.VarChar, 40));
MyDataAdapter.SelectCommand.Parameters["@userrole"].Direction = ParameterDirection.Output;
DataSet DS = new DataSet();
MyConnection.Open();
MyDataAdapter.Fill(DS, "UsersRole");
Session.Add("Role", DS);
TextBox1.Text = MyDataAdapter.SelectCommand.Parameters[1].Value.ToString();
Label1.Text = MyDataAdapter.SelectCommand.Parameters[1].Value.ToString();
//if (TextBox1.Text == ("CustService"))
//{
// Response.Redirect("ReportsMain_CS.aspx");
//}
//else if (TextBox1.Text == ("Marketing"))
//{
// Response.Redirect("ReportsMain_MK.aspx");
//}
//else if (TextBox1.Text == ("Scorecard"))
//{
// Response.Redirect("ReportsMain_SC.aspx");
//}
//else if (TextBox1.Text == ("Sales"))
//{
// Response.Redirect("ReportsMain_SA.aspx");
//}
//else if (TextBox1.Text == ("Admin"))
//{
// Response.Redirect("ReportsMain.aspx");
//}
}
}
}
}
【问题讨论】:
-
在什么情况下不起作用?当您在调试器中单步执行时,
TextBox1.Text是否具有您期望的值?附带说明一下,如果您在不显示任何用户输入的情况下立即重定向,则将值存储在变量中比存储在页面控件中更有意义。 -
是的。在代码中,我使用我的登录名作为硬编码参数,它正在拉回正确的数据。销售额显示在 Text1.Text 和 Label1.Text 中。之后我想做的就是编码,如果结果是销售,重定向到 ReportsMain_SA.aspx。从用户的角度来看,当他们点击报告链接 (ReportsMain.aspx),并且他们的角色是销售时,他们应该立即转到 ReportsMain_SA.aspx。这只是我试图为我的经理提供的一个短期解决方案,因为他们是最后一刻才决定限制用户查看报告的。
-
我已经为此工作了几天,我的头撞到了墙上,真的很沮丧,因为我知道这可能是一个简单的问题。
-
这很好,但是...您实际上还没有解释它是如何不起作用的。
Response.Redirect()确实应该重定向用户。它是如何失败的?当您在调试器中逐步执行此操作时,具体在哪里/如何失败? -
当我添加此代码时,它不会重定向页面。它只是打开 ReportsMain.aspx 页面。