2014-01-24 33 views
0

我有一個包含許多單選按鈕的頁面。我可以檢查是否有任何這些單選按鈕之一被選中?然後更改該選中的單選按鈕的文本?檢查值並更改asp.net頁面上的許多單選按鈕的文本?

我使用asp.net C#3.5

我使用

RadioButton ff = new RadioButton(); 
RadioButton ff2 = new RadioButton(); 
RadioButton ff3 = new RadioButton(); 
RadioButton ff4 = new RadioButton(); 
在for循環中

創造出許多單選按鈕

+0

需要你寫詳細介紹一下aspx頁面。您使用服務器控件或簡單的HTML的複選框? – Raghurocks

回答

1

假設你正在使用一個asp: RadioButton,則一種方法是將RadioButton的AutoPostBack屬性設置爲true。

然後在服務器端的處理程序中,您可以更改文本。

顯然這是有代價的。

例如

<asp:RadioButton ID="ctrlRadioButton" runat="server" AutoPostBack="True" 
    oncheckedchanged="ctrlRadioButton_CheckedChanged" Text="Select this" /> 
在你的代碼類

然後

protected void ctrlRadioButton_CheckedChanged(object sender, EventArgs e) 
{ 
    ctrlRadioButton.Text = "New Text"; 
} 

更新:

如果創建動態的單選按鈕:

protected void Page_Init(object sender, EventArgs e) 
{ 
    for (int i = 0; i < 4; ++i) 
    { 
     RadioButton rb = new RadioButton() { AutoPostBack = true, Text = "Initial text" }; 
     rb.CheckedChanged += RadioButton_CheckedChanged; 
     Form.Controls.Add(rb); // Or add to a panel if you prefer 
    } 
} 

和處理程序

protected void RadioButton_CheckedChanged(object sender, EventArgs e) 
{ 
    RadioButton rb = (RadioButton)sender; 
    rb.Text = "New Text"; 
} 
1

您可以通過使用jquery的type="radio" 您的網頁上的所有單選按鈕,運行循環,並發現檢查單選按鈕,並更改單選按鈕的文字。

試試這個代碼:

$('input[type="radio"]').each(function(){ 
if($(this).is(':checked')) { $(this).html("yourname") } 
});