CheckedChanged在以编程方式添加时不会触发

时间:2013-12-16 17:46:30

标签: c# asp.net

我有下面的代码工作正常,除了checkchanged的单选按钮事件没有触发。我可以通过使用回发来实现这一点,但我想避免这种情况并启动我的JavaScript。任何见解都会很棒!

  protected void Page_Load(object sender, EventArgs e)
    {

        //Register JavaScript
        string javaName = "alert";
        Type javaType = this.GetType();

        ClientScriptManager csm = Page.ClientScript;
        if (!csm.IsStartupScriptRegistered(javaType, javaName))
        {
            StringBuilder buildScript = new StringBuilder();
            buildScript.Append("<script type=text/javascript>");
           // buildScript.Append("window.alert();");
            buildScript.Append("function myFunction(){");
            buildScript.Append("window.open('jstest.htm','','width=800,height=600')}");
            buildScript.Append("</script>");
            csm.RegisterStartupScript(javaType, javaName, buildScript.ToString());
        }






            //Loop through results
            for (int i = 0; i <= 10; i++)
            {
                //Create yes and no Radio Buttons for each question
                RadioButton radioyes = new RadioButton();
                radioyes.ID = "radioy_" + i.ToString();
                radioyes.GroupName = "Yquestion_" + i;
                radioyes.Text = "yes";
                //radioyes.CheckedChanged += radio_CheckedChanged;
                //radioyes.AutoPostBack = true;
                radioyes.Attributes.Add("checkedchanged", "myFunction()");

                RadioButton radiono = new RadioButton();
                radiono.ID = "radion_" + i.ToString();
                radiono.GroupName = "Yquestion_" + i;
                radiono.Text = "no";
                //radiono.CheckedChanged += radio_CheckedChanged;
                //radiono.AutoPostBack = true;



                //Add dynamic controls to placeholder
                QuestionZone.Controls.Add(new LiteralControl("This is a test " + i.ToString()));
                QuestionZone.Controls.Add(radioyes);
                QuestionZone.Controls.Add(radiono);
                QuestionZone.Controls.Add(new LiteralControl("<br><br>"));
            }

        }

2 个答案:

答案 0 :(得分:0)

我将属性更改为“onclick”而不是“checkchanged”,因为如果用户选择“是”,我只会触发一个事件,这似乎是一个可接受的解决方法。

答案 1 :(得分:0)

另外一个问题可能是:

  radioyes.Attributes.Add("checkedchanged", "myFunction()");

以上与以下交换:

 radioyes.Attributes.Add("onchange", "myFunction()");