2013-12-16 47 views
0

我有下面的代碼,它能正常工作,除了checkchanged的單選按鈕事件沒有觸發。我可以通過回發來實現這個工作,但我想避免這種情況,並啓動我的JavaScript。任何見解都會很棒!當編程式添加時CheckChanged沒有觸發

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>")); 
      } 

     } 
+0

你有checkchanged事件寄存器註釋掉? –

+0

是的,但如果您查看「是」按鈕的代碼,則可以看到我將該事件添加爲屬性。 checkchanged不起作用,但onclick做。我的猜測是checkchanged需要回傳來比較狀態。 –

回答

0

我改變了屬性「點擊」,而不是「checkchanged」因爲我只是一個事件,如果用戶選擇「是」,這似乎是一個可接受的解決方法開火。

0

那麼另外一個修補程序可能是:

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

掉上面以下內容:

radioyes.Attributes.Add("onchange", "myFunction()"); 
相關問題