2015-02-24 77 views
0

我有一個單選按鈕列的gridview。我有一個JavaScript功能,只允許用戶點擊一個單選按鈕。你第一次點擊gridview沒有任何反應。每次點擊後,工作正常。爲什麼第一次點擊gridview中的單選按鈕什麼也不做?

更新: 如果我直接單擊單選按鈕,它確實工作的第一次,但如果我點擊該行,我必須單擊兩次。

<script type="text/javascript"> 
    function SelectSingleRadiobutton(rdbtnid) { 
     var rdBtn = document.getElementById(rdbtnid); 
     var rdBtnList = document.getElementsByTagName("rbTypeGroup"); 
     for (i = 0; i < rdBtnList.length; i++) { 
      if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id) { 
       rdBtnList[i].checked = false; 
      } 
     } 
    } 
</script> 



<asp:GridView ShowHeader="false" SkinID="noborder" runat="server" ID="gvType" AutoGenerateColumns="false" 
         OnRowDataBound="gvType_RowDataBound" ClientIdMode="Predictable"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:RadioButton ID="optValue" runat="server" Name="options" Style="cursor: default;" Tag="rbTypeGroup" OnClick="SelectSingleRadiobutton(this.id)"/> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Name" /> 
     <asp:BoundField DataField="Description" /> 
    </Columns> 
</asp:GridView> 

protected void gvType_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     HibConfig.IncidentType type = (HibConfig.IncidentType)e.Row.DataItem; 

     RadioButton rdo = ((RadioButton)e.Row.FindControl("optValue")); 
     rdo.Checked = incident.Type.Id == type.Id ? true : false; 
     e.Row.Attributes.Add("onclick", string.Format("document.getElementById('{0}').click();", rdo.ClientID)); 
     e.Row.Attributes.Add("dataId", type.Id.ToString()); 
    } 
} 

回答

0

網格視圖可能會在您的JS被初始化後被加載。我認爲你看到的是在初始頁面加載你的js沒有gridview的上下文。您最初單擊網格行會導致回發,然後加載JS文件。如果你正在使用Jquery,你可能想要將JS包裝在document.ready()$function() call中。另一種選擇是嘗試加載文檔底部的JS,以確保DOM已經呈現。 我有一些JS文件「打破」或與ASP元素的頁面鬆散上下文。回傳可能會導致一些問題與JS

相關問題