c# - 根据权限对 GridView 编辑发出警报

标签 c# javascript asp.net gridview

我有一个带有编辑选项的 GridView ,可以编辑 GridView 行。我的网络应用程序中还提供了不同的用户权限,例如管理员、 super 用户、用户。如果用户不是管理员并且他尝试编辑一行,我需要发出如下所示的警报。但我下面使用的 js 将为所有用户发出警报,因为没有设置验证。

到目前为止我的尝试

GridView:

<ItemTemplate>
    <asp:ImageButton ID="btnEdit" OnClientClick="myFunction()" runat="server" CommandName="Edit" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ImageUrl="~/Images/Edit.gif" />
</ItemTemplate>

Javascript:

function myFunction() {
        var edit;
        if (confirm("For ADMIN only. Do not edit/delete the row. Still want to continue?") == true) {
        }
        else {
            window.location.reload();
        }
        document.getElementById("btnEdit").innerHTML = edit;
    }     

捕获当前登录用户:

此外,我还使用以下代码捕获了同一页面中当前登录的用户。当前登录的用户信息保存在标签中。

protected void Page_Load(object sender, EventArgs e)
{
    checkUser();
}

public void checkUser()
{
    string currentUser = HttpContext.Current.Request.LogonUserIdentity.Name;
    string[] words = currentUser.Split('\\');
    currentUser = words[1];
    DataSet ds = new DataSet();
    using (SqlConnection conn = new SqlConnection(strcon))
    {
        SqlCommand command = new SqlCommand();
        command.Connection = conn;
        string strQuery = "select UserId from Permissions where UserId='" +  currentUser + "'";
        SqlCommand cmd = new SqlCommand(strQuery, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
    }
    lblUser.Text = ds.Tables[0].Rows[0]["UserName"].ToString();
}

Permissions表中,我有一个名为“Angular 色”的列,我需要检查用户是否不是管理员并且需要发出警报。非常感谢对此的任何建议或帮助。

最佳答案

试试这个方法

脚本为

     <script language="javascript" type="text/javascript">
      function myFunction() {
          var edit = confirm("For ADMIN only. Do not edit/delete the row. Still want to continue?");
          if (edit) {
              return true;
          }
          else {
              return false;
          }
       } 
      </script>

函数调用为 OnClientClick="return myFunction();"

关于c# - 根据权限对 GridView 编辑发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24161646/

相关文章:

c# - 货到付款插件在 NopCommerce 的付款方式页面中不可用

c# - Roslyn 可以安装在 VS 社区版上吗?

javascript - javascript中的字符串操作

javascript - Javascript 中的 BarProp 是什么?

asp.net - 将 MasterPage ImageButton 事件传递给内容页面

c# - javascript 中的 Web.config 值

c# - IQueryable 扩展方法成为表达式

javascript - 如果你在使用 React.js,你还会编写 HTML/CSS 文件吗?

c# - 使用 Microsoft Web Optimization Framework 时不要丑化某些文件

c# - 使用 SyndicationItem 类在 rss 提要中显示图像