基于权限的GridView编辑警报
Alert on GridView edit based on permissions
我有一个gridview的编辑选项,能够编辑gridview行。在我的web应用程序中也有不同的用户权限,如Admin, SuperUser, user。如果用户不是Admin
,并且他试图编辑一行,我需要给出如下所示的警报。但是我下面使用的js会给所有用户警报,因为没有验证设置。
MY TRY SO FAR
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
表中,我有一个名为Roles的列,我需要检查用户是否不是Admin并需要发出警报。非常感谢大家的建议和帮助。
试试这个
脚本为
<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>
And function call as OnClientClick="return myFunction();"
你可以这样做。
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
{
Button btnEdit = e.Row.FindControl("btnEdit") as Button;
if(lblUser.Text !="Admin")
btnEdit.OnClientClick = "alert('Not allowed');return false;";
}
}
如果你想使用你的函数,你可以做如下修改:
btnEdit.OnClientClick = "return myFunction()";
为什么不在服务器端做呢?当您进入Edit功能时,检查权限。这样,一个"聪明"的用户仍然无法编辑,因为他绕过了你的客户端检查。
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- ExtJS 5用程序点击actioncolumn gridview
- 高亮显示时编辑文本大小和颜色
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 将事件聚焦/模糊在可编辑内容的元素上
- 编辑HTML表的源数据
- ExtJS网格单元格编辑器,防止焦点松动问题
- 如何在visualstudio中调试web api时编辑javascript文件
- 具有所有样式的文本正在复制到可编辑文本区域
- 可以't使用PHP使用Froala编辑器上传图像URL
- 用Greasemonkey编辑专栏
- 如何在corona sdk中从CK编辑器中检索数据
- 基于数据类型的编辑框的汇总列表
- 在gridview中只选择一个复选框,然后编辑选中的行
- 使用使用rel属性显示的模式编辑gridview单元格
- 如何获得在javascript中gridview的编辑项模板内放置的控件的客户端
- 基于权限的GridView编辑警报
- 如何在GridView'的点击事件上附加JavaScript;s编辑按钮/删除按钮
- 从gridview编辑模式中获取要由Javascript使用的控件的ID