You must Sign In to post a response.
  • Category: ASP.NET

    Visibility of the control by code behind in ASP.Net

    Hi Guys,
    I just want to set the visibility of the button control through the code behind file. Can you please send me the code.
    I am trying with the below code but it seems not able to get it.
    Code behind file code:

    public partial class Default2 : System.Web.UI.Page
    {
    public static bool IsVisible = true;
    protected void Page_Load(object sender, EventArgs e)
    {
    IsVisible = false;
    }
    }

    .aspx file code:

    <div id="mydiv">
    <asp:Button ID="Button1" Text="Help" runat="server" Visible="<%# !IsVisible %>" />
    </div>

    But it is not making the visible as false.
    Thanks in advance.
  • #755941
    Hi Pawan,

    I think the problem is before page load event source code controls will execute you are assign the visibility of the control on your code behind after that the control is not refreshed in source may be that is the issue.

    better to do like this.


    protected void Page_Load(object sender, EventArgs e)
    {
    IsVisible = false;
    Button1.Visible = IsVisible;
    }


    I'm not sure I'm guessing, if any thing is wrong then correct me.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #755943
    Hai Naveen,
    This is not my scenario which you described. Actually I want to implement the access-permissions for the button controls. Let 's say based on the user, I want to show/hide the buttons. All the access-permissions are there in the database and I want to get them once based on the user type and then want to set it in the controls visibility. I don't want to write it in the code behind for each of my page.
    Let me know if anyone is having the solution.
    Thanks in advance.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #755947
    Hi
    Pawan try this

    Client Side
    ===============

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div id="mydiv" style='<%=IsVisible ? "" : "display: none" %>'>
    <asp:Button ID="Button1" Text="Help" runat="server" />
    </div>
    <asp:HiddenField ID="HndVisible" runat="server" />
    </form>
    </body>
    </html>

    Server Side
    ============

    public static bool IsVisible = true;
    protected void Page_Load(object sender, EventArgs e)
    {
    IsVisible = false;
    Session["IsVisible"] = false;
    // Button1.Style.Add("display", "none");
    }
    Working Good this.

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #755948
    Awasthi Ji,

    I would go with below lines of modification in your code.


    .aspx code
    <asp:Button ID="Button1" Text="Help" runat="server" Visible="false" />

    .aspx.cs code

    //Wherever you want to check the box visibility put there

    //get database value from database
    bool IsVisible = ds.Tables[0].Rows[0]["Visibility"]; //Please parse code correctly, because I am posting from tab where no visual studio
    if(IsVisible == true)

    Button1.Visible = true;

    else( )


    Thankss

    -------------
    Glad to be,
    John Bhatt
    Editor - DNS Forums
    https://www.pyarb.com

  • #755960
    Hi Pawan,

    Try like below.


    <asp:Button id="btnStatus" runat="server" Visible='<%#Eval("Status")=="1"?"true":"false" %>' Text="Status"></asp: Button>


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #755961
    Sorry guys,
    This is not the way to implement. After keep on trying with the various ways, at last I got the Solution.
    There is a method called as DataBind() under the Page class which is used to bind the dynamic values to the controls at the runtime.
    So in this case we can have the same method in the btnLogin_Click event as we are going to set it

    protected void btnLogin_Click(object sender, EventArgs e)
    {
    //set the access here in the Login page just after the credentials are valid for the logged-on user
    Common.SetAccessControls(2);
    Page.DataBind();
    }

    Rest of the things are same. As per my scenarios, I am getting the values of the button controls visibility based on the access permissions from the database and wanted to set to the code level.
    So now i can just get the access permissions and keep it in some static properties. These permissions will be based on the user type and then I can just set it like:

    <div id="Div1" runat="server">
    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click"/>
    <asp:Button ID="btnAdd" runat="server" Text="Add" Visible='<%# Common.IsAdd %>' />
    <asp:Button ID="btnDelete" runat="server" Text="Delete" Visible='<%# Common.IsDelete %>' />
    <asp:Button ID="btnView" runat="server" Text="View" Visible='<%# Common.IsView %>' />
    <asp:Button ID="btnEdit" runat="server" Text="Edit" Visible='<%# Common.IsEdit %>' />
    <asp:Button ID="btnShare" runat="server" Text="Share" Visible='<%# Common.IsShare %>' />
    </div>

    Anyway, thanks for trying with various ways.
    Hope it will be helpful to others.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.