【问题标题】:Grouping controls on different child pages in asp.netasp.net中不同子页面上的分组控件
【发布时间】:2013-01-10 15:45:45
【问题描述】:

我所做的是登录,用户名出现在我的母版页中的标签中。我需要做的是,如果登录用户具有管理员权限,则可以在多个子页面上设置可见控件(包括一个子页面上的 gridview deletecontrol)。一直在努力弄清楚。只是想学习。

有没有办法在类调用管理员下对所有控件进行分类,并从母版页调用检查用户权限?

后面的登录页面代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;

public partial class login1 : System.Web.UI.Page
{

    public void Page_Load(object sender, EventArgs e)
    {

    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {        
        DataSet ds = new DataSet();
        ds = WCGSQL.showdata("select * from Login where Username='" + UserName.Text + "' and Password='" + Password.Text + "'");
        if (ds.Tables[0].Rows.Count != 0)
        {

            Session["Username"] = UserName.Text;
            Response.Redirect("Home.aspx");

        }
        else
        {
            FailureText.Visible = true;
            FailureText.Text = "Invalid Login";
        }
    }
}

应用程序/代码背后的代码

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public class WCGSQL
{
    static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|WCG.mdf;Integrated Security=True;User Instance=True");
    static public Boolean savedata(string qurt)
    {
        try
        {
            SqlCommand cmd = new SqlCommand(qurt, con);
            con.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            con.Close();
        }


    }

    static public DataSet showdata(string qurt)
    {
        DataSet ds = new DataSet();
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter(qurt, con);
            adp.Fill(ds);
            return ds;
        }
        catch
        {
            return ds;
        }
    }

}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您想使用LoginView 控件。请参阅ASP.NET Login Controls Overview 了解整个套件。


    来自 MSDN 的示例:

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
        <head runat="server">
        <title>ASP.NET Example</title>
    </head>
    <body>
            <form id="form1" runat="server">
                <p>
                    <asp:LoginStatus id="LoginStatus1" runat="server"></asp:LoginStatus></p>
                <p>
                    <asp:LoginView id="LoginView1" runat="server">
                        <AnonymousTemplate>
                            Please log in for personalized information.
                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            Thanks for logging in 
                            <asp:LoginName id="LoginName1" runat="Server"></asp:LoginName>.
                        </LoggedInTemplate>
                        <RoleGroups>
                            <asp:RoleGroup Roles="Admin">
                                <ContentTemplate>
                                    <asp:LoginName id="LoginName2" runat="Server"></asp:LoginName>, you
                                    are logged in as an administrator.
                                </ContentTemplate>
                            </asp:RoleGroup>
                        </RoleGroups>
                    </asp:LoginView></p>
            </form>
        </body>
    </html>
    

    【讨论】:

      【解决方案2】:
      if (HttpContext.Current.User.IsInRole("member"))
      {
        //enable/disable here
      }
      

      【讨论】:

      • 哇,听起来很简单,但这里的小问题是内置登录控制的一部分,因为我没有使用内置登录,我使用以下 code protected void LoginButton_Click(object sender, EventArgs e) { DataSet ds = 新数据集(); ds = WCGSQL.showdata("select * from Login where Username='" + UserName.Text + "' and Password='" + Password.Text + "'"); if (ds.Tables[0].Rows.Count != 0) { Session["Username"] = UserName.Text; Response.Redirect("Home.aspx"); } 其他 {} code
      • 马克,当用户被认证时,你创建一个ticket吗?请编辑您的问题并添加更多代码,以便我们了解到底发生了什么。
      • 在您的方法中包含临时 sql 也是一种非常非标准的方式。你可以使用存储过程。
      • 试试这个示例项目。它会给你一个好主意http://www.codeproject.com/Articles/37660/Fully-configured-ASP-NET-Membership-Website-Templa
      • 好的,我编辑了问题以添加更多代码,还将提供 Tammy 给定的链接,感谢大家的帮助,非常感谢
      【解决方案3】:

      嘿,我做过一个这样的应用程序。您需要做的是为管理员和其他用户创建单独的网页(即在登录页面之后)。然后,您可以通过他的凭据轻松地对管理员进行身份验证并重定向到相应的页面。在那里你可以根据需要放置一些控件。

      如果您关心的是创建一个像控制访问板这样的应用程序,管理员可以在其中授予模块权限以显示在其他用户的子页面上显示的内容,然后通过您的回复告诉我。

      【讨论】:

      • @Durgesh Khandal 我可以做到这一点,但事实是用户和管理员都在查看相同的页面,只是有一些控制差异,并且不想通过复制来使文档变大表格只是为了添加一些命令......只是为了给你一个例子在一个页面上有一个 GridVeiw1 并且用户可以选择而管理员可以删除和添加访问它只是使这两个相应地可见或不可见的问题但是如果这是唯一可行的解​​决方案,那么我想我将不得不这样做
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2012-05-23
      • 1970-01-01
      相关资源
      最近更新 更多