我有一个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
中的imagebutton时,如果所选项目被更改,则稍后点击imagebutton会触发radiobuttonlist
的selectedinexchanged
事件。
我不完全确定你想达到什么目的。但似乎你需要处理RadioButtonList
的OnSelectedIndexChanged
事件上的一些逻辑
首先在RadioButtonList
上设置AutoPostBack="true"
属性
然后在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
}
实际上,问题是我已经在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;
}
}
我清除了列表,然后添加了新的条目,这样它就清除了viewstate问题,这是由刚刚改变Select=true属性引起的。:)