【问题标题】:ASP .net datagrid sometimes show empty rows, without textASP .net 数据网格有时显示空行,没有文本
【发布时间】:2016-07-01 05:13:41
【问题描述】:

平均售价。网络数据网格随机显示没有文本的空行。虽然显示的是空行,但在双击空行时,该行中的数据会正确填充到另一个文本框中。

下面是 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>

附件是如何在数据网格中填充结果的屏幕截图。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 上。

【问题讨论】:

  • 向我们展示你的代码!
  • 我怀疑这是因为您使用了TemplateField。如果您不使用任何控件来绑定值,则无需使用它。相反,您可以使用 BoundField &lt;asp:BoundField DataField="YourDataField" HeaderText="Your Column Header" /&gt;

标签: c# asp.net binding datagrid


【解决方案1】:

确保全局变量 mLocationData 没有在代码的其他地方被更改。似乎此变量或 Session["DataRecords"] 正在其他地方重置。

如果页面在第一次加载时正确加载,并且记录在随后的回发中消失,则表明数据源正在更改。

【讨论】:

  • 我们在搜索另一个字符串时更改 mLocationData,并且数据网格会填充新的搜索结果。我们得到了正确的结果,有时对于新的搜索,数据网格会得到空行,如您在附加的屏幕截图中所见。
  • 如何填充新结果。看起来数据源正在填充空白记录,这说明存在行,但空白字段。
  • 数据库中的数据被写入分配给 mLocationData 的 XML。检查 fntLoadXML 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多