hello ..
I have listview in that had set paging ...but am unable do edit the record in that paging...
am sharing code..any help appreciated..
<div id="Main" style=" height:100%;width:100%">
<div id="large" style=" height:12%;width:100%">
<div style="float:left;height:100%;width:12%; border:1px solid black; text-align:center">
CustomerId
<asp:TextBox ID="txtCustomerId" runat="server" BorderColor="Black" BackColor="LightBlue" ReadOnly="true"></asp:TextBox>
</div>
<div style="float:left;height:100%;width:12%;border:1px solid black; text-align:center">
Name
<asp:TextBox ID="txtName" runat="server" BorderColor="Black" BackColor="LightBlue" onkeydown="return isAlpha(event.keyCode);" MaxLength="25" ontextchanged="txtName_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv2" runat="server" ErrorMessage="" SetFocusOnError="true" ControlToValidate="txtName" ValidationGroup="a"></asp:RequiredFieldValidator>
</div>
<div style="float:left;height:100%;width:12%;border:1px solid black;text-align:center">
Country
<asp:TextBox ID="txtCountry" runat="server" BorderColor="Black" BackColor="LightBlue" onkeydown="return isAlpha(event.keyCode);" MaxLength="20"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv3" runat="server" ErrorMessage="" SetFocusOnError="true" ControlToValidate="txtCountry" ValidationGroup="a"></asp:RequiredFieldValidator>
</div>
<div style="float:left;height:100%;width:5%;border:1px solid black;text-align:center">
<br />
<asp:Button ID="btnadd" runat="server" Text="Insert" BackColor="Aqua" onclick="btnadd_Click" ValidationGroup="a"/>
</div>
<div style="float:left;height:100%;width:5%;border:1px solid black;text-align:center">
<br />
<asp:Button ID="btncan" runat="server" Text="Clear" BackColor="Aqua" onclick="btncan_Click"/>
</div>
<div style="float:left;height:100%;width:54%"></div>
</div>
<asp:ListView ID="ListView1" runat="server" onitemcanceling="ListView1_ItemCanceling" onitemediting="ListView1_ItemEditing" onitemdatabound="ListView1_ItemDataBound"
onitemupdating="ListView1_ItemUpdating" onitemdeleting="ListView1_ItemDeleting" ItemPlaceholderID="itemplaceholder" GroupPlaceholderID="groupplaceholder" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="groupplaceholder" runat="server"/>
<tr>
<td colspan="3" >
<%-- This part used for paging --%>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="3" style="width:90%" onprerender="DataPager1_PreRender" >
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false"
ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
<%-- end --%>
</td>
</tr>
</div>
</LayoutTemplate>
<GroupTemplate>
<div>
<asp:PlaceHolder ID="itemplaceholder" runat="server"/>
</div>
</GroupTemplate>
<ItemTemplate>
<div id="large2" style="height:88%;width:100%">
<div style="float:left;height:100%;width:12%; border:1px solid black; text-align:center">
<asp:Label ID="lblcustomerid" runat="server" Text='<%#Eval("CustomerId")%>' Width="165px" Height="22px"></asp:Label>
</div>
<div style="float:left;height:100%;width:12%;border:1px solid black; text-align:center" >
<asp:Label ID="lblname" runat="server" Text='<%#Eval("Name")%>' Width="165px" Height="22px"></asp:Label>
</div>
<div style="float:left;height:100%;width:12%;border:1px solid black; text-align:center">
<asp:Label ID="lblcountry" runat="server" Text='<%#Eval("Country")%>' Width="165px" Height="22px"></asp:Label>
</div>
<div style="float:left;height:100%;width:5%;border:1px solid black; text-align:center">
<asp:Button ID="btnedit" runat="server" CommandName="Edit" Text="Edit" BackColor="Aqua"/>
</div>
<div style="float:left;height:100%;width:5%;border:1px solid black; text-align:center">
<asp:Button ID="btndelete" runat="server" CommandName="Delete" BackColor="Aqua" OnClientClick="return confirm('Are you sure you want to delete?');" Text="Delete"/>
</div>
<div style="float:left;height:100%;width:64%;">
<asp:Label runat="server" ID="lblstatus" Text='<%#Eval("Status")%>' Visible="false" Width="250px"></asp:Label>
</div>
</div>
</ItemTemplate>
</asp:ListView>
</div>
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add("CustomerId");
dt.Columns.Add("Name");
dt.Columns.Add("Country");
dt.Columns.Add("Status");
ViewState["Details"] = dt;
ListView1.DataSource = dt;
ListView1.DataBind();
txtCustomerId.Text = (dt.Rows.Count + 1).ToString();
txtName.Focus();
}
}
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(ListView1.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
DataTable dt = new DataTable();
dt = (DataTable)ViewState["Details"];
DataRow dr = dt.NewRow();
dr["CustomerId"] = (dt.Rows.Count + 1).ToString();
dr["Name"] = txtName.Text;
dr["Country"] = txtCountry.Text;
dr["Status"] = "Saved";
DataPager DataPager1 = ((DataPager)ListView1.Items[e.StartRowIndex].FindControl("DataPager1"));
int PageCount = (DataPager1.TotalRowCount * DataPager1.PageSize);
if (PageCount * DataPager1.PageSize != DataPager1.TotalRowCount)
{
PageCount = PageCount + 1;
}
//for (int i = 0; i < PageCount; i++)
//{
//}
//ddl.Items.FindByValue(CurrentPage.ToString()).Selected = true;
ListView1.DataSource = dt;
ListView1.DataBind();
}
protected void btnadd_Click(object sender, EventArgs e)
{
bool IsRecordExist = false;
DataTable dt = new DataTable();
dt = (DataTable)ViewState["Details"];
DataRow dr = dt.NewRow();
IsRecordExist = CheckRecorExist();
if (IsRecordExist == false)
{
dr["CustomerId"] = (dt.Rows.Count + 1).ToString();
dr["Name"] = txtName.Text;
dr["Country"] = txtCountry.Text;
dr["Status"] = "Saved";
dt.Rows.Add(dr);
ViewState["Details"] = ListView1.DataSource = dt;
ListView1.DataBind();
RefreshHeader();
}
else
{
dt = (DataTable)ViewState["Details"];
DataRow drUpdate = dt.Select("CustomerId=" + txtCustomerId.Text).FirstOrDefault();
drUpdate["Name"] = txtName.Text;
drUpdate["Country"] = txtCountry.Text;
drUpdate["Status"] = ((Label)ListView1.Items[0].FindControl("lblstatus")).Text = "Modified";
dr["Status"] = "Modified";
ViewState["Details"] = ListView1.DataSource = dt;
ListView1.DataBind();
RefreshHeader();
}
}
private bool CheckRecorExist()
{
bool IsExistRecord = false;
foreach (ListViewItem item in ListView1.Items)
{
if (((Label)ListView1.Items[item.DataItemIndex].FindControl("lblcustomerid")).Text == txtName.Text)
{
IsExistRecord = true;
break;
}
}
return IsExistRecord;
}
private void RefreshHeader()
{
DataTable dt = new DataTable();
dt = (DataTable)ViewState["Details"];
int Row = dt.Rows.Count + 1;
txtCustomerId.Text = Row.ToString();
txtName.Text = "";
txtCountry.Text = "";
txtName.Focus();
}
protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
DataTable dt = (DataTable)ViewState["Details"];
string EditNo = ((Label)ListView1.Items[e.NewEditIndex].FindControl("lblCustomerId")).Text;
((Label)ListView1.Items[e.NewEditIndex].FindControl("lblstatus")).Text = "Saved";
DataRow dr = dt.Select("CustomerId=" + EditNo).FirstOrDefault();
dr["Status"] = ((Label)ListView1.Items[0].FindControl("lblstatus")).Text;
dr["Status"] = "Saved";
ViewState["Details"] = ListView1.DataSource = dt;
txtCustomerId.Text = ((Label)ListView1.Items[e.NewEditIndex].FindControl("lblcustomerid")).Text;
txtName.Text = ((Label)ListView1.Items[e.NewEditIndex].FindControl("lblname")).Text;
txtCountry.Text = ((Label)ListView1.Items[e.NewEditIndex].FindControl("lblcountry")).Text;
txtName.Focus();
}
protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
ListView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["Details"];
ViewState["Details"] = dt;
ListView1.DataSource = dt;
ListView1.DataBind();
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
TextBox txtCustomerId = ((TextBox)ListView1.Items[e.ItemIndex].FindControl("txtCustomerId"));
TextBox txtName = ((TextBox)ListView1.Items[e.ItemIndex].FindControl("txtName"));
TextBox txtCountry = ((TextBox)ListView1.Items[e.ItemIndex].FindControl("txtCountry"));
ListView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["Details"];
ViewState["Details"] = dt;
ListView1.DataSource = dt;
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[e.ItemIndex]["CustomerId"] = txtCustomerId.Text;
dt.Rows[e.ItemIndex]["Name"] = txtName.Text;
dt.Rows[e.ItemIndex]["Country"] = txtCountry.Text;
}
ListView1.DataBind();
}
protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
string cusid = ((Label)ListView1.Items[e.ItemIndex].FindControl("lblcustomerid")).Text;
((Label)ListView1.Items[e.ItemIndex].FindControl("lblstatus")).Text = "Deleted";
DataTable dt = (DataTable)ViewState["Details"];
DataRow dr = dt.Select("CustomerId=" + cusid).FirstOrDefault();
dr["Status"] = ((Label)ListView1.Items[e.ItemIndex].FindControl("lblstatus")).Text;
dr["Status"] = "Deleted";
ViewState["Details"] = ListView1.DataSource = dt;
ListView1.DataBind();
RefreshHeader();
txtName.Focus();
}
protected void btncan_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtCountry.Text = "";
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DataTable dt = (DataTable)ViewState["Details"];
DataRow dr = ((System.Data.DataRowView)e.Item.DataItem).Row;
switch (dr["Status"].ToString())
{
case "Deleted":
{
btnadd.Visible = true;
btncan.Visible = true;
((Button)e.Item.FindControl("btnedit")).Visible = false;
((Button)e.Item.FindControl("btndelete")).Visible = false;
((Label)e.Item.FindControl("lblcustomerid")).BackColor = Color.Red;
((Label)e.Item.FindControl("lblname")).BackColor = Color.Red;
((Label)e.Item.FindControl("lblcountry")).BackColor = Color.Red;
break;
}
case "Saved":
{
btnadd.Visible = true;
btncan.Visible = true;
((Button)e.Item.FindControl("btnedit")).Visible = true;
((Button)e.Item.FindControl("btndelete")).Visible = true;
break;
}
case "Modified":
{
//e.Item.DataItem = Color.Empty;
btnadd.Visible = true;
btncan.Visible = true;
((Button)e.Item.FindControl("btnedit")).Visible = true;
((Button)e.Item.FindControl("btndelete")).Visible = true;
break;
}
default:
{
break;
}
}
}
}
protected void txtName_TextChanged(object sender, EventArgs e)
{
int x = 0;
DataTable dt = (DataTable)ViewState["Details"];
ViewState["Details"] = dt;
for (int i = 0; i < dt.Rows.Count; i++)
{
if ((txtName.Text == dt.Rows[i]["Name"].ToString() && dt.Rows[i]["Country"].ToString() != i + 1.ToString()))
{
x = 1;
}
if (x == 1)
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Name Already Exists');", true);
txtName.Text = string.Empty;
}
else
{
}
}
}
here am facing if i edit the recordit is showing index error..must be non negative...page size is 3...so in second page if i edit it is showing index error...any help please..