2013-04-07 36 views
0

我有一個radiobuttonlist我改變所選擇的項目中codebehidRadioButtonList的SelectedIndexChanged事件射擊意外

private void DisplayPrivacyTerms(long ImageId) 
{ 
    if (ImageryDataAccess.GetImagePrivacyTerm(ImageId).ToLower() == "me only") 
    { 
     RadioButtonListPrivacy.Items[0].Selected = true; 
    } 
    if (ImageryDataAccess.GetImagePrivacyTerm(ImageId).ToLower() == "friends") 
    { 
     RadioButtonListPrivacy.Items[1].Selected = true; 
    } 
    if (ImageryDataAccess.GetImagePrivacyTerm(ImageId).ToLower() == "public") 
    { 
     RadioButtonListPrivacy.Items[2].Selected = true; 
    } 
} 

時所選擇的項目更改上述方式,後來postback到服務器觸發selectedindexchanged事件。
特別是我有listview,它顯示imagebuttons。當我點擊listview的ImageButton的,如果選擇的項目更改然後ImageButton的後點擊觸發selectedinexchanged事件的radiobuttonlist ..
這究竟是爲什麼我不希望這會觸發該事件..

+0

問題太模糊瞭解?請更具體一點。 – 2013-04-07 12:18:49

+0

實際上問題在於以編程方式更改所選項目。如何有效地改變radiobuttonlist.items []。selected屬性?當它觸發selectedindexchanged事件? – spiral 2013-04-07 12:22:50

回答

0

我不完全確定你想要達到的目標。但好像你需要處理一些邏輯上的OnSelectedIndexChanged事件您RadioButtonList

首先設置AutoPostBack="true"財產上的RadioButtonList

然後在OnSelectedIndexChanged事件,寫你的邏輯。

protected void RadioButtonListPrivacy_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    // your logic here 
    // so basically when you click on any of the items in your radiobuttonlist, 
    // this event will fire and you can write your logic based on it 
} 
+0

當我更改SELECTED = TRUE PROPETY,然後點擊其他按鈕,然後返回服務器觸發器,這個事件... – spiral 2013-04-07 13:10:26

+0

檢查我自己的答案,並確切地告訴我視圖狀態是如何做到這一點的?我仍然不確定什麼是錯的 – spiral 2013-04-07 14:27:13

0

其實問題是我已經在aspx頁面中聲明瞭初始化項目。我麻煩改變了功能,類似於下面的代碼

private void DisplayPrivacyTerms(long ImageId) 
     { 
    RadioButtonListPrivacy.Items.Clear(); 
    ListItem itemMe= new ListItem("Me Only", "1"); 
    RadioButtonListPrivacy.Items.Add(itemMe); 
    ListItem itemMates = new ListItem("Subject Mates", "2"); 
    RadioButtonListPrivacy.Items.Add(itemMates); 
    ListItem itemPublic = new ListItem("Public", "3"); 
    RadioButtonListPrivacy.Items.Add(itemPublic); 

    if (ImageryDataAccess.GetImagePrivacyTerm(ImageId).ToLower() == "me only") 
    { 
     RadioButtonListPrivacy.Items[0].Selected = true; 
    } 
    if (ImageryDataAccess.GetImagePrivacyTerm(ImageId).ToLower() == "subject mates") 
    { 
     RadioButtonListPrivacy.Items[1].Selected = true; 
    } 
    if (ImageryDataAccess.GetImagePrivacyTerm(ImageId).ToLower() == "public") 
    { 
     RadioButtonListPrivacy.Items[2].Selected = true; 
    } 

} 

我清除列表,然後補充說方式,生龍活虎與只是改變了選擇= true屬性所產生的視圖狀態問題的新條目.. :)

相關問題