【发布时间】:2012-01-12 03:26:00
【问题描述】:
我是一名新的 ASP.NET 开发人员,我正在使用这种编程语言为我开发第一个 Web 应用程序。我正在尝试通过开发以下场景来使用向导控件来管理用户: Wizard Step1: 包含一个文本框,管理员可以在其中放置用户的用户名 当他单击下一个按钮时,将根据数据库中的用户表检查用户名;如果他存在于数据库中,他的信息将显示在 Wizard Step2 中,他的信息将是只读的。如果他不存在,则会通过消息通知管理员。
向导步骤 2: 包含显示用户信息的中继器或占位符。
向导步骤3:另外,如果用户存在,此步骤将显示该用户在系统中的当前角色,并显示一个用于编辑他角色的按钮
我的 ASP.NET 代码:
<asp:Wizard ID="Wizard1" runat="server" DisplaySideBar="false" Width="80%" >
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server" title="Employee Username/Network ID">
<table border="0">
<tr>
<td class="InputLabel">Username:</td>
<td class="InputControl">
<asp:TextBox ID="TextBox1" runat="server" />
</td>
</tr>
</table>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" title="Manage User">
<div class="content">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
</div>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep3" runat="server" Title="Edit User Role">
<label for="role">Current Role: </label>
<asp:Label ID="Label1" runat="server" BackColor="#FFFF99" Font-Bold="True" ForeColor="#000099" />
<asp:RadioButtonList id="radio1" runat="server" TextAlign="left">
<asp:ListItem id="option1" runat="server" value="Admin" />
<asp:ListItem id="option2" runat="server" value="Contribute" />
<asp:ListItem id="option3" runat="server" value="User" />
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Clicked" />
</asp:WizardStep>
</WizardSteps>
<HeaderTemplate>
<ul id="wizHeader">
<asp:Repeater ID="SideBarList" runat="server">
<ItemTemplate>
<li><a class="<%# GetClassForWizardStep(Container.DataItem) %>" title="<%#Eval("Name")%>">
<%# Eval("Name")%></a> </li>
</ItemTemplate>
</asp:Repeater>
</ul>
</HeaderTemplate>
</asp:Wizard>
而背后的代码是
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserManagement : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string username = TextBox1.Text;
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
string cmdText = "SELECT * FROM employee WHERE Username = @Username";
//For checking the user
if (username != null)
{
if (CheckUsername(username) == true)
{
try
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(cmdText, conn);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader["Name"].ToString());
Console.WriteLine(myReader["JobTitle"].ToString());
Repeater1.DataSource = myReader;
Repeater1.DataBind();
myReader.Close();
conn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
//For sending object to the Wizard1.PreRender
Wizard1.PreRender += new EventHandler(Wizard1_PreRender);
}
//Method for checking the existence of the username in the database (retrun true or false)
private bool CheckUsername(string username)
{
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open(); // Open DB connection.
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
int count = (int)cmd.ExecuteScalar();
// True (> 0) when the username exists, false (= 0) when the username does not exist.
return (count > 0);
}
}
}
protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
if (Wizard1.ActiveStepIndex == 1)
{
string username = TextBox1.Text;
}
}
//Method for replacing the default sidebar of the Wizard Control with a custom sidebar (represented in a repeater)
protected void Wizard1_PreRender(object sender, EventArgs e)
{
Repeater SideBarList = Wizard1.FindControl("HeaderContainer").FindControl("SideBarList") as Repeater;
SideBarList.DataSource = Wizard1.WizardSteps;
SideBarList.DataBind();
}
protected string GetClassForWizardStep(object wizardStep)
{
WizardStep step = wizardStep as WizardStep;
if (step == null)
{
return "";
}
int stepIndex = Wizard1.WizardSteps.IndexOf(step);
if (stepIndex < Wizard1.ActiveStepIndex)
{
return "prevStep";
}
else if (stepIndex > Wizard1.ActiveStepIndex)
{
return "nextStep";
}
else
{
return "currentStep";
}
}
protected void Button1_Clicked(Object sender, EventArgs e)
{
// When the button is clicked,
// show the new role of the user
//Label1.Text = "...button clicked...";
}
}
//Session["Username"] = Username.Text;
//String strUserName = Request.QueryString["Username"];
//string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
//string cmdText = "SELECT * FROM employee WHERE Username = @Username";
////For checking the user
//if (Request.QueryString["Username"] != null)
//{
// //String strUserName = Request.QueryString["Username"];
// ////Check userName Here
// //String strReturnStatus = "false";
// if (CheckUsername(Request.QueryString["Username"]) == true)
// {
// //strReturnStatus = "true";
// try
// {
// SqlConnection conn = new SqlConnection(connString);
// conn.Open();
// SqlDataReader myReader = null;
// SqlCommand myCommand = new SqlCommand(cmdText, conn);
// myReader = myCommand.ExecuteReader();
// while (myReader.Read())
// {
// Console.WriteLine(myReader["Name"].ToString());
// Console.WriteLine(myReader["JobTitle"].ToString());
// Repeater1.DataSource = myReader;
// Repeater1.DataBind();
// myReader.Close();
// conn.Close();
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.ToString());
// }
// }
我一直在为代码隐藏而苦苦挣扎。它甚至无法检查用户名,我不知道为什么。另外,我不确定是否应该将任何代码放入其中以显示数据库中的用户信息。
更新:
对于角色,我有三个表用于获取和设置角色。它们的结构如下:
用户表:姓名、用户名、部门(用户名为主键)
角色表: RoleID、RoleName(RoleID为主键)
UserRole表: UserRoleID、Username、RoleID(UserRoleID为主键)
另一个更新(最后):
用户表:姓名、用户名、部门代码(用户名为主键)
部门表表: DepartmentCode,DepartmantName(DepartmentCode为主键)
角色表: RoleID、RoleName(RoleID为主键)
UserRole表: UserRoleID、Username、RoleID(UserRoleID为主键)
我在 Wizard1_NextButtonClick 方法中使用以下查询:
protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
switch (Wizard1.WizardSteps[e.NextStepIndex].ID)
{
case "WizardStep2":
string username = TextBox1.Text;
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
//For checking the user
if (!String.IsNullOrEmpty(username) && CheckUsername(username))
{
try
{
Session["Username"] = username;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
//string cmdText = "SELECT * FROM employee WHERE Username = @Username";
string cmdText = "SELECT dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo," +
"ISNULL(dbo.Roles.RoleID, 3) AS RoleID, dbo.Divisions.DivisionName" +
"FROM dbo.Divisions INNER JOIN dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode" +
"LEFT OUTER JOIN dbo.Roles RIGHT OUTER JOIN dbo.UserRole ON dbo.Roles.RoleID = dbo.UserRole.RoleID ON" +
"dbo.employee.Username = dbo.UserRole.Username" +
"WHERE (dbo.employee.Username = @Username)";
SqlCommand myCommand = new SqlCommand(cmdText, conn);
myCommand.Parameters.AddWithValue("@Username", username);
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
adapter.Fill(table);
string Name = table.Rows[0]["Name"] as string;
string Username = table.Rows[0]["Username"] as string;
//string DivisionName = table.Rows[0]["DivisionName"] as string;
string JobTitle = table.Rows[0]["JobTitle"] as string;
string BadgeNo = table.Rows[0]["BadgeNo"].ToString();
//string role = table.Rows[0]["RoleName"] as string;
lblName.Text = Name;
lblUsername.Text = Username;
//lblDivision.Text = DivisionName;
lblJobTitle.Text = JobTitle;
lblBadgeNo.Text = BadgeNo;
//lblRole.Text = role;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
else
{
//If the user does not exist or a blank value has been entered
//Cancel the nextstep redirection and display an error message in a span
e.Cancel = true;
errorSpan.InnerText = "The user id specified is blank or does not exist";
}
break;
case "WizardStep3":
//Simply bind the radio list
radio1.SelectedValue = lblRole.Text;
break;
}
}
查询将在向导 Step2 中显示我的姓名、用户名、部门(或部门)、职务和徽章编号。此外,它应该在向导步骤 3 中向我显示用户的角色,使管理员能够插入和删除角色而不是更新角色。
我在 SQLServer Management Studio 中测试了查询,它运行良好,但是当我将它放入 C# 代码时,我在网页中没有得到任何结果,我不知道为什么。
【问题讨论】:
-
信息太多了。您能否尝试仅一步编写一个向导并告诉我们它是如何进行的?