using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MyCodeSnippet
{
public partial class SampleRC : System.Web.UI.Page
{
#region Private Members
private string qry = "select * from UserLogin";
private string SortField;
private string SortOrder;
private int PageNumber;
private int UserId;
private int pgCount;
public static int countPerRenderPage = 0;
public static int countTotalRec = 0;
static int PageSize;
static int TotalSize;
static int CurrentPage;
#endregion
#region connection string
static SqlConnection conn = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
#endregion
#region GetViewState/SetViewState
private void GetViewState()
{
this.UserId = Convert.ToInt32(ViewState["UserId"]);
}
private void SetViewState()
{
ViewState["UserId"] = this.UserId;
}
#endregion
#region Public Members
#region Init
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
}
#endregion
#endregion
//#region Init
//protected override void OnInit(EventArgs e)
//{
// base.OnInit(e);
// rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
//}
//#endregion
#region PageLoad
protected void Page_Load(object sender, EventArgs e)
{
divUpdate.Visible = false;
divpagination.Visible = true;
if (!IsPostBack)
{
BindDataByChar("");
// BindPaging();
//bindrepeater();
SetViewState();
}
else
{
GetViewState();
}
}
#endregion
#region DeleteOperaion
protected void Button_Click(Object sender, CommandEventArgs e)
{
//foreach(RepeaterItem item in rptUser.Items)
//{
// CheckBox chk = (CheckBox)item.FindControl("chkbox");
// if (chk.Checked)
// {
UserId = Convert.ToInt32(e.CommandName);
string qry = "Delete from UserLogin where UserId=" + UserId;
SqlCommand cmd = new SqlCommand(qry, conn);
cmd.CommandType = CommandType.Text;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.ExecuteNonQuery();
conn.Close();
//For Refresh the Repeter Control
BindDataByChar("");
// }
//}
}
#endregion
#region Insert
protected void ButtonInsert_Click(Object sender, CommandEventArgs e)
{
divUpdate.Visible = true;
this.UserId = -1;
SetViewState();
if (txt1.Text == "" && txt2.Text == "")
{
Response.Write("Please Enter the Data");
}
else
{
btnUpdate_Click(sender, e);
}
}
#endregion
#region Sorting
void SortData(string SortExpression)
{
if (ViewState["SortOrder"] == null)
{
ViewState["SortOrder"] = " ASC";
}
else if (ViewState["SortOrder"].ToString() == " ASC")
{
ViewState["SortOrder"] = " DESC";
}
else
{
ViewState["SortOrder"] = " ASC";
}
qry = qry + " ORDER BY " + SortExpression.ToString() + " " + ViewState["SortOrder"];
BindDataByChar("");
}
protected void lnkbtnUserIdSort_Click(object sender, EventArgs e)
{
SortField = "UserId";
SortData(SortField);
}
protected void lnkbtnPassSort_Click(object sender, EventArgs e)
{
SortField = "Password";
SortData(SortField);
}
#endregion
#region Pagination
// Bind repeater control..
public void rptBindGrid()
{
PageSize = int.Parse(DropDownListRowsPerPage.SelectedValue);
int count = 0;
string strCount = "select count(*) from UserLogin";
DataSet ds = new DataSet();
using (SqlConnection con = (new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123")))
{
using (SqlCommand cmd = new SqlCommand(strCount, con))
{
con.Open();
TotalSize = Convert.ToInt32(cmd.ExecuteScalar());
}
using (SqlDataAdapter da = new SqlDataAdapter("select * from UserLogin", con))
{
int StartRecord = ((CurrentPage) - 1) * ((PageSize));
da.Fill(ds, StartRecord, (PageSize), "UserLogin");
if ((count = ds.Tables[0].Rows.Count) > 0)
{
BuildPagers();
rptUser.DataSource = ds.Tables[0].DefaultView;
rptUser.DataBind();
}
}
}
}
private void BindPaging()
{
int i = 49;
if (countPerRenderPage != 0 && countTotalRec != 0)
{
int tillLoop = countTotalRec / countPerRenderPage;
for (int k = 0; k <= tillLoop; k++)
{
if (i < 58)
{
char c = (char)i;
LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + c;
lnk.CausesValidation = false;
lnk.CssClass = "white12boldtxt";
lnk.Style["padding-right"] = "10px";
lnk.ForeColor = System.Drawing.Color.Red;
lnk.Text = c.ToString();
lnk.CommandArgument = c.ToString();
lnk.Command += new CommandEventHandler(Page_List_lnk);
rptPages.Controls.Add(lnk);
//lblPaging.Controls.Add(lnk);
i++;
}
}
}
}
void BindDataByChar(string alphabet)
{
int count = 0;
string qry = string.Empty;
if (!String.IsNullOrEmpty(alphabet))
qry = "select * from UserLogin where UserLogin like '" + alphabet + "%'";
else
qry="select * from UserLogin" ;
string strCount = "select count(*) from UserLogin";
DataTable dt = new DataTable();
SqlConnection con=new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
DataSet ds = new DataSet();
using (con)
{
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (conn.State == ConnectionState.Closed)
{
con.Open();
}
da.Fill(dt);
if (dt.Rows.Count > 0)
{
TotalSize=dt.Rows.Count;
countTotalRec = Convert.ToInt32(TotalSize);
}
}
SqlConnection con1 = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
using (SqlDataAdapter da = new SqlDataAdapter(qry, con1))
{
CurrentPage = 1;
PageSize = Convert.ToInt32(DropDownListRowsPerPage.SelectedValue);
int StartRecord = ((CurrentPage) - 1) * ((PageSize));
da.Fill(ds, StartRecord, (PageSize), "UserLogin");
if ((countPerRenderPage = count = ds.Tables[0].Rows.Count) > 0)
{
BuildPagers();
//rptUser.DataSource = ds.Tables[0].DefaultView;
//rptUser.DataBind();
//Pagination
PagedDataSource pgitems = new PagedDataSource();
DataView dv = new DataView(dt);
pgitems.DataSource = dv;
pgitems.AllowPaging = true;
pgitems.PageSize = int.Parse(DropDownListRowsPerPage.SelectedValue);
pgitems.CurrentPageIndex = PageNumber;
//Total No of pages
//int j = 49;
//LinkButton lnk =FindControl("btnPage") as LinkButton;
//if (countPerRenderPage != 0 && countTotalRec != 0)
//{
// int tillLoop = countTotalRec / countPerRenderPage;
// for (int k = 0; k <= tillLoop; k++)
// {
// if (j < 58)
// {
// rptPages.Controls.Add(lnk):
// }
// }
//}
if (pgitems.PageCount > 1)
{
rptPages.Visible = true;
ArrayList pages = new ArrayList();
int i = 1;
for (; i < pgitems.PageCount; i++)
{
if (i < 10)
{
pages.Add((i).ToString());
rptPages.DataSource = pages;
rptPages.DataBind();
}
}
}
else
rptPages.Visible = false;
rptUser.DataSource = pgitems;
rptUser.DataBind();
}
}
}
void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
BindDataByChar("");
}
private void BuildPagers()
{
if ((((CurrentPage)) - 1) > 0)
{
prev.Visible = true;
first.Visible = true;
}
else
{
prev.Visible = false;
first.Visible = false;
}
if ((CurrentPage) * (PageSize) > (TotalSize))
{
next.Visible = false;
last.Visible = false;
}
else
{
next.Visible = true;
last.Visible = true;
}
}
protected void next_Click(object sender, EventArgs e)
{
Page_List(sender, e);
}
protected void prev_Click(object sender, EventArgs e)
{
Page_List(sender, e);
}
protected void last_Click(object sender, EventArgs e)
{
Page_List(sender, e);
}
protected void first_Click(object sender, EventArgs e)
{
Page_List(sender, e);
}
public void Page_List(object sender, EventArgs e)
{
if (((LinkButton)sender).ID == "prev")
{
if ((CurrentPage) >0)
{
if ((((CurrentPage)) - 1) > 0)
{
CurrentPage = ((CurrentPage) - 1);
}
}
}
else if (((LinkButton)sender).ID == "next")
{
if ((CurrentPage)>0)
{
if ((CurrentPage) * (PageSize) < (TotalSize))
{
CurrentPage = (CurrentPage + 1);
}
}
}
else if (((LinkButton)sender).ID == "last")
{
if ((CurrentPage)>0)
{
if ((CurrentPage) * (PageSize) < (TotalSize))
{
CurrentPage = (((TotalSize) / (PageSize)) );
}
}
}
else if (((LinkButton)sender).ID == "first")
{
if ((CurrentPage) == 0)
CurrentPage = 1;
if ((((CurrentPage)) - 1) > 0)
CurrentPage= 1;
}
// Now bind data
rptBindGrid();
}
public void Page_List_lnk(object sender, EventArgs e)
{
string[] charval = { "lnk" };
string[] strlId = (((LinkButton)sender).ID).Split(charval, StringSplitOptions.RemoveEmptyEntries);
string xfdsf = (((LinkButton)sender).ID).Remove(0, 3);
if (!String.IsNullOrEmpty(xfdsf))
rptBindGrid_k(xfdsf);
}
public void rptBindGrid_k(string val)
{
CurrentPage = int.Parse(val);
int StartRecord = 0;
int count = 0;
string strCount = "select count(*) from UserLogin";
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
using (con)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(strCount, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (conn.State == ConnectionState.Closed)
{
con.Open();
}
da.Fill(dt);
if (dt.Rows.Count > 0)
{
TotalSize=dt.Rows.Count;
}
}
SqlConnection con1 = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
using (SqlDataAdapter da = new SqlDataAdapter("select * from UserLogin", con1))
{
if ((CurrentPage) >0)
{
StartRecord = ((CurrentPage) - 1) * ((PageSize));
da.Fill(ds, StartRecord, (PageSize), "UserLogin");
}
else
{
StartRecord = 0;
da.Fill(ds, StartRecord, (TotalSize), "UserLogin");
}
if ((count = ds.Tables[0].Rows.Count) > 0)
{
if(StartRecord != 0)
BuildPagers();
rptUser.DataSource = ds.Tables[0].DefaultView;
rptUser.DataBind();
}
}
}
protected void DropDownListRowsPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
//PageNumber = int.Parse(DropDownListRowsPerPage.SelectedValue);
//ViewState["PageNumber"] = PageNumber;
//bindrepeater();
BindDataByChar("");
}
#endregion
#region serach
protected void btnserach_Click(object sender, EventArgs e)
{
if (txtsUserId.Text == "")
{
Response.Write("Please enter the data");
}
else
{
qry = qry + " where UserId=" + txtsUserId.Text;
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(qry, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
da.Fill(dt);
if (dt.Rows.Count > 0)
{
rptUser.DataSource = dt;
rptUser.DataBind();
TotalSize = dt.Rows.Count;
countTotalRec = Convert.ToInt32(TotalSize);
}
}
}
#endregion
#region Select
protected void ButtonSelect_Click(Object sender, CommandEventArgs e)
{
divUpdate.Visible = true;
this.UserId = Convert.ToInt32(e.CommandName);
SetViewState();
string qry = "select * from UserLogin where UserId=" + UserId;
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(qry, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txt1.Text = dt.Rows[0]["UserId"].ToString();
txt2.Text = dt.Rows[0]["Password"].ToString();
}
}
#endregion
#region UpDate
protected void btnUpdate_Click(object sender, EventArgs e)
{
divUpdate.Visible = true;
GetViewState();
string qry1;
if (this.UserId == -1)
{
qry1 = "insert into UserLogin values(" + Convert.ToInt32(txt1.Text) + "," + "'"+ txt2.Text + "'" + ")";
}
else
{
qry1 = "update UserLogin set UserId=" + Convert.ToInt32(txt1.Text) + ",Password=" + "'" + txt2.Text + "'" + " where UserId=" + this.UserId;
}
SqlCommand cmd = new SqlCommand(qry1, conn);
cmd.CommandType = CommandType.Text;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.ExecuteNonQuery();
conn.Close();
//For Refresh the Repeter Control
//bindrepeater();
BindDataByChar("");
divUpdate.Visible = false;
}
#endregion
protected void rptUser_ItemCreated(object sender, RepeaterItemEventArgs e)
{
}
}
}
No comments:
Post a Comment