0

如何使用 JavaScript 为 C# 类中的单选按钮控件实现“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?');"); 
4

3 回答 3

1
Button1.Attributes.Add("onclick", "javascript:document.getElementById('" + TextBox1.ClientID + "').value = 'Button1'");

      Button2.Attributes.Add("onclick", "javascript:document.getElementById('" + TextBox1.ClientID + "').value = 'Button2'");
于 2012-10-19T03:59:06.690 回答
1

试试这个

document.getElementById('myTextbox').value = 'Button1';
于 2012-10-18T23:35:05.390 回答
1

因为您的隐藏文本框在服务器端运行,所以它的 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 控件。

于 2012-10-18T23:45:19.480 回答