I have a OptionRadioButtonList
class extends RadioButtonList
web controls:
public class OptionRadioButtonList : RadioButtonList
{
public EventHandler SelectionChangeEventHandler;
public OptionRadioButtonList()
{
RepeatDirection = RepeatDirection.Vertical;
AutoPostBack = true;
Items.Add(new ListItem("YES", "YES"));
Items.Add(new ListItem("NO", "NO"));
CssClass = "rbList";
}
protected virtual void OnSelectionIndexChanged(object sender, EventArgs e)
{
if (SelectionChangeEventHandler != null)
{
SelectionChangeEventHandler(sender, e);
}
}
}
This class is used in a aspx.cs file like this:
protected void Page_Load(object sender, EventArgs e)
{
// more code here
OptionRadioButtonList rblist = new OptionRadioButtonList();
rblist.SelectionChangeEventHandler += new EventHandler(mainQuestion_rblistHandler);
LocalPlaceHolder.Controls.Add(rblist); // this is a placeholder in aspx file
// more code here
}
// RadioButtonList Event handler
protected void mainQuestion_rblistHandler(object sender, EventArgs e)
{
RadioButtonList currentRblist = (RadioButtonList)sender;
if (currentRblist.SelectedValue.Equals("YES"))
{
// do something
}
else
{
// do something else
}
}
So the UI part works fine, but when I change the selection of the yes no radiobuttonList
, the Page_Load
function gets called, but not mainQuestion_rblistHandler
can not be reached when I set a break points there. I am not sure what I did wrong, can anyone help me. Thank you in advance.
You never call OnSelectionIndexChanged
. Override the base OnSelectedIndexChanged
method and call OnSelectionIndexChanged
in there.
public class OptionRadioButtonList : RadioButtonList
{
public EventHandler SelectionChangeEventHandler;
public OptionRadioButtonList()
{
RepeatDirection = RepeatDirection.Vertical;
AutoPostBack = true;
Items.Add(new ListItem("YES", "YES"));
Items.Add(new ListItem("NO", "NO"));
CssClass = "rbList";
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
var handler = SelectionChangeEventHandler;
if (handler == null) return;
handler(this, e);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.