RadioButton的CheckedChanged在刷新后消失

时间:2016-10-10 07:37:44

标签: javascript c# html asp.net

我有一个ASP.Net页面,在运行时有很多动态生成的控件。每个控件都绑定到“更改事件”。除了带有CheckedChanged事件的RadioButtons,一切都很好。

我第一次访问该页面一切正常。但是第二次RadioButtons失去了CheckedChanged事件。

首页访问时生成的HTML代码:

<input id="ctl00_masterContentPlaceHolder_question467b446b749c4252a4df1839ae81ee7fYesRadioButton" type="radio" name="ctl00$masterContentPlaceHolder$question467b446b749c4252a4df1839ae81ee7fRadioButtonGroup" value="question467b446b749c4252a4df1839ae81ee7fYesRadioButton" onclick="javascript:setTimeout('__doPostBack(\'ctl00$masterContentPlaceHolder$question467b446b749c4252a4df1839ae81ee7fYesRadioButton\',\'\')', 0)" tabindex="1">

<input id="ctl00_masterContentPlaceHolder_question467b446b749c4252a4df1839ae81ee7fNoRadioButton" type="radio" name="ctl00$masterContentPlaceHolder$question467b446b749c4252a4df1839ae81ee7fRadioButtonGroup" value="question467b446b749c4252a4df1839ae81ee7fNoRadioButton" onclick="javascript:setTimeout('__doPostBack(\'ctl00$masterContentPlaceHolder$question467b446b749c4252a4df1839ae81ee7fNoRadioButton\',\'\')', 0)">

第二次访问后(来自第二个RadioButton的onclick事件丢失):

<input id="ctl00_masterContentPlaceHolder_question467b446b749c4252a4df1839ae81ee7fYesRadioButton" type="radio" name="ctl00$masterContentPlaceHolder$question467b446b749c4252a4df1839ae81ee7fRadioButtonGroup" value="question467b446b749c4252a4df1839ae81ee7fYesRadioButton" onclick="javascript:setTimeout('__doPostBack(\'ctl00$masterContentPlaceHolder$question467b446b749c4252a4df1839ae81ee7fYesRadioButton\',\'\')', 0)" tabindex="1">

<input id="ctl00_masterContentPlaceHolder_question467b446b749c4252a4df1839ae81ee7fNoRadioButton" type="radio" name="ctl00$masterContentPlaceHolder$question467b446b749c4252a4df1839ae81ee7fRadioButtonGroup" value="question467b446b749c4252a4df1839ae81ee7fNoRadioButton" checked="checked">

修改

Page_Load 事件中添加了控件:

System.Collections.Generic.List<CPM.Arda.Questionnaire.Base.Objects.QuestionRenderResult<System.Web.UI.Control>> questionRenderResults = _questionnaireParser.Render(questionnaireGroup, false);
            foreach (CPM.Arda.Questionnaire.Base.Objects.QuestionRenderResult<System.Web.UI.Control> questionRenderResult in questionRenderResults)
                questionnaireGroupControlsPanel.Controls.Add(questionRenderResult.QuestionControl);

Render()方法中的代码:

    System.Web.UI.WebControls.RadioButton yesRadioButton = new System.Web.UI.WebControls.RadioButton();
    yesRadioButton.ID = "question" + questionnaireGroupQuestion.Id.ToString().Replace("-", "") + "YesRadioButton";
    yesRadioButton.Text = "Ja";
    yesRadioButton.GroupName = "question" + questionnaireGroupQuestion.Id.ToString().Replace("-", "") + "RadioButtonGroup";
    yesRadioButton.TabIndex = tabIndex;
    yesRadioButton.Enabled = !_readOnly;
    yesRadioButton.AutoPostBack = true;
    yesRadioButton.CheckedChanged += inputControl_ValueChanged;

    System.Web.UI.WebControls.RadioButton noRadioButton = new System.Web.UI.WebControls.RadioButton();
    noRadioButton.ID = "question" + questionnaireGroupQuestion.Id.ToString().Replace("-", "") + "NoRadioButton";
    noRadioButton.Text = "Nein";
    noRadioButton.GroupName = "question" + questionnaireGroupQuestion.Id.ToString().Replace("-", "") + "RadioButtonGroup";
    noRadioButton.Style.Add("margin-left", "10px");
    noRadioButton.Enabled = !_readOnly;
    noRadioButton.AutoPostBack = true;
    noRadioButton.CheckedChanged += inputControl_ValueChanged;

任何人都能解释一下吗?

1 个答案:

答案 0 :(得分:0)

找到解决我问题的方法。

似乎是AjaxToolKit中的一个错误。看看这个:

ASP.NET Radio button checked changed event not firing for first radio button

为我工作。