2016-07-01 32 views
0

ASP。淨數據網格隨機顯示沒有文本的空行。雖然顯示的是空行,但雙擊空行時,行中的數據會正確填充到另一個文本框中。ASP .net datagrid有時顯示空行,沒有文字

以下是UI代碼。

<asp:Panel ID="pnlDataControl" runat="server" Enabled="true" EnableViewState="true">   
     <div class="grdCtl"> 
     <asp:GridView ID="grdlDataControl" runat="server" ShowFooter="True" 
      AutoGenerateColumns="False" GridLines="None" AllowPaging="True" AllowSorting="True" 
      Width="100%" BorderColor="Red" BorderWidth="0px" BorderStyle="Solid" EmptyDataText="" 
      onpageindexchanging="grdlDataControl_PageIndexChanging" 
      ondatabound="grdlDataControl_DataBound" 
      onrowdatabound="grdlDataControl_RowDataBound" 
      onselectedindexchanged="grdlDataControl_SelectedIndexChanged" 
      onrowcommand="grdlDataControl_RowCommand" style="margin-bottom: 18px;"> 
    <SelectedRowStyle BackColor="Gray" Font-Bold="true" /> 
     <Columns>   
      <asp:TemplateField HeaderStyle-CssClass="grdHead" HeaderStyle-Wrap="false" ItemStyle-CssClass="" HeaderStyle-Width="10px" ItemStyle-Width="10px"> 
       <ItemTemplate></ItemTemplate> 
       <HeaderTemplate></HeaderTemplate> 
       <HeaderStyle CssClass="grdHead" Width="10px" Wrap="False" /> 
       <ItemStyle Width="10px" /> 
      </asp:TemplateField> 
     </Columns> 

     <EmptyDataRowStyle HorizontalAlign="Center" /> 
     <FooterStyle /> 

     </asp:GridView> 
     </div>  
    </asp:Panel> 
    <br/> 
     </ContentTemplate> 
    </asp:UpdatePanel> 

附上截圖,結果是如何在DataGrid中填充。 empty rows背後

private void fntLoadData() 
       { 
        try 
        { 
         mLocationData = (IEnumerable<clsLocationData>)Session["DataRecords"]; 
         if (mLocationData != null) mDataTotalRecords = mLocationData.Count(); 

         this.grdlDataControl.DataSource = mLocationData; 
         this.grdlDataControl.DataBind(); 
         if(clsApplication.LDSCount>=clsApplication.cParamMaximumResult) 
         { 
          Label ctlLabel = (Label)grdlDataControl.BottomPagerRow.FindControl("lblAlert"); 
          ctlLabel.Text = fntGetLanguageValue("MoreRecords", "MESSAGES", "OR").Replace("1%", clsApplication.cParamMaximumResult.ToString()); 
         } 
        } 
        catch (Exception ex) 
        { 
         fntCatchError(ex,"fntLoadData():"); 
        } 
       } 

    protected void grdlDataControl_DataBound(object sender, EventArgs e) 
      { 
       GridViewRow gvrPager = grdlDataControl.BottomPagerRow; 
       if (gvrPager == null) return; 

       DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages"); 
       Label txtPages = (Label)gvrPager.Cells[0].FindControl("txtPages"); 
       Label lblPages = (Label)gvrPager.Cells[0].FindControl("lblPages3"); 

       // populate dropdownlist 
       if (ddlPages != null) 
       { 
        for (int i = 0; i < grdlDataControl.PageCount; i++) 
        { 
         int intPageNumber = i + 1; 
         ListItem lstItem = new ListItem(intPageNumber.ToString()); 
         if (i == grdlDataControl.PageIndex) lstItem.Selected = true; 
         ddlPages.Items.Add(lstItem); 
        } 
       } 

       if (txtPages != null) txtPages.Text = grdlDataControl.PageCount.ToString(); 
       if (lblPages != null) lblPages.Text = "(" + mDataTotalRecords + " " + fntGetLanguageValue("lblItems", "LABEL", "OR") + ")"; 

       // Check for next, prev images status 
       ImageButton btnFrst = (ImageButton)gvrPager.Cells[0].FindControl("btnPagerFrst"); 
       ImageButton btnPrev = (ImageButton)gvrPager.Cells[0].FindControl("btnPagerPrev"); 
       ImageButton btnNext = (ImageButton)gvrPager.Cells[0].FindControl("btnPagerNext"); 
       ImageButton btnLast = (ImageButton)gvrPager.Cells[0].FindControl("btnPagerLast"); 

       if (grdlDataControl.PageIndex == 0) 
       { 
        btnPrev.Enabled = false; //btnPrev.ImageUrl = "./Images/icon_prev_i.gif"; 
        btnFrst.Enabled = false; //btnFrst.ImageUrl = "./Images/icon_frst_i.gif"; 
       } 
       else if (grdlDataControl.PageIndex + 1 == grdlDataControl.PageCount) 
       { 
        btnLast.Enabled = false; //btnLast.ImageUrl = "./Images/icon_last_i.gif"; 
        btnNext.Enabled = false; //btnNext.ImageUrl = "./Images/icon_next_i.gif"; 
       } 
       else 
       { 
        btnLast.Enabled = true; //btnLast.ImageUrl = "./Images/icon_last.gif"; 
        btnNext.Enabled = true; //btnNext.ImageUrl = "./Images/icon_Next.gif"; 
        btnPrev.Enabled = true; //btnPrev.ImageUrl = "./Images/icon_Prev.gif"; 
        btnFrst.Enabled = true; //btnFrst.ImageUrl = "./Images/icon_frst.gif"; 
       } 
      } 

      protected void grdlDataControl_RowDataBound(object sender, GridViewRowEventArgs e) 
      { 
       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        if (clsApplication.cDataDisplayTypIcon == "true") 
        { 
         string sSymbol = ((clsLocationData)(e.Row.DataItem)).sTYPE; 
         System.Web.UI.WebControls.Image ImgIcon = new System.Web.UI.WebControls.Image(); 
         string iconPath = "./Images/@" + sSymbol + ".gif"; 
         string iconFile = "./Images/@NOTYPE.gif"; 

         if (fntIsValidImage(Server.MapPath(iconPath))) iconFile = iconPath; 
         ImgIcon.ImageUrl = iconFile; 
         ImgIcon.Width = 16; ImgIcon.Height = 16; ImgIcon.ImageAlign = ImageAlign.AbsMiddle; ImgIcon.CssClass = "grdLocationIcon"; 

         e.Row.Cells[0].Controls.Add(ImgIcon); 
        } 

        if (mDateRelevanceIdx > 0) 
        { 
         e.Row.Cells[mDateRelevanceIdx].Controls.Clear(); 
         Double dRelevance = ((clsLocationData)(e.Row.DataItem)).sRELEVANCE; 

         if (clsApplication.cDataDisplayRelvBar == "true") 
         { 
          string sBarCSS = "grdRelevenceBar1"; 
          if (dRelevance > 10) sBarCSS = "grdRelevenceBar2"; 
          if (dRelevance > 20) sBarCSS = "grdRelevenceBar3"; 
          if (dRelevance > 30) sBarCSS = "grdRelevenceBar4"; 

          int iBarWidth = 40; 
          if (dRelevance > 10) iBarWidth = 30; 
          if (dRelevance > 20) iBarWidth = 20; 
          if (dRelevance > 30) iBarWidth = 10; 

          Label LabelBar = new Label(); 
          //LabelBar.Width = new Unit((100 - dRelevance)/2); 
          LabelBar.Width = new Unit(iBarWidth); 
          LabelBar.CssClass = sBarCSS; 
          e.Row.Cells[mDateRelevanceIdx].Controls.Add(LabelBar); 
         } 
         if (clsApplication.cDataDisplayRelvTxt == "true") 
         { 
          Label LabelTxt = new Label(); 
          LabelTxt.Text = (100 - dRelevance).ToString(); 
          e.Row.Cells[mDateRelevanceIdx].Controls.Add(LabelTxt); 
         } 
        } 
       } 

       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        e.Row.Attributes.Add("onmouseover", "this.className='grdRowHigh';"); 
        e.Row.Attributes.Add("onmouseout", "this.className='grdRowNorm';"); 

        string sArgsData1 = "Select$" + e.Row.RowIndex.ToString(); 
        e.Row.Cells[0].Attributes.Add("onclick", Page.ClientScript.GetPostBackClientHyperlink(grdlDataControl, sArgsData1)); 

        string sArgsData2 = "DblSelect$" + e.Row.RowIndex.ToString(); 
        e.Row.Attributes.Add("Ondblclick", Page.ClientScript.GetPostBackClientHyperlink(grdlDataControl, sArgsData2)); 

       } 
      } 



     protected void grdlDataControl_PageIndexChanging(object sender, GridViewPageEventArgs e) 
       { 
        grdlDataControl.PageIndex = e.NewPageIndex; 
        grdlDataControl.SelectedIndex = -1; 

        fntLoadDataControl(); 
       } 


       protected void grdlDataControl_RowCommand(object sender, GridViewCommandEventArgs e) 
       { 
        if (e.CommandName == "Select" || e.CommandName == "DblSelect") 
        { 
         GridView grdData = (GridView)sender; 

         int iRowIndex = int.Parse(e.CommandArgument.ToString()); 
         int iPageIndex = grdData.PageIndex; 
         int iRowNumber = clsApplication.cDataRecordPerPage * iPageIndex + iRowIndex; 

         fntLoadDataControl(); 
         if (e.CommandName == "DblSelect") 
         { 
          fntGetRowDataItem(iRowNumber, "dclick"); 

         } 
         if (e.CommandName == "Select") fntGetRowDataItem(iRowNumber, "sclick"); 
        } 
        this.grdlDataControl.DataBind(); 
       } 

       void btnCtl_Click(object sender, EventArgs e) 
       { 
        Button btnCtl = (Button)sender; 
        mDataRecordSortFld = btnCtl.CommandName; 
        mDataRecordSortOrd = btnCtl.CommandArgument; 
        mDataRecordSortOrd = fntGetSortOrder(mDataRecordSortFld, mDataRecordSortOrd); 

        grdlDataControl.SelectedIndex = -1; 

        fntSortDataControl(); 
        fntLoadDataControl(); 
       } 

    protected void btnSearch_Click(object sender, EventArgs e) 
      { 

       grdlDataControl.PageIndex = 0; 
       grdlDataControl.SelectedIndex = -1; 

       Session["DataRecords"] = null; 

       fntGetLocationData(false, null); 
     } 

    private void fntGetLocationData(bool isLocVer, EMEALVINTERFACE.LocationData LocData) 
      { 
    XmlDocument xmlLocation = oLV.GetLocationData(isLocVer,LocData); 
    fntLoadXML(xmlLocation); 
    } 

    private void fntLoadXML(XmlDocument xmlDocument) 
      { 
       try 
       { 
        if (xmlDocument != null && xmlDocument.InnerXml != "") 
        { 
         XDocument xDoc = XDocument.Parse(xmlDocument.InnerXml); 
         // IEnumerable<clsLocationData> vLocations = null; 
          var vLocations = from location in xDoc.Descendants("LOCATION") 
             select new clsLocationData 
             { 
    //copy locations 
             }; 

         mLocationData = (IEnumerable<clsLocationData>)vLocations.ToList(); 

         mLocationData = fntSortData(mLocationData, mDataRecordSortFld, mDataRecordSortOrd); 


         Session["DataRecords"] = mLocationData.ToList(); 
        } 
        else 
        { 
         Session["DataRecords"] = null; 
        } 
       } 
       catch (Exception ex) 
       { 
        fntCatchError(ex,"fntLoadXML():"); 
       } 
      } 

而且

代碼,這個應用程序正在使用的許多遠程客戶端,它是在一個服務器託管在IIS。

+0

向我們展示您的代碼! –

+0

我懷疑它是因爲你使用'TemplateField'而發生的。 如果您不使用任何控件來綁定值,則不需要使用它。 您可以使用'BoundField'' ' –

回答

1

確保全局變量mLocationData在代碼中的其他地方未被更改。看起來,這個變量或Session [「DataRecords」]正在其他地方重置。

如果第一次加載頁面時頁面加載正確,並且記錄在隨後的回發中消失,則指向正在更改的數據源。

+0

我們正在更改mLocationData搜索另一個字符串,並且數據網格會填充新的搜索結果。我們會正確地得到結果,有時對於新搜索,datagrid會獲得空行,如您在附加屏幕截圖中所看到的那樣。 – user6536462

+0

如何填充新結果。它看起來像數據源正在填充空白記錄,它佔了行的存在,但空白字段。 – Michael

+0

來自數據庫的數據被寫入正在分配給mLocationData的XML。檢查fntLoadXML方法。 – user6536462