如何在“用户管理向导"中放置“删除"选项? [英] How to put Delete option in this User Management Wizard?

查看:93
本文介绍了如何在“用户管理向导"中放置“删除"选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我正在使用的基于Web的应用程序中开发了用户管理模块.通过使用ASP.NET向导控件,它的开发工作如下:
第一步:它包含一个文本框,用于输入员工的用户名
第二步:它显示员工的信息
第三步:赋予用户访问系统的角色

如果用户不在数据库中,则在为用户指定角色后将其添加到系统中,然后单击完成"按钮.我现在想要的是以下内容:在第二步中,当显示用户信息时,我想放置两个选项:删除或添加/编辑角色
如果系统中存在他,将启用删除
添加将一直启用,用于编辑用户的角色

那么有什么办法吗?

我在ASP.NET和C#中的代码如下:

ASP.NET:

I have developed User Management Module in the web-based application that I am working on it. By using ASP.NET Wizard control, it is developed to be worked as following:
First Step: it contains a TextBox for entering the username of employee
Second Step: it shows the information of the employee
Third Step: giving the user the role to access the system

If the user is not in the database, it will be added to the system after specifying a role for him and click on the Finish button. What I want now is the following: In the second step, when the user information be displayed, I want to put two options: Delete or Add/Edit Role
Delete will be enabled, if he is existed in the system
Add will be enabled all the time, for editing the role for the user

So is there any way to do that?

My code in ASP.NET and C# is as following:

ASP.NET:

<div id="wizard" align="center"  runat="server">
        <asp:Wizard ID="Wizard1" runat="server" DisplaySideBar="False" Width="63%" ActiveStepIndex="2"
            OnNextButtonClick="Wizard1_NextButtonClick" OnFinishButtonClick="Wizard1_FinishButtonClick">
            <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>
                            <td>
                            <span id="errorSpan"  runat="server" style="color:Red;"></span>
                            </td>
                        </tr>
                    </table>
                </asp:WizardStep>
                <asp:WizardStep ID="WizardStep2" runat="server" Title="Manage User">
                    <div class="content">
                        <table>
                            <tr>
                                <td>
                                    Name:
                                    <asp:Label ID="lblName" runat="server"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Username:
                                    <asp:Label ID="lblUsername" runat="server"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Division:
                                    <asp:Label ID="lblDivision" runat="server"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Badge Number:
                                    <asp:Label ID="lblBadgeNo" runat="server"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Job Title:
                                    <asp:Label ID="lblJobTitle" runat="server"></asp:Label>
                                </td>
                            </tr>
                        </table>
                    </div>
                </asp:WizardStep>
                <asp:WizardStep ID="WizardStep3" runat="server" Title="Edit User Role">
                    <div class="content">
                        <table>
                            <caption>
                                Current Role:
                                <asp:Label ID="lblRole" runat="server"></asp:Label>
                            </caption>
                            </tr>
                            <tr>
                                <asp:Label ID="Label1" runat="server" BackColor="#FFFF99" Font-Bold="True" ForeColor="#000099" />
                                <asp:RadioButtonList ID="radio1" runat="server" TextAlign="left" RepeatDirection="Horizontal"
                                    DataSourceID="SqlDataSource1" DataTextField="RoleName" DataValueField="RoleID">
                                    <asp:ListItem id="option1" runat="server" Value="Admin" />
                                    <asp:ListItem id="option2" runat="server" Value="Contribute" />
                                    <asp:ListItem id="option3" runat="server" Value="Management" />
                                    <asp:ListItem id="option4" runat="server" Value="User" />
                                </asp:RadioButtonList>
                                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                    SelectCommand="SELECT * FROM [Roles]"></asp:SqlDataSource>
                                <%--<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Clicked" />
                                <span id="infoSpan"  runat="server" style="color:Red;"></span>--%>
                            </tr>
                        </table>
                    </div>
                </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>



C#:



C#:

protected void Page_Load(object sender, EventArgs e)
    {
        //Set the Wizard Step 0 as the initial wizard step when the page loads
        if (!Page.IsPostBack)
        {
            Wizard1.ActiveStepIndex = 0;
        }

        //For sending object to the Wizard1.PreRender
        Wizard1.PreRender += new EventHandler(Wizard1_PreRender);
    }


    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        //If one of the items is selected AND a username exists in the Username session object update the user role
        string username = TextBox1.Text;

        if (!String.IsNullOrEmpty(radio1.SelectedValue) && !String.IsNullOrEmpty(username))
        {
            string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";

            //This for adding the new PMOD user to the system
            string insertUserCommand = "INSERT INTO employee (Name, Username, JobTitle, BadgeNo, EmpOrgType, DivisionCode) values (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)";
            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))
                {
                    if ((int)cmd.ExecuteScalar() == 0){
                        //An object from ObjectUser class to get the user information from the Secure system and insert them to the database
                        ObjectUser user = new ObjectUser(username,true);

                        SqlCommand cmd2 = new SqlCommand(insertUserCommand, conn);
                        cmd2.Parameters.AddWithValue("@Name", user.Name);
                        cmd2.Parameters.AddWithValue("@Username", username);
                        cmd2.Parameters.AddWithValue("@JobTitle", user.GetProperty("EMP_TITLE"));
                        cmd2.Parameters.AddWithValue("@BadgeNo", user.GetProperty("EMP_BADGE_NUMBER"));
                        cmd2.Parameters.AddWithValue("@EmpOrgType", user.GetProperty("EMP_EMPTYPE"));
                        cmd2.Parameters.AddWithValue("@DivisionCode", user.Org.Division.SapCode);
                        cmd2.ExecuteNonQuery();
                    }

                }
            }

            //For updating the role of the user by deleting its current role and inserting a new role
            string deleteCommand = "DELETE FROM UserRole where Username=@Username";
            string insertCommand = "INSERT INTO UserRole (RoleID,Username) values(@RoleID,@Username)";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                using (SqlCommand cmd = new SqlCommand(deleteCommand, conn))
                {
                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.ExecuteNonQuery();
                    //Now the insert
                    cmd.CommandText = insertCommand;
                    cmd.Parameters.Clear(); //need this because still has params from del comm
                    cmd.Parameters.AddWithValue("@RoleID", radio1.SelectedValue);
                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.ExecuteNonQuery();
                    //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                    //cmd.ExecuteScalar();
                    //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                }
            }

            Wizard1.Visible = false;
            wizard.InnerHtml = @"<p>The task has been done successfully. <br /> <a href='UserManagement.aspx'>Edit Another User</a></p>";
        }


    }


    //Method for checking the existence of the username in the database (retrun true or false)
    private bool CheckUsername(string username)
    {
        if (Service.GetPerson(username).GetProperty("RES_NETID").Equals("-"))
            return false;
        else if (Security.isPMODMember(username))
            return true;
        else
            return false;

        //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)
    {
        string username = TextBox1.Text;
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";

        switch (Wizard1.WizardSteps[e.NextStepIndex].ID)
        {
            case "WizardStep2":

                //For checking the user
                if (!String.IsNullOrEmpty(username) && CheckUsername(username))
                {
                    try
                    {
                        SqlConnection conn = new SqlConnection(connString);
                        conn.Open();
                        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, dbo.Roles.RoleName
                                         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);

                        ObjectUser user = new ObjectUser(username, true);

                        string Name = user.Name;
                        string Username = user.ID;
                        string DivisionName = user.Org.Title;
                        string JobTitle = user.GetProperty("EMP_TITLE");
                        string BadgeNo = user.GetProperty("EMP_BADGE_NUMBER");
                        string role = "User";
                        string roleid = "4";
                        if (table.Rows.Count > 0)
                        {
                            role = table.Rows[0]["RoleName"] as string;
                            roleid = table.Rows[0]["RoleID"].ToString();
                        }

                        lblName.Text = Name;
                        lblUsername.Text = Username;
                        lblDivision.Text = DivisionName;
                        lblJobTitle.Text = JobTitle;
                        lblBadgeNo.Text = BadgeNo;

                        lblRole.Text = role;
                        radio1.SelectedValue = roleid;
                    }
                    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 username specified is blank or does not belong to PMOD";
                }

                break;
            case "WizardStep3":

                break;
        }
    }

推荐答案

ConnectionStrings:testConnectionString%> SelectCommand ="SELECT * FROM [角色]"></asp:SqlDataSource> <%-< asp:按钮ID ="Button1" runat =服务器" Text =提交" OnClick ="Button1_Clicked"/> < span id ="infoSpan" runat ="server" style ="color:Red;"></span>-%> </tr> </table> </div> </asp:WizardStep> </WizardSteps> < HeaderTemplate> < ul id ="wizHeader"> < asp:Repeater ID ="SideBarList" runat ="server"> < ItemTemplate> < li>< a class =<%#GetClassForWizardStep(Container.DataItem)%>" title =<%#Eval(" Name)%>"> <%#Eval(名称")%></a> </li> </ItemTemplate> </asp:Repeater> </ul> </HeaderTemplate> </asp:向导>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT * FROM [Roles]"></asp:SqlDataSource> <%--<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Clicked" /> <span id="infoSpan" runat="server" style="color:Red;"></span>--%> </tr> </table> </div> </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>



C#:



C#:

protected void Page_Load(object sender, EventArgs e)
    {
        //Set the Wizard Step 0 as the initial wizard step when the page loads
        if (!Page.IsPostBack)
        {
            Wizard1.ActiveStepIndex = 0;
        }

        //For sending object to the Wizard1.PreRender
        Wizard1.PreRender += new EventHandler(Wizard1_PreRender);
    }


    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        //If one of the items is selected AND a username exists in the Username session object update the user role
        string username = TextBox1.Text;

        if (!String.IsNullOrEmpty(radio1.SelectedValue) && !String.IsNullOrEmpty(username))
        {
            string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";

            //This for adding the new PMOD user to the system
            string insertUserCommand = "INSERT INTO employee (Name, Username, JobTitle, BadgeNo, EmpOrgType, DivisionCode) values (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)";
            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))
                {
                    if ((int)cmd.ExecuteScalar() == 0){
                        //An object from ObjectUser class to get the user information from the Secure system and insert them to the database
                        ObjectUser user = new ObjectUser(username,true);

                        SqlCommand cmd2 = new SqlCommand(insertUserCommand, conn);
                        cmd2.Parameters.AddWithValue("@Name", user.Name);
                        cmd2.Parameters.AddWithValue("@Username", username);
                        cmd2.Parameters.AddWithValue("@JobTitle", user.GetProperty("EMP_TITLE"));
                        cmd2.Parameters.AddWithValue("@BadgeNo", user.GetProperty("EMP_BADGE_NUMBER"));
                        cmd2.Parameters.AddWithValue("@EmpOrgType", user.GetProperty("EMP_EMPTYPE"));
                        cmd2.Parameters.AddWithValue("@DivisionCode", user.Org.Division.SapCode);
                        cmd2.ExecuteNonQuery();
                    }

                }
            }

            //For updating the role of the user by deleting its current role and inserting a new role
            string deleteCommand = "DELETE FROM UserRole where Username=@Username";
            string insertCommand = "INSERT INTO UserRole (RoleID,Username) values(@RoleID,@Username)";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                using (SqlCommand cmd = new SqlCommand(deleteCommand, conn))
                {
                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.ExecuteNonQuery();
                    //Now the insert
                    cmd.CommandText = insertCommand;
                    cmd.Parameters.Clear(); //need this because still has params from del comm
                    cmd.Parameters.AddWithValue("@RoleID", radio1.SelectedValue);
                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.ExecuteNonQuery();
                    //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                    //cmd.ExecuteScalar();
                    //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                }
            }

            Wizard1.Visible = false;
            wizard.InnerHtml = @"<p>The task has been done successfully. <br /> <a href='UserManagement.aspx'>Edit Another User</a></p>";
        }


    }


    //Method for checking the existence of the username in the database (retrun true or false)
    private bool CheckUsername(string username)
    {
        if (Service.GetPerson(username).GetProperty("RES_NETID").Equals("-"))
            return false;
        else if (Security.isPMODMember(username))
            return true;
        else
            return false;

        //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)
    {
        string username = TextBox1.Text;
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";

        switch (Wizard1.WizardSteps[e.NextStepIndex].ID)
        {
            case "WizardStep2":

                //For checking the user
                if (!String.IsNullOrEmpty(username) && CheckUsername(username))
                {
                    try
                    {
                        SqlConnection conn = new SqlConnection(connString);
                        conn.Open();
                        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, dbo.Roles.RoleName
                                         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);

                        ObjectUser user = new ObjectUser(username, true);

                        string Name = user.Name;
                        string Username = user.ID;
                        string DivisionName = user.Org.Title;
                        string JobTitle = user.GetProperty("EMP_TITLE");
                        string BadgeNo = user.GetProperty("EMP_BADGE_NUMBER");
                        string role = "User";
                        string roleid = "4";
                        if (table.Rows.Count > 0)
                        {
                            role = table.Rows[0]["RoleName"] as string;
                            roleid = table.Rows[0]["RoleID"].ToString();
                        }

                        lblName.Text = Name;
                        lblUsername.Text = Username;
                        lblDivision.Text = DivisionName;
                        lblJobTitle.Text = JobTitle;
                        lblBadgeNo.Text = BadgeNo;

                        lblRole.Text = role;
                        radio1.SelectedValue = roleid;
                    }
                    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 username specified is blank or does not belong to PMOD";
                }

                break;
            case "WizardStep3":

                break;
        }
    }


没有人会阅读这个庞大的代码转储.是的,有可能.将所需的控件放在此窗体上,并编写代码以使它们起作用.如果您遇到困难,请仅使用相关的代码片段提出具体问题.
No-one is going to read this huge code dump. Yes, it''s possible. Put the required controls on this form, and write code to make them work. If you get stuck, ask specific questions with relevant code snippets only.


这篇关于如何在“用户管理向导"中放置“删除"选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆