[英]onclick event for radio button using c#
如何使用 JavaScript 為 C# class 中的單選按鈕控件實現“onclick”事件以更改隱藏的服務器控件文本框文本值以反映單擊了哪個按鈕? 這些按鈕是在客戶端上運行的 html 控件。
假設我有 2 個單選按鈕:Button1 和 Button2。 在我的 C# 代碼中,我需要替換下面的警報調用來更新我的 Textbox 控件,此隱藏中的值將在 PostBack 上處理。
Button1.Attributes.Add("onclick", "alert(' You clicked Button 1. How do I update textBox.Text? in here?');");
Button2.Attributes.Add("onclick", "alert('You clicked Button 2. How do I update textBox.Text? in here?');");
試試這個
document.getElementById('myTextbox').value = 'Button1';
因為您的隱藏文本框在服務器端運行,所以它的 ID 是未知的。 您需要獲取客戶端 ID。
Button1.Attributes.Add("onclick", "javascript:document.getElementById('" + Textbox1.ClientId + "').value = this.ID");
Button2.Attributes.Add("onclick", "javascript:document.getElementById('" + Textbox1.ClientId + "').value = this.ID");
請記住,您需要使用 JavaScript 隱藏文本框,或者改用 hiddenInput 控件。
Button1.Attributes.Add("onclick", "javascript:document.getElementById('" + TextBox1.ClientID + "').value = 'Button1'");
Button2.Attributes.Add("onclick", "javascript:document.getElementById('" + TextBox1.ClientID + "').value = 'Button2'");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.