2014-01-16 283 views
0

我正在使用此代碼來檢查是否選定了單選按鈕列表,但是如果沒有檢查到任何內容,它將不返回false,並繼續到後面的類中的函數。單選按鈕列表是從代碼隱藏的數據綁定中生成的。檢查是否選擇了單選按鈕列表

<asp:LinkButton class="btn" id="linkDelete" runat="server" onclick="link_Delete" OnClientClick="checkform()">Delete Template</asp:LinkButton> 

<asp:radiobuttonlist id="optMessageType" runat="server" onselectedindexchanged="optMessageType_SelectedIndexChanged" RepeatDirection="Vertical" CssClass="none-table"> 
</asp:radiobuttonlist> 

function checkform() { 
       var radiolist = document.getElementById('<%= optMessageType.ClientID %>'); 
       var radio = radiolist.getElementsByTagName("input"); 
       if (radio.length > 0) { 
        for (var x = 0; x < radio.length; x++) { 
         if (radio[x].type === "radio" && radio[x].checked) { 
          //alert("Checking..."); 
          // alert("Selected item Value " + radio[x].value); 
          var r = confirm("Are you sure you want to delete this template?") 
          if (r == true) { 
           //alert("You pressed OK!") 
           return true; 
          } 
          else { 
           //alert("You pressed Cancel!"); 
           return; 
          } 
         } 
         else { 
          alert("Select a template to delete"); 
          return; 
         } 
        } 
       } 
       else { 
        alert("nope"); 
        return; 
       } 
      } 

代碼背後:

protected void link_Delete(object sender, System.EventArgs e) 
{ 
Label3.Text = "Deleted!"; //For Testing 
} 

回答

0

你應該寫

<asp:LinkButton class="btn" id="linkDelete" runat="server" onclick="link_Delete" OnClientClick="return checkform();">Delete Template</asp:LinkButton> 

我已經在OnClientClick屬性附加傷害funcion呼叫添加return

希望這可以解決我們的問題。

+0

沒有,沒有做到這一點。 – PeteTheGreek

相關問題