2013-07-25 76 views
0

我大致遵循this link的概念。模擬一個嵌套的GridView。 它看起來像這樣:GridView中的Javascript會導致其他事件觸發?

buttons to display detailsRow

基本上我創建的第二行是display:none,想使用JavaScript來切換它。

JavaScript和ASPX代碼:

<script type="text/javascript" language="JavaScript"> 
    function detailsToggle(Company_ID) { 
     try { 
      detail_row = document.getElementById('detailsRow_' + Company_ID); 
      parent_row = document.getElementById('parentRow_' + Company_ID); 
      img = parent_row.cells[0].firstChild; 

      if (detail_row.className !== 'hidden') { 
       detail_row.className = detail_row.className + ' hidden'; 
       img.src = '../Images/icons/+.gif'; 
      } 
      else { 
       detail_row.className = detail_row.className.replace(/\bhidden\b/, ''); 
       img.src = '../Images/icons/-.gif'; 
      } 
     } 
     catch(ex) { alert(ex); } 
    } 
</script> 
<style type="text/css"> 
    .hidden 
    { 
     display: none; 
    } 
</style> 
<asp:GridView ID="gvLegalEntityBrowser" DataKeyNames="Company_ID" AutoGenerateColumns="False" 
    runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" 
    OnRowDataBound="gvLegalEntityBrowser_RowDataBound" OnPageIndexChanging="pagingIndexChanged" 
    AllowPaging="True" PageSize="25" AllowSorting="false"> 
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <%--Placeholder --%> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="CompanyCode" HeaderText="CompanyCode" SortExpression="CompanyCode" /> 
     <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> 
    </Columns> 
    <EmptyDataTemplate>No data</EmptyDataTemplate> 
    <EditRowStyle BackColor="#999999" /> 
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
    <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
    <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
    <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
</asp:GridView> 

...我apsx.cs的RowDataBound方法:

//Idea from : http://www.codeproject.com/Articles/160773/Expandable-Rows-in-GridView 
protected void gvLegalEntityBrowser_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     GridViewRow gvRow = e.Row as GridViewRow; 
     Int64 iCompanyID = 0; //Get Company_ID for Legal Entity Row 
     iCompanyID = System.Convert.ToInt64(gvLegalEntityBrowser.DataKeys[gvRow.RowIndex]["Company_ID"]); 
     GridView gvLegalEntityRelation = new GridView(); 
     gvRow.ID = "parentRow_" + iCompanyID; 
     gvRow.ClientIDMode = ClientIDMode.Static; //Set HTML ID element = control.ID 

     //Add javascript toggle button to each row 
     System.Web.UI.WebControls.ImageButton toggleButton = new System.Web.UI.WebControls.ImageButton(); 
     toggleButton.ID = "btnToggle_" + iCompanyID; 
     toggleButton.ImageUrl = "../Images/icons/+.gif"; 
     toggleButton.OnClientClick = "detailsToggle(" + (iCompanyID) + ")"; 
     gvRow.Cells[0].Controls.Add(toggleButton); 
     toggleButton.Attributes.Add("onmouseover","this.style.cursor='hand'"); 
     toggleButton.Attributes.Add("onmouseout", "this.style.cursor='default'"); 

     using (dsLegalEntitiesTableAdapters.View_LegalEntityRelationCountTableAdapter daLERelCount = new dsLegalEntitiesTableAdapters.View_LegalEntityRelationCountTableAdapter()) 
     { 
      //Set Details Data Source 
      gvLegalEntityRelation.DataSource = daLERelCount.GetRelationsDataByCompanyID(iCompanyID); 
      gvLegalEntityRelation.AutoGenerateColumns = true; 

      GridViewRow detailsgvRow = new GridViewRow(gvRow.RowIndex + 1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); 
      detailsgvRow.CssClass = "hidden"; 
      detailsgvRow.ID = "detailsRow_" + iCompanyID; 
      detailsgvRow.ClientIDMode = ClientIDMode.Static; //Set HTML ID element = control.ID 
      TableCell cell = new TableCell(); 
      cell.ColumnSpan = 4; 
      cell.BorderStyle = BorderStyle.None; 
      cell.Controls.Add(gvLegalEntityRelation); 
      detailsgvRow.Cells.Add(cell); 
      ((GridView)sender).Controls[0].Controls.Add(detailsgvRow); 

      gvLegalEntityRelation.DataBind(); 
     } 
    } 
} 

JavaScript的正常工作,我甚至看到了正確的結果一個瞬間:

enter image description here

...但在價格調整匯率NT的GridView重建弄成這個樣子:

After javascript executes

問:有沒有人有一個想法是什麼可能會導致GridView的重建?

回答

0

所以......問題其實很基本。一位同事向我指出了這一點。

我用ImageButton展開了內部表格。 .NET圖像按鈕具有Click處理程序和ClientClick處理程序。點擊告訴在PostBack上做什麼,ClientClick傳遞給JavaScript。

顯然,即使我沒有爲Click定義目標函數,頁面仍然會執行Post-Back。因此,首先JavaScript的作品,然後頁面做了回傳,搞亂了頁面。

解決方案:交換ImageButton以獲得帶有click-event for javascript的簡單圖像。

相關問題