Hello,
I'm using a listview with paging. in listview row, i have button to save that row details.
this is working fine on the 1st page. but in the next consecuitve pages, the datakeyname return null.
here is my code below.
Aspx:
<asp:ListView runat="server" ID="LviewDisplayProducts" ItemPlaceholderID="item" OnPagePropertiesChanging="OnPagePropertiesChanging"
DataKeyNames="ItemCode"
onitemdatabound="LviewDisplayProducts_ItemDataBound">
<LayoutTemplate>
<table id="product-list">
<tr>
<td><b>Description</b></td>
<td colspan="3"><b>Details</b></td>
</tr>
<tr id="item" runat="server"></tr>
<tr>
<td colspan = "3">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="LviewDisplayProducts" PageSize="5">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="true" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="true" ShowPreviousPageButton = "false" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<EmptyDataTemplate>
<div id="no-items">No results found</div>
</EmptyDataTemplate>
<ItemTemplate>
<table style="background-color:#7F7C77" width="100%" cellpadding="10" cellspacing="10">
<tr style="outline:thin solid">
<td>
<table>
<tr><td>
<asp:ImageButton runat="server" ID="imgDesign" ImageUrl="~/ipadcss/set-demo.jpg" Width="100%" OnClientClick="return LoadDiv(this.src);" />
</td></tr>
</table>
</td>
<td>
<table>
<tr>
<td>
Sets: <%# Eval("Karat")%>
</td>
</tr>
<tr>
<td>
Set: <%# Eval("ItemGroupDesc")%>
</td>
</tr>
<tr>
<td>
Weight: <%# Eval("AvgWt")%>
</td>
</tr>
<tr>
<td>
Code: <asp:Label Text='<%# Eval("ItemCode")%>' runat="server" ID="lblItemCode"></asp:Label>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
Branch: <%# Eval("Location")%>
</td>
</tr>
<tr>
<td>
LSC: <%# Eval("Price")%>
</td>
</tr>
<tr>
<td>
Price: <%# Eval("Price")%>
</td>
</tr>
<tr>
<td>
Peice: <%# Eval("Pcs")%>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
<asp:GridView runat="server" ID="grvSalesManStock" Width="100%" AutoGenerateColumns="false" GridLines="None">
<Columns>
<asp:BoundField HeaderText="" DataField="description" />
<asp:BoundField HeaderText="" DataField="Pcs" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</td>
<td>
<asp:TextBox runat="server" ID="txtDetails" TextMode="MultiLine"></asp:TextBox>
</td>
<td>
<asp:ImageButton runat="server" ID="btnCheckOut" OnClick="btnCheckOut_Click" ImageUrl="~/ipadcss/add_to_cart.png" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
Paging:
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(LviewDisplayProducts.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.GetFilteredProducts();
}
Add Button code:
if (Session["EmpId"] != null)
{
int EmpId = int.Parse(Session["EmpId"].ToString());
ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
string ItemCode = LviewDisplayProducts.DataKeys[item.DataItemIndex].Value.ToString();
TextBox txtDetails = (TextBox)item.FindControl("txtDetails");
string EmpRemarks = txtDetails.Text.Trim();
using (SqlConnection con = new SqlConnection(Constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Cart_Items(emp_id, product_id,emp_remarks)VALUES(@EmpId,@ProductId,@EmpRemarks)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@EmpId", EmpId);
cmd.Parameters.AddWithValue("@ProductId", ItemCode);
cmd.Parameters.AddWithValue("EmpRemarks", EmpRemarks);
cmd.Connection = con;
sda.SelectCommand = cmd;
try
{
con.Open();
cmd.ExecuteNonQuery();
lblAlert.Visible = true;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "HideLabel();", true);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}
}
}
}
else
Response.Redirect("login.aspx");
}
this line throws error in next pages:
string ItemCode = LviewDisplayProducts.DataKeys[item.DataItemIndex].Value.ToString();