Hello @everyone
I have an ASP.NET GridView inside an UpdatePanel. On the GridView i have a OnRowCommand which is used to delete or update rows from the Grid. This all works fine. The rows are being deleted, and the user sees them disappearing from his screen. All as it should. In other cases users use a OnRowCommand to update the column details on these rows. That also works fine.
But now, outside of that UpdatePanel there is a button to save the data along with GridView rows. And here comes the problem. After removing all the rows (records) from the GridView and then clicking on the save button, the code behind still sees GridView.Rows.Count > 0, instead of 0.
I'm sure there is a way to handle this so that i don't run into this error. But I am unable to figure it out. Please help me getting out of this.
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div id="divlineItems" runat="server" style="background-color: azure; text-align: center; border-top-color: dodgerblue;">
<h5>LINE ITEMS</h5>
</div>
<div style="overflow-x:scroll; overflow-y:hidden;">
<asp:GridView ID="gvLineItems" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Size="14.5px" CssClass="table table-responsive"
ForeColor="#333333" GridLines="None" Height="16px"
ShowFooter="True" ShowHeaderWhenEmpty="True" Width="100%" OnRowCommand="gvLineItems_RowCommand1" OnRowDataBound="gvLineItems_RowDataBound" OnDataBound="gvLineItems_DataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Sl No">
<ItemTemplate>
<asp:Label ID="lblSlno" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Services">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%#bind("Id") %>' CssClass="hidden"></asp:Label>
<asp:Label ID="lblServiceId" runat="server" Text='<%#bind("Service_Id") %>' CssClass="hidden"></asp:Label>
<asp:Label ID="lblServiceName" runat="server" Text='<%#bind("Service_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%#bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Scope">
<ItemTemplate>
<asp:Label ID="lblScope" runat="server" Text='<%#bind("ScopeofWork") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit">
<ItemTemplate>
<asp:Label ID="lblUnitId" runat="server" Text='<%#bind("Unit_Id") %>' CssClass="hidden"></asp:Label>
<asp:Label ID="lblUnitName" runat="server" Text='<%#bind("Unit_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%#bind("Qty") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%#bind("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount Cash">
<ItemTemplate>
<asp:Label ID="lblDiscountCash" runat="server" Text='<%#bind("Discount_Cash") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount %">
<ItemTemplate>
<asp:Label ID="lblDiscountPercentage" runat="server" Text='<%#bind("Discount_Percentage") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<%#bind("Total") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ControlStyle-CssClass="show">
<ItemTemplate>
<asp:Label ID="lblStatusId" runat="server" Text='<%#bind("statusId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CausesValidation="false" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="vEdit" ImageUrl="~/images/edit.png" Text="Edit" Width="20px" CssClass="btnAdd" />
<asp:ImageButton ID="imgDelete" runat="server" CausesValidation="false" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="vDelete" ImageUrl="~/images/delete1.png" Text="Delete" Width="20px" CssClass="btnAdd" />
<asp:ImageButton ID="imgClone" runat="server" CausesValidation="false" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="vClone" ImageUrl="~/images/Clone.png" Text="Clone" Width="20px" CssClass="btnAdd" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="center" />
<HeaderStyle BackColor="#2F4F4F" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
<asp:Label ID="lblLineItemTotal" Style="float: right;" Visible="false" runat="server" Text="0.00"></asp:Label><br />
<br />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
protected void gvLineItems_RowCommand1(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvLineItems.Rows[index];
Label rindex = (Label)row.FindControl("lblSlno");
Session["rindexSlno"] = rindex.Text;
Label lineItemId = (Label)row.FindControl("lblId");
//Session["lineItemId"] = lineItemId.Text;
Label serviceId = (Label)row.FindControl("lblServiceId");
ddlServices.SelectedValue = serviceId.Text;
Label description = (Label)row.FindControl("lblDescription");
Session["Description"] = description.Text;
txtDescription.Text = description.Text;
Label scopeofWork = (Label)row.FindControl("lblScope");
ftbScopeofWork.Text = scopeofWork.Text;
Label lblUnit = (Label)row.FindControl("lblUnitId");
ddlUnits.SelectedValue = lblUnit.Text;
Label qty = (Label)row.FindControl("lblQty");
txtQty.Text = qty.Text;
Label price = (Label)row.FindControl("lblPrice");
txtPrice.Text = price.Text;
Label discountCash = (Label)row.FindControl("lblDiscountCash");
txtDiscountCash.Text = discountCash.Text;
Label discountPercent = (Label)row.FindControl("lblDiscountPercentage");
txtDiscountPercentage.Text = discountPercent.Text;
if (e.CommandName == "vEdit") //For Edit
{
btnAdd.Text = "Update";
}
else if (e.CommandName == "vDelete")//For Delete
{
removeLineItemsRow(index);
refreshGridLineItems();
txtSplDiscount_TextChanged(this, null);
if (((DataTable)ViewState["dtable_LineItems"]).Rows.Count > 0)
btnSave.Visible = true;
else
btnSave.Visible = false;
}
else if (e.CommandName == "vClone")
{
btnAdd.Text = "Clone";
}
}
//Removing and refreshing gridview
private void refreshGridLineItems()
{
try
{
gvLineItems.DataSource = ((DataTable)ViewState["dtable_LineItems"]);
gvLineItems.DataBind();
}
catch (Exception ex) { }
}
private void removeLineItemsRow(int i_qnRowIndex)
{
try
{
if (i_qnRowIndex >= 0) //checking Row index greater than or equal to zero
{
((DataTable)ViewState["dtable_LineItems"]).Rows[i_qnRowIndex]["statusId"] = 5;
}
DataTable dtable_lineitemDetails = ((DataTable)ViewState["dtable_LineItems"]);
}
catch (Exception ex) { }
}
//on Row databound
protected void gvLineItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblTot = e.Row.FindControl("lblTotal") as Label;
Label lblStatusId = e.Row.FindControl("lblStatusId") as Label;
if (lblStatusId.Text != "5")
{
tot = tot + Convert.ToDecimal(lblTot.Text);
}
else if (lblStatusId.Text == "5")
e.Row.Visible = false;
lblLineItemTotal.Text = "Total= " + tot.ToString();
lblGrandTotal.Text = "Grand Total= " + (tot - (txtSplDiscount.Text != "" && Convert.ToDecimal(txtSplDiscount.Text) < tot ? Convert.ToDecimal(txtSplDiscount.Text) : 0)).ToString();
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[8].Text = "Total =";
e.Row.Cells[9].Text = tot.ToString(); ;
}
}