radiobuttonlist selectedindexchanged事件触发意外

时间:2013-04-07 11:47:04

标签: asp.net events viewstate init radiobuttonlist

我有radiobuttonlist我在codebehid

中更改了所选项目
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中的图像按钮并且所选项目已更改时,稍后单击图像按钮会触发selectedinexchanged事件radiobuttonlist ..
为什么会发生这种情况我不希望这会触发这个事件..

2 个答案:

答案 0 :(得分: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  
}  

答案 1 :(得分: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;
    }

}

我清除了列表,然后添加了新条目,以便清除仅通过更改Select = true属性而产生的viewstate问题.. :)