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 FV : System.Web.UI.Page
{
#region connection string
private int UserId;
//static string txtpass;
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
private void SetData()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM UserLogin", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
try
{
conn.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
frmview.DataSource = ds;
frmview.DataBind();
conn.Close();
}
finally
{
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.SetData();
Label lblUID = frmview.FindControl("lblUserId") as Label;
this.UserId = Convert.ToInt32(lblUID.Text);
SetViewState();
}
}
protected void frmview_ItemInserting(object sender, FormViewInsertEventArgs e)
{
TextBox txtUserId = frmview.FindControl("txtInsUserId") as TextBox;
TextBox txtPass = frmview.FindControl("txtInsPass") as TextBox;
if (txtUserId == null) { return; }
if (txtPass == null) { return; }
SqlCommand cmd = new SqlCommand(
"insert into UserLogin values(" + Convert.ToInt32(txtUserId.Text) + "," + "'" + txtPass.Text + "'" + ")",
conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
frmview.ChangeMode(FormViewMode.ReadOnly);
this.SetData();
}
finally
{
}
}
protected void frmview_ModeChanging1(object sender, FormViewModeEventArgs e)
{
frmview.ChangeMode(e.NewMode);
this.SetData();
}
protected void frmview_ItemUpdating1(object sender, FormViewUpdateEventArgs e)
{
TextBox txtUId = frmview.FindControl("txtUpUserId") as TextBox;
TextBox txtPassword = frmview.FindControl("txtUpPass") as TextBox;
GetViewState();
if (txtUId == null) { return; }
if (txtPassword == null) { return; }
SqlCommand cmd = new SqlCommand(
"update UserLogin set UserId=" + Convert.ToInt32(txtUId.Text) + ",Password=" + "'" + txtPassword.Text + "'" + " where UserId=" + this.UserId,
conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
frmview.ChangeMode(FormViewMode.ReadOnly);
this.SetData();
}
finally
{
}
//string qry = "update UserLogin set UserId=" + Convert.ToInt32(txtUserId.Text) + ",Password=" + "'" + txtpassword.Text + "'" + " where UserId=" + this.UserId;
}
protected void frmview_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
GetViewState();
SqlCommand cmd = new SqlCommand("DELETE FROM UserLogin WHERE UserId ="+this.UserId,
conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
frmview.ChangeMode(FormViewMode.ReadOnly);
this.SetData();
}
finally
{
}
}
#region Pagination
private void InitialiseFormViewPagerRow(FormViewRow formViewRow)
{
if (formViewRow != null)
{
// Check the page index so that we can :
// 1. Disable the 'First' and 'Previous' paging image buttons if paging index is at 0
// 2. Disable the 'Last' and 'Next' paging image buttons if paging index is at the end
// 3. Enable all image buttons if the conditions of 1 and 2 are not satisfied
if (frmview.PageIndex == 0)
{
// Disable 'First' and 'Previous' Paging image buttons
ImageButton firstPageImageButton = formViewRow.FindControl(
"FirstPageImageButton") as ImageButton;
ImageButton previousPageImageButton = formViewRow.FindControl(
"PreviousPageImageButton") as ImageButton;
if (firstPageImageButton != null && previousPageImageButton != null)
{
firstPageImageButton.Enabled = false;
previousPageImageButton.Enabled = false;
}
}
else if ((frmview.PageIndex + 1) == frmview.PageCount)
{
// Disable 'Last' and 'Next' Paging image buttons
ImageButton lastPageImageButton = formViewRow.FindControl(
"LastPageImageButton") as ImageButton;
ImageButton nextPageImageButton = formViewRow.FindControl(
"NextPageImageButton") as ImageButton;
if (lastPageImageButton != null && nextPageImageButton != null)
{
lastPageImageButton.Enabled = false;
nextPageImageButton.Enabled = false;
}
}
else
{
// Enable the Paging image buttons
ImageButton firstPageImageButton = formViewRow.FindControl(
"FirstPageImageButton") as ImageButton;
ImageButton previousPageImageButton = formViewRow.FindControl(
"PreviousPageImageButton") as ImageButton;
ImageButton lastPageImageButton = formViewRow.FindControl(
"LastPageImageButton") as ImageButton;
ImageButton nextPageImageButton = formViewRow.FindControl(
"NextPageImageButton") as ImageButton;
if (firstPageImageButton != null && lastPageImageButton != null &&
previousPageImageButton != null && nextPageImageButton != null)
{
firstPageImageButton.Enabled = true;
lastPageImageButton.Enabled = true;
nextPageImageButton.Enabled = true;
previousPageImageButton.Enabled = true;
}
}
// Get the DropDownList found as part of the Pager Row.
// One can then initialise the DropDownList to contain
// the appropriate page settings. Eg. Page Number and
// number of Pages
DropDownList pageNumberDropDownList = formViewRow.FindControl(
"PageNumberDropDownList") as DropDownList;
Label pageCountLabel = formViewRow.FindControl(
"PageCountLabel") as Label;
if (pageNumberDropDownList != null && pageCountLabel != null)
{
for (int i = 0; i < frmview.PageCount; i++)
{
int page = i + 1;
ListItem li = new ListItem(page.ToString(), i.ToString());
pageNumberDropDownList.Items.Add(li);
}
pageNumberDropDownList.SelectedIndex = frmview.PageIndex;
pageCountLabel.Text = frmview.PageCount.ToString();
}
}
}
///
/// Handle the SelectedIndexChanged event for the Page Number
/// DropDownList. This allows one to select a page index via a
/// DropDownList.
///
protected void PageNumberDropDownList_OnSelectedIndexChanged(
object sender, EventArgs e)
{
DropDownList pageNumberDropDownList = sender as DropDownList;
if (pageNumberDropDownList != null)
{
if (frmview.DataItemCount > 0)
{
if (pageNumberDropDownList.SelectedIndex < frmview.PageCount ||
pageNumberDropDownList.SelectedIndex >= 0)
{
frmview.PageIndex = pageNumberDropDownList.SelectedIndex;
this.SetData();
Label lblUID = frmview.FindControl("lblUserId") as Label;
this.UserId = Convert.ToInt32(lblUID.Text);
SetViewState();
}
}
}
}
#endregion
protected void frmview_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
frmview.PageIndex = e.NewPageIndex;
this.SetData();
Label lblUID = frmview.FindControl("lblUserId") as Label;
this.UserId = Convert.ToInt32(lblUID.Text);
SetViewState();
}
protected void frmview_DataBound1(object sender, EventArgs e)
{
InitialiseFormViewPagerRow(frmview.TopPagerRow);
InitialiseFormViewPagerRow(frmview.BottomPagerRow);
}
}
}
No comments:
Post a Comment