javascript - 将 javascript 代码移动到设计 View 不起作用。仅代码隐藏 Attributes.Add ("onclick"有效。困惑

标签 javascript asp.net onclick code-behind

我想在单击按钮后禁用该按钮,同时触发回发事件以生成报告。我的第一组代码不起作用,因为禁用按钮后不久,页面将不会提交/回发。这是第一组未实现的代码。 onclientclick 调用一个包含这些行的 javascript 函数

document.getElementById('btnGenerateReport').disabled=true; GetPostBackEventReference(btnGenerateReport,'');

因为它没有回发

我在 page_load 代码后面尝试了以下操作

btnGenerateReport.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(btnGenerateReport, ""))

效果很好。但我尝试复制生成的 JavaScript 并直接粘贴到设计 View

onclick="this.disabled=true;__doPostBack('btnDownloadClientsWithConviction','');" 

在我禁用attributes.add后面的代码后,它不能单独从客户端工作 但是当我检查 View 源时,两个页面是相同的

为什么我无法将代码从代码隐藏移动到设计 View ?

最佳答案

因为 Button.ClientId 是使用 NamingContainer 生成的.

试试这个:

document.getElementById('<%= btnGenerateReport.ClientId %>').disabled=true; 
GetPostBackEventReference('<%= btnGenerateReport.ClientId %>','');

关于javascript - 将 javascript 代码移动到设计 View 不起作用。仅代码隐藏 Attributes.Add ("onclick"有效。困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3258186/

相关文章:

javascript - 如何判断 Vue 组件是否处于事件状态

javascript - 使用新的 ES6 Array.from() 方法

Javascript:获取元素的当前 "onclick"内容

jquery - 在第一次点击时更改背景图片 - 然后在第二次点击时使用 jquery 再次将其更改回来

javascript - 在javascript中引用正则表达式文字?为什么不?

javascript - jQuery 单击事件显示 JSON 数组中的特定数据无法正常工作

c# - 在 C# 的中继器中选择 CheckBox 时如何避免页面刷新?

c# - 检查 asp.net mvc 中是否已经过去了 x 个月

javascript - 输入隐藏控件不会在回发之间保留 his 值

javascript - jquery Click事件不会触发