本文介绍了如何在运行时在.NET 3.5中更改ASP.NET控件的可见属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Default.aspx页面具有一些控件.一些控件的可见性取决于条件.在这里,趋向于完成的是根据条件值在运行时更改visible属性.

Sampel标记(静态模式下为Default.aspx)

A Default.aspx page has some controls. Some controls visibility depends upon conditions. Here, what is tend to accomplish is change the visible property at runtime depending upon the conditional value.

Sampel Markup (Default.aspx in Static Mode)

<div id="DivBtnImgCopy" runat="server" Visible = "True">
    <asp:ImageButton ID="BtnImgCopy" CssClass="image" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif" runat="server" OnClientClick="CopyImage(); SelectButton(this,true);return false;" />
</div>


我试过的是在文件后面的代码中编写一个方法,并尝试从该方法获取值以将visible属性设置为true或false.

CodeBehindFile(Default.aspx.cs)


What I tried is write a method in code behind file and tried to get value from that method to set visible property to true or false.

CodeBehindFile (Default.aspx.cs)

protected bool ShowHideButton()
    {
        bool bStatus = false;
        try
        {
            if (sCondition == "false")
            {
                bStatus = false;
            }
            else if (sCondition == "true")
            {
                bStatus = true;
            }
            return bStatus;
        }
        catch { }
    }


标记示例(动态模式下为Default.aspx)


Sample Markup (Default.aspx in Dynamic Mode)

<div id="DivBtnImgCopy" runat="server" visible = "<% =ShowHideButton() %>">
   <asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif" runat="server" />
</div>



但是,得到以下错误:无法从其字符串表示形式<%= ShowHideButton()%>"的"Visible"属性创建类型为"System.Boolean"的对象.

完成此任务的任何解决方案或解决方法.需要帮助.



But, Getting below error: Cannot create an object of type ''System.Boolean'' from its string representation ''<%=ShowHideButton() %>'' for the ''Visible'' property.

Any solution or work-around to accomplish this task. Need Help.

推荐答案

<div id="DivBtnImgCopy"  runat="server" visible="<%# ShowHideButton()%>">
   <asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif" runat="server" />
</div>



并更新后面的代码,如下所示.



And update code behind as below.

protected bool ShowHideButton()
    {
        bool bStatus = false;
        try
        {
            if (sCondition == "false")
            {
                bStatus = false;
            }
            else if (sCondition == "true")
            {
                bStatus = true;
            }
         }
        catch { }
return bStatus;

    }




希望这会起作用.




Hope this will work.


<div id="DivBtnImgCopy" runat="server" visible = "<% =bool.Parse(ShowHideButton().ToString()) %>">


protected string ShowHideButton()
    {
        string bStatus = "";
        try
        {
            if (sCondition == "false")
            {
                bStatus = "none";
            }
            else if (sCondition == "true")
            {
                bStatus = "";
            }            
        }
        catch { }
        return bStatus;
    }



如下修改您的div.



modify your div as below.

<div id="DivBtnImgCopy">
   <asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif" runat="server" />
</div>

<script type="text/javascript">
document.getElementById("DivBtnImgCopy").style.display = "<%=ShowHideButton() %>";       
</script>


这篇关于如何在运行时在.NET 3.5中更改ASP.NET控件的可见属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:47