I have a formview displaying data with an option to edit each row. There are different user roles in my system such as Admin, SuperUser, and User. If a non-Admin user tries to edit a row, I want to display a warning message. Currently, the JavaScript function I implemented shows the alert for all users because there is no validation in place.
ATTEMPT SO FAR
FormView:
<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;
}
Identifying current user:
The current logged-in user's information is displayed on the page using the following code snippet. The username is stored in a label.
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();
}
Within the Permissions
table, I have a column called 'Roles' which I need to use to verify if the user is not an Admin and provide an alert accordingly. Any guidance or assistance on achieving this would be greatly appreciated.