向asp.net生成的链接添加确认javascript

Adding confirm javascript to asp.net generated links

本文关键字:链接 添加 确认 javascript asp net      更新时间:2024-01-22

我在服务器上生成了许多按钮,每个按钮都是唯一的。

我希望能够有一个弹出窗口来询问用户是否确实想点击,然后让它完成回发。

生成的按钮代码为:

<a id="ctl00_ContentPlaceHolder1_btnID9994"
   href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnID9994','')">
    <img src="mt_locked.gif"/>
</a>
<a id="ctl00_ContentPlaceHolder1_btnID9995"
   href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnID9995','')">
    <img src="mt_locked.gif"/>
</a>

按钮使用以下方法生成:

 objBtn.ID = "btnID" & room_id.ToString
 objBtn.CommandArgument = "Unblock"
 AddHandler objBtn.Click, AddressOf btnClicked

我可以在生成上面的按钮时添加Confirm javascript代码吗,所以它将在这里的某个地方呈现:

href="javascript:***confirm code here somewhere - if ok then continue to:... __doPostBack('ctl00

感谢您的帮助,

标记

我有点困惑,因为你说的是按钮,但结果是<a>。为什么不使用ASP.NET图像按钮呢。

HTML

<asp:ImageButton runat="server" ID="btn_YourButton" ImageUrl="pathToImage" OnClick="btn_YourButton_Click" />

服务器端

btn_YourButton.Attributes.Add("onclick", string.Format(@"javascript:; if(!confirm('Are you sure you want to delete the user ""{0}""?'))return false;", username));

生成按钮时可以执行以下操作:

objBtn.Attributes.Add("onclick", " return confirm('confirmation message');");

或通过客户端,使用jQuery

$(function(){
   $("a[id=*btnID]").click(function(){
      return confirm('confirmation message');
   });
});