Custom paging with gridview and linqdatasource - c#

I have a problem which in theory should be very simple but i spent 3 hours now not being able to figure it out. I have a gridview and lingdatasource and i try to implement custom paging. No matter what i do i seem to be stuck with a pagesize of 25! Code below
Thanks!
aspx page
<asp:GridView ID="gvCustomerList" SkinID="StandardGridView" AutoGenerateColumns="false" DataSourceID="LqCustomerSource"
AllowPaging="false" AllowSorting="false" PageSize="50" runat="server" OnSorting="gvCustomerList_OnSorting"
OnRowCommand="gvSupplierCustomerList_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Customer Id" HeaderStyle-HorizontalAlign="Left" SortExpression="CustomerId">
<ItemTemplate>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" HeaderStyle-HorizontalAlign="Left" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).FirstName %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" HeaderStyle-HorizontalAlign="Left" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).LastName %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink ID="lnkDetails" Text="Details" NavigateUrl='<%# "~/BackOffice/Customers/CustomerDetails.aspx?CustomerId=" + ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ask" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink ID="lnkAsk" Text="Ask" NavigateUrl='<%# "~/BackOffice/Customers/Ask.aspx?CustomerId=" + ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:LinqDataSource ID="LqCustomerSource" AutoPage="false" runat="server"
OnSelecting="LqCustomerSource_Selecting" AutoSort="False">
</asp:LinqDataSource>
code behind
protected void LqCustomerSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
try
{
CustomerDataManager manager = new CustomerDataManager();
string searchName = "";// txtCustomerName.Text.Trim();
int count = 0;
List<Customer> customerList = manager.GetSelectedCustomers(gvCustomerList.PageIndex * 50, 50, searchName, SortExpression, SortDirectionForCustomers, out count);
//List<CustomerInsuranceInfo> infoList = manager.GetCustomerInsuranceDataFromList(customerList);
lblRows.Text = count.ToString();
e.Arguments.StartRowIndex = 0;
e.Arguments.MaximumRows = 50;
e.Arguments.TotalRowCount = count;
if (customerList == null)
e.Cancel = true;
else
e.Result = customerList;
}
catch (Exception ex)
{
ApplicationLogBO.Log(ex);
// Alert(WebsiteUtil.StandardErrorMessage);
e.Cancel = true;
}
}

Ok, it looks like you need to set it in the code behind in LqCustomerSource_Selecting
gvCustomerList.PageSize = 50

Related

Cannot retrieve value on rowdeleting in GridView

I have a problem for retrieving values for deleting records.
I'm using OnRowDeleting in a gridview with templatefields, and I can't retrive record selected value for deleting, this is my grid:
<asp:GridView ID="gvw_Cli_Emp_EmpData" runat="server" AutoGenerateColumns="false"
CssClass="mGrid" PagerStyle-CssClass="pgr" Width="50%" AutoGenerateDeleteButton="True"
AlternatingRowStyle-CssClass="alt" Font-Size="Small" OnRowDeleting="Borrando">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="#" onclick="window.open('Clientes_Empleados_Detalle.aspx?cliCod= <%#Eval("ClienteCodigo1").ToString()
+ "&EmpNom=" + Eval("Empleado1").ToString()
+ "&EmpCod=" + Eval("IdCliEmp").ToString()
+ "&idDepart=" + Eval("IdDepartamento").ToString()
+ "&Depart=" + Eval("Departamento1").ToString()
+ "&EmpNiv=" + Eval("NivelAcceso1").ToString()
%> ','PrintMe','height=500px,width=800px,scrollbars=1');">Editar</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CLIENTE">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_CliCod" runat="server" Text='<%# Eval("ClienteCodigo1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CODIGO">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpId" runat="server" Text='<%# Eval("IdCliEmp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLEADO">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpNom" runat="server" Text='<%# Eval("Empleado1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID DEP">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_DepId" runat="server" Text='<%# Eval("IdDepartamento") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPARTAMENTO">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpDep" runat="server" Text='<%# Eval("Departamento1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NIVEL">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpNiv" runat="server" Text='<%# Eval("NivelAcceso1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And for delete:
protected void Borrando(object sender, GridViewDeleteEventArgs e)
{
string cell = gvw_Cli_Emp_EmpData.Rows[e.RowIndex].Cells[0].Text; //this retuns me ""
int EmpCliCod = Convert.ToInt32(cell);
DialogResult dialogResult = MessageBox.Show(new Form { TopMost = true }, "Delete?", "Confirma", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
cliEmpBL.clientesEmpleados_SupEmpleado(EmpCliCod); //here execute deletion record from my datalayer
Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertScript", "alert('Deleted!');", true);
}
else if (dialogResult == DialogResult.No)
{ }
}
So, as you can see returns me "".
I have tried resolve this, using:
System.Windows.Forms.Label EmpId = e.Item.FindControl("lbl_CliEmp_CliCod") as System.Windows.Forms.Label;
string val = EmpId.Text;
But same result, any idea?
please, I hope anyone can help me.
best regards
Try implementing below example, you will get id for selected row
<asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("ClienteCodigo1")%>'>Delete</asp:LinkButton>
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int ID = Convert.ToInt32(e.CommandArgument);
//now perform the delete operation using ID value
}
}
Thanks Patrik your solution helped me.
<asp:GridView ID="gvw_CliEmp_EmpData" runat="server" AutoGenerateColumns="false"
CssClass="mGrid" PagerStyle-CssClass="pgr" Width="50%"
AlternatingRowStyle-CssClass="alt" Font-Size="Small"
OnRowCommand="gvw_CliEmp_EmpData_RowCommand" OnRowDeleting="gvw_CliEmp_EmpData_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="#" onclick="window.open('Clientes_Empleados_Detalle.aspx?cliCod= <%#Eval("ClienteCodigo1").ToString()
+ "&EmpNom=" + Eval("Empleado1").ToString()
+ "&EmpCod=" + Eval("IdCliEmp").ToString()
+ "&idDepart=" + Eval("IdDepartamento").ToString()
+ "&Depart=" + Eval("Departamento1").ToString()
+ "&EmpNiv=" + Eval("NivelAcceso1").ToString()
%> ','PrintMe','height=500px,width=800px,scrollbars=1');">Editar</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CLIENTE">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_CliCod" runat="server" Text='<%# Eval("ClienteCodigo1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CODIGO">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpId" runat="server" Text='<%# Eval("IdCliEmp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLEADO">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpNom" runat="server" Text='<%# Eval("Empleado1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID DEP">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_DepId" runat="server" Text='<%# Eval("IdDepartamento") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPARTAMENTO">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpDep" runat="server" Text='<%# Eval("Departamento1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NIVEL">
<ItemTemplate>
<asp:Label ID="lbl_CliEmp_EmpNiv" runat="server" Text='<%# Eval("NivelAcceso1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete"
CommandArgument='<%#Eval("IdCliEmp")%>'>Eliminar
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and code behind:
#region ==== BORRAR REGISTROS DEL INGRESO ====
protected void gvw_CliEmp_EmpData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int EmpCliCod = Convert.ToInt32(e.CommandArgument);
DialogResult dialogResult = MessageBox.Show(new Form { TopMost = true }, "Eliminar?", "Confirmar", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
cliEmpBL.clientesEmpleados_SupEmpleado(EmpCliCod); //eliminar desde DL
Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertScript", "alert('Eliminado!');", true);
}
else if (dialogResult == DialogResult.No)
{ }
gvw_CliEmp_EmpData.DataSource = null;
gvw_CliEmp_EmpData.DataBind();
gvw_CliEmp_EmpData.DataSource = cliEmpBL.clientes_Empleados_cons_EmpxCliente(lbl_CliEmp_CliCod.Text);
gvw_CliEmp_EmpData.DataBind();
}
}
protected void gvw_CliEmp_EmpData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//this is because grid fired event RowDeleting which wasn't handled
}
#endregion

How can I dynamically edit gridview data?

How to edit and delete fields in a gridview, not connected with database and gridview is being populated with data entered by a user in the textboxes? Also, how can I remove the row using delete button in the gridview?
this is my grid view in ASP page :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="mGrid"
ShowFooter="True" ShowHeaderWhenEmpty="True" Width="70%" OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField HeaderText="Item Code">
<EditItemTemplate>
<asp:TextBox ID="txtItemCode" runat="server" Text='<%# Bind("ItemCode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ItemCode" runat="server" Text='<%# Bind("ItemCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<EditItemTemplate>
<asp:TextBox ID="txtProduct" runat="server" Text='<%# Bind("Product") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Product" runat="server" Text='<%# Bind("Product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="txtUnitPrice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="UnitPrice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Batch">
<EditItemTemplate>
<asp:TextBox ID="txtBatch" runat="server" Text='<%# Bind("Batch") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Batch" runat="server" Text='<%# Bind("Batch") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField FooterText="Total" HeaderText="Expiary Date">
<EditItemTemplate>
<asp:TextBox ID="txtExpiaryDate" runat="server" Text='<%# Bind("ExpiaryDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ExpiaryDate" runat="server" Text='<%# Bind("ExpiaryDate") %>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="GetQuantity" runat="server" Text='<%# GetQuantity() %>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="GetTotal" runat="server" Text='<%# GetTotal() %>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/delete.png" EditImageUrl="~/Images/edit.png"
HeaderText="EDIT" ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="Gainsboro" ForeColor="Black" />
<PagerStyle CssClass="pgr" HorizontalAlign="Right" />
</asp:GridView>
I'm using this code to add data to the grid.
protected void btnAdd_Click(object sender, EventArgs e)
{
if (ViewState["CurrentData"] != null)
{
var dt = (DataTable)ViewState["CurrentData"];
var count = dt.Rows.Count;
BindGrid(count);
}
else
{
BindGrid(1);
}
Clear();
}
private void BindGrid(int rowcount)
{
var DataTbl = new DataTable();
DataRow DataRw;
DataTbl.Columns.Add("ItemCode", typeof(String));
DataTbl.Columns.Add("Product", typeof(String));
DataTbl.Columns.Add("UnitPrice", typeof(String));
DataTbl.Columns.Add("Batch", typeof(String));
DataTbl.Columns.Add("ExpiaryDate", typeof(String));
DataTbl.Columns.Add("Quantity", typeof(double));
DataTbl.Columns.Add("Total", typeof(double));
if (ViewState["CurrentData"] != null)
{
for (var i = 0; i < rowcount + 1; i++)
{
DataTbl = (DataTable)ViewState["CurrentData"];
if (DataTbl.Rows.Count > 0)
{
DataRw = DataTbl.NewRow();
DataRw[0] = DataTbl.Rows[0][0].ToString();
}
}
DataRw = DataTbl.NewRow();
DataRw[0] = txtItemCode.Text;
DataRw[1] = ddlProduct.SelectedItem;
DataRw[2] = txtUnitPrice.Text;
DataRw[3] = txtBatch.Text;
DataRw[4] = txtExpiaryDate.Text;
DataRw[5] = txtQty.Text;
DataRw[6] = (Convert.ToInt32(Convert.ToDouble(
txtUnitPrice.Text) * Convert.ToDouble(
txtQty.Text)) - ((Convert.ToDouble(
txtUnitPrice.Text) * Convert.ToDouble(
txtQty.Text))) * Convert.ToDouble(
txtProductDiscount.Text) / 100).ToString();
DataTbl.Rows.Add(DataRw);
}
else
{
DataRw = DataTbl.NewRow();
DataRw[0] = txtItemCode.Text;
DataRw[1] = ddlProduct.SelectedItem;
DataRw[2] = txtUnitPrice.Text;
DataRw[3] = txtBatch.Text;
DataRw[4] = txtExpiaryDate.Text;
DataRw[5] = txtQty.Text;
DataRw[6] = (Convert.ToInt32(Convert.ToDouble(
txtUnitPrice.Text) * Convert.ToDouble(
txtQty.Text)) - ((Convert.ToDouble(
txtUnitPrice.Text) * Convert.ToDouble(
txtQty.Text))) * Convert.ToDouble(
txtProductDiscount.Text) / 100).ToString();
DataTbl.Rows.Add(DataRw);
}
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
GridView1.DataSource = DataTbl;
GridView1.DataBind();
}
ViewState["CurrentData"] = DataTbl;
}
First of all your code and query is not clear.
if i am understanding it right then the problem in your code is that data is not deleting or updating.
as i saw in your code, you are holding data in view state. please recheck your code at the stage of GridView1_RowUpdating() and GridView1_RowCommand() (hoping you are using this for delete) events. after deleting/updating the data you should update the view state also.

Cannot display Sum of column in Gridview

I am not able to display the Value in a lable in footer. Below is the code
Aspx
<asp:GridView ID="gvallAccount" runat="server" AutoGenerateColumns="False" Width="589px"
OnRowDataBound="gvallAccount_RowDataBound" OnRowCommand="gvallAccount_RowCommand">
<Columns>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="lblAccountID" runat="server" Text='<%# Bind("ProductAccountID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" Visible="True">
<ItemTemplate>
<asp:Label ID="lblProductName" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Number" Visible="True">
<ItemTemplate>
<asp:LinkButton ID="lnkAccCode" runat="server" Text='<%# Bind("ProductAccountCode") %>'
OnClick="lnkAccCode_Click" CommandName="gotoLink"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Value(KES)" Visible="True">
<ItemTemplate>
<asp:Label ID="lblBalance" runat="server" Text='<%# Bind("BalanceToDate") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# Code
protected void gvallAccount_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "BalanceToDate"));
grdTotal = grdTotal + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
lblTotal.Text = grdTotal.ToString();
}
}
catch (Exception Ex)
{
logger.Error("gvallAccount_RowDataBound : " + Ex.Message);
}
}
You need to have ShowFooter="True" attribute in the GridView tag.
<asp:GridView ID="gvallAccount" runat="server" AutoGenerateColumns="False" Width="589px"
OnRowDataBound="gvallAccount_RowDataBound" OnRowCommand="gvallAccount_RowCommand" ShowFooter="True">

Gridview not showing records?

I just attached linq data to gridview it is not showing row ?
DataDataContext db = new DataDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindData();
}
private void BindData()
{
var source = from n in db.Names
select n;
gridSample.DataSource = source.ToList<Name>();
gridSample.DataBind();'
}
<asp:UpdatePanel ID="panelGrid" runat="server">
<ContentTemplate>
<asp:GridView runat="server" ID="gridSample" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="First Name">
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%Bind("FirstName") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblFirstName" Text='<%Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%Bind("LastName") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblLastName" Text='<%Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Birth">
<EditItemTemplate>
<asp:TextBox ID="txtDOB" runat="server" Text='<%Bind("DOB") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblDOB" Text='<%Eval("DOB") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Try this
List<Name> source = (from n in db.Names select n).ToList();
gridSample.DataSource = source;
gridSample.DataBind();'
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' />
try this:
private void BindData()
{
var source = from n in db.Names
select n;
gridSample.DataSource = source;
gridSample.DataBind();'
}
Regards
Set the property
AutoPostBack = True
for the GridView. Since it is inside the UpdatePanel.

ASP.NET Gridview RowUpdating event not firing

HTML Page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:GridView ID="Grd_Threshold" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="Both" AutoGenerateColumns="false" EmptyDataText="No Records." PageSize="8"
AllowPaging="true" OnPageIndexChanging="Grd_Threshold_PageIndexChanging" OnRowCommand="Grd_Threshold_RowCommand"
DataKeyNames="ID" AutoGenerateEditButton="true" OnRowCancelingEdit="Grd_Threshold_RowCancelingEdit" OnRowEditing="Grd_Threshold_RowEditing"
OnRowUpdating="Grd_Threshold_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="No.">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CLNAME") + ", " + Eval("CFNAME") +" " +Eval("CMNAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Doctor" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DOCTORNAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Name" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("INAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Form" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("IFORM")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Strength" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ISTRG")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order Date" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ORDERDATE")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" ItemStyle-Width="80px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("PRICE")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" ItemStyle-Width="80px">
<EditItemTemplate>
<asp:TextBox ID="Txt_Qty" runat="server" Width="50px" Text='<%# Eval("QTY")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("QTY")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("USERID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
CS Code:
protected void Page_Load(object sender, EventArgs e)
{
try
{
Title = "Dashboard";
if (!Master.Page.IsPostBackEventControlRegistered)
{
BindGrid();
}
}
catch (Exception ex)
{
Common.WriteLog(ex);
}
}
private void BindGrid()
{
try
{
if (SessionUtility.DashboardRecords == null || SessionUtility.DashboardRecords.Rows.Count == 0)
{
string WebServiceUrl = SessionUtility.WebServiceUrl + "/GetAllOrders/";
JsonArrayCollection ReqResponse = Common.GetWebServiceResponse_MultipleValues(WebServiceUrl, "GET");
DataTable Patients = Common.ConvertJsonArrayObjectCollectionToDataTable(ReqResponse);
SessionUtility.DashboardRecords = Patients;
}
if (SessionUtility.UserRight != "1")
{
//Grd_Threshold.Columns[0].Visible = false;
Grd_Threshold.AutoGenerateEditButton = false;
}
else if (SessionUtility.UserRight == "1")
{
Grd_Threshold.AutoGenerateEditButton = true;
}
Grd_Threshold.DataSource = SessionUtility.DashboardRecords;
Grd_Threshold.DataBind();
UpdatePanel1.Update();
}
catch (Exception ex)
{
Common.WriteLog(ex);
}
}
Hi All,
In above written code only EDIT event is being fired & that is only once after that UPDATE,CANCEL no event is being fired. What am i doing wrong?All I want to do is change the GridRow Color on the basis of the value entered in QTY field.
I figured out the problem. The event was not firing because of the same IDs that i had given to the Label in each row. Hope this helpful to someone.
Not sure about your use of IsPostBackEventControlRegistered - I havent seen it used before but it seems that probably you are wiping out your update event by DataBind() every time in Page_Load().
Page_Load is fired every time the page loads which is then changing what the page is doing by resetting the grid.
Try wrapping the control in:
if(!IsPostBack)
{
// bind
}
so that it only does this on the first page load.

Categories

Resources