Setting Button visibility to false code behind

I have a list of buttons around 100. I'm setting visibile to false in aspx page and then when needed I'm trying to set visibility to true from code behind.

I cannot do Button1.Visible = true; (where Button1 is the Id of the button because there are about 100 buttons)

Is there any other way to set visibility to true from code behind? I'm creating a function which will accept the Id of the button and then I want to set visibility to true.


You can use "Display:none" instead of "Visible = false" like below code.

Button1.Style.Add("Display","none"); // for hide
Button1.Style.Add("Display","block"); // for show 

This will hide your button from screen.

Also another way if you want to do on Design page instead of Code behind you can write your condition like,

<% if("your condition") {<%>
 //enter your HTML part here
<%} else{%>
// HTML
<%}%>

Use of this you can hide your button from design screen too.


If your buttons are in some parent control with runat="server" , then you can get the collection of controls from there in the Controls property of the parent control. Then you can check if the control is Button or not and then you can set Visible = "false" from the code behind.
Example: if you have buttons inside a div with id="div1" then you can code like below

foreach (var c in div1.Controls)
    if (c is Button)
       (c as Button).Visible = false;

I think you can handle it from the client side is better.You just put some class names along with the buttons you want to show or hide. Suppose your buttons class name is 'myClassName'. Then write a style as shown below

.myClassName
{
    display:none;
}

Then remove the visible=false property from the button and put the class instead of it. Then write the javascript method as shown below.

function ShowButtons()
{
    $('.myClassName').show();
}

Then call the below line of code from your code behind. It will call the javascript method from the code behind.

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","ShowButtons()",true);

Try this one.

链接地址: http://www.djcxy.com/p/31002.html

上一篇: JavaScript图表库

下一篇: 将按钮可见性设置为后面的错误代码