Disable some Button in all Pages using Master Page in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

I have a application in which there are more than 50 pages .

My requirement is that i want to disable some button in all pages using master page on condition.

Let clear once again.I want to disable some button only not all in whole application.

Posted 4 months ago Modified on 4 months ago

Hi iammann,

Use CSS to disable the button.

For the button you want to disable add the attribute disabled to it.

When not required remove the attribute disabled.

HTML

<asp:Button ID="btnAdd" Text="Add" runat="server" OnClick="Add" disabled />
<asp:Button ID="btnUpdate" Text="Update" runat="server" OnClick="Update" />
<asp:Button ID="btnDelete" Text="Delete" runat="server" OnClick="Delete" disabled />

Demos

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        button:disabled, button[disabled]
        {
            border: 1px solid #999999;
            background-color: #cccccc;
            color: #666666;
        }
    </style>
</head>
<body>
    <input type="submit" name="btnAdd" value="Add" id="btnAdd" disabled="" />
    <input type="submit" name="btnUpdate" value="Update" id="btnUpdate" />
    <input type="submit" name="btnDelete" value="Delete" id="btnDelete" disabled="" />
</body>
</html>

Demo


Posted 4 months ago

thanks dharmendra sir,

But my i think you have not understand exactly what i want.

my question is that i have multiple pages in a application and there is many buttons in all pages.i want to disable some buttons in all pages using a common code but from the server side not css or javascript.

suppose a user logged in and i want that particular user should not be able to delete some data in whole appliation so how can i implement this .

My application is ready but now this scenario came from the client to do this .so i cant create any role for that particular user.

using some condtions i have to do it but on application level.not a single page.


Posted 4 months ago

Hi iammann,

Refer below sample. I have checked the condition with Button Text. You can change it as per your need.

HTML

MasterPage.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Default.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Button ID="btnDisplay" Text="Display" runat="server" />
    <asp:Button ID="btnUpdate" Text="Update" runat="server" />
    <asp:Button ID="btnDelete" Text="Delete" runat="server" />
</asp:Content>

CS.aspx

UserName : <asp:TextBox runat="server" ID="txtUser" /><br />
<asp:Button Text="Login" runat="server" OnClick="Login" />

Code

C#

CS.aspx.cs

protected void Login(object sender, EventArgs e)
{
    Session["User"] = txtUser.Text;
    Response.Redirect("Default.aspx");
}

MasterPage.master.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["User"].ToString() == "Admin")
    {
        HideDeleteButton();
    }
}

private void HideDeleteButton()
{
    foreach (Button btn in this.ContentPlaceHolder1.Controls.OfType<Button>())
    {
        if (btn.Text == "Delete")
        {
            btn.Enable = false;
        }
    }
}

VB.Net

VB.aspx.vb

Protected Sub Login(ByVal sender As Object, ByVal e As EventArgs)
    Session("User") = txtUser.Text
    Response.Redirect("Default.aspx")
End Sub

MasterPage.master.vb

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Session("User").ToString() == "Admin" Then
            HideDeleteButton()
        End If
    End Sub

    Private Sub HideDeleteButton()
        For Each btn As Button In Me.ContentPlaceHolder1.Controls.OfType(Of Button)()
            If btn.Text == "Delete" Then
                btn.Enable = False
            End If
        Next
    End Sub

Screenshot