2012-07-23 67 views
0

我有一些在服務器上生成的按鈕,每個按鈕都是唯一的。添加確認javascript到asp.net生成的鏈接

我希望能夠有一個彈出窗口來詢問用戶是否肯定要點擊,然後讓它跟隨回發。

生成的按鈕的代碼是:

<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 

反正而產生上方的按鈕我可以添加確認JavaScript代碼,所以它將在這裏呈現:

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

感謝您的任何幫助,

馬克

回答

0

您可以生成按鈕時做到:

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

或客戶端,使用jQuery

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

我,因爲你有點迷茫的說按鈕,但結果是一個<a>。爲什麼不使用ASP.NET圖像按鈕?

HTML

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

Serverside集團

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