ASP.NET - 从代码隐藏中评估按钮值

ASP.NET - evaluating button value from code behind

本文关键字:评估 按钮 隐藏 代码 NET ASP      更新时间:2023-09-26

我有一个asp按钮,当用户单击它时,它会调用C#方法:

<asp:Button id="btnReport2" name="btnReport2"  runat="server" Text="Show Report" ToolTip="Report" OnClick="btnReport2_Click"/>

以下方法称为:

 protected void btnReport2_Click(object sender, EventArgs e)
        {
            if (btnReport2.Text == "Show Report")
            {
                GetReport();
            }
            ClientScript.RegisterStartupScript(Page.GetType(), "ShowReport", "ShowReport()", true);
    }

如您所见,在方法的末尾,我调用了一个jQuery脚本:

function ShowReport() {
            debugger;
            if ($('#btnReport2').val() == 'Show Report') {
                $('#btnReport2').val('Hide Report');
                $('#spPrint').attr('style', 'display:inline');
                $('#btnDeleteAll2').animate({ width: ['toggle', 'swing'], heigth: ['toggle', 'swing'], opacity: 'toggle' }, 500, "swing");
                $('#divReport').animate({ width: ['toggle', 'swing'], heigth: ['toggle', 'swing'], opacity: 'toggle' }, 500, "swing");
                $('#divResult').slideToggle(500);
            } else {
                $('#btnReport2').val('Show Report');
                $('#spPrint').attr('style', 'display:none');
            }
        }

单击按钮时,我将其文本更改为"隐藏报告"。但是,当我在调试时再次单击它时,btnReport2.Text 属性仍设置为"显示报告",这对我来说很奇怪,因为它显示"隐藏报告"。

对这个问题有任何想法吗?

您仅在客户端而不是服务器端更改了按钮的值。这就是为什么在服务器端调试时值仍然保持不变的原因。