Diable some button in whole asp.net web application

Last Reply 16 days ago By pandeyism

Posted 18 days 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.

You are viewing reply posted by: pandeyism 16 days ago.
Posted 16 days 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