Reputation: 1
ASP. net data grid show empty rows without text randomly. Though is shows empty rows, on double clicking on empty row, the data from the row gets populated correctly to another text box.
Below is the UI code.
<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>
Attached is the screenshot how the results are populated in the datagrid.empty rows
Code behind
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():");
}
}
Also, this application is being used by many remote clients and it is hosted on IIS in a server.
Upvotes: 0
Views: 465
Reputation: 188
Make sure the global variable mLocationData is not being altered elsewhere in the code. It seems that either this variable, or Session["DataRecords"] is being reset elsewhere.
If the page loads correctly the first time the page loads, and the records disappear on subsequent postbacks, that points to the datasource being altered.
Upvotes: 1