Thursday, May 14, 2009

How to use Details View

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 DV : 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
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
this.UserId = Convert.ToInt32(dvUser.Rows[0].Cells[1].Text);
SetViewState();


}
}

private void GetData()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM UserLogin", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

try
{
conn.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);

dvUser.DataSource = ds;
dvUser.DataBind();





conn.Close();
}
finally
{

}
}

protected void dvUser_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
TextBox UserId =dvUser.Rows[0].Cells[1].Controls[0] as TextBox;
TextBox Password = dvUser.Rows[1].Cells[1].Controls[0] as TextBox;

if (UserId == null) { return; }
if (Password == null) { return; }


SqlCommand cmd = new SqlCommand(
"insert into UserLogin values(" + Convert.ToInt32(UserId.Text) + "," + "'" + Password.Text + "'" + ")",
conn);


try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
dvUser.ChangeMode(DetailsViewMode.ReadOnly);


this.GetData();

}
finally
{

}
}

protected void dvUser_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
GetViewState();


TextBox UserId =dvUser.Rows[0].Cells[1].Controls[0] as TextBox;
TextBox Password = dvUser.Rows[1].Cells[1].Controls[0] as TextBox;

if (UserId == null) { return; }
if (Password == null) { return; }

SqlCommand cmd = new SqlCommand(
"update UserLogin set UserId=" + Convert.ToInt32(UserId.Text) + ",Password=" + "'" + Password.Text + "'" + " where UserId=" + this.UserId,
conn);


try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
dvUser.ChangeMode(DetailsViewMode.ReadOnly);

this.GetData();

}
finally
{
}

}

protected void dvUser_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
GetViewState();
SqlCommand cmd = new SqlCommand("DELETE FROM UserLogin WHERE UserId =" + this.UserId,
conn);


try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
dvUser.ChangeMode(DetailsViewMode.ReadOnly);
this.GetData();

}
finally
{

}
}

protected void dvUser_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
dvUser.ChangeMode(e.NewMode);
this.GetData();
}

protected void dvUser_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
dvUser.PageIndex = e.NewPageIndex;
this.GetData();

//e.Item.Cells[n].Text;
//int i = dvUser.PageIndex;
this.UserId= Convert.ToInt32(dvUser.Rows[0].Cells[1].Text);
SetViewState();


}

protected void dvUser_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
this.UserId = Convert.ToInt32(e.Keys["UserId"]);
SetViewState();
}

#region Pagination
private void InitialiseDetailsViewPagerRow(DetailsViewRow DetailsViewRow)
{

if (DetailsViewRow != 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 (dvUser.PageIndex == 0)
{

// Disable 'First' and 'Previous' Paging image buttons



ImageButton firstPageImageButton = DetailsViewRow.FindControl(

"FirstPageImageButton") as ImageButton;



ImageButton previousPageImageButton = DetailsViewRow.FindControl(

"PreviousPageImageButton") as ImageButton;



if (firstPageImageButton != null && previousPageImageButton != null)
{

firstPageImageButton.Enabled = false;



previousPageImageButton.Enabled = false;

}

}

else if ((dvUser.PageIndex + 1) == dvUser.PageCount)
{

// Disable 'Last' and 'Next' Paging image buttons



ImageButton lastPageImageButton = DetailsViewRow.FindControl(

"LastPageImageButton") as ImageButton;



ImageButton nextPageImageButton = DetailsViewRow.FindControl(

"NextPageImageButton") as ImageButton;



if (lastPageImageButton != null && nextPageImageButton != null)
{

lastPageImageButton.Enabled = false;



nextPageImageButton.Enabled = false;

}

}

else
{

// Enable the Paging image buttons



ImageButton firstPageImageButton = DetailsViewRow.FindControl(

"FirstPageImageButton") as ImageButton;



ImageButton previousPageImageButton = DetailsViewRow.FindControl(

"PreviousPageImageButton") as ImageButton;



ImageButton lastPageImageButton = DetailsViewRow.FindControl(

"LastPageImageButton") as ImageButton;



ImageButton nextPageImageButton = DetailsViewRow.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 = DetailsViewRow.FindControl(

"PageNumberDropDownList") as DropDownList;



Label pageCountLabel = DetailsViewRow.FindControl(

"PageCountLabel") as Label;



if (pageNumberDropDownList != null && pageCountLabel != null)
{

for (int i = 0; i < dvUser.PageCount; i++)
{

int page = i + 1;


ListItem li = new ListItem(page.ToString(), i.ToString());
pageNumberDropDownList.Items.Add(li);


}



pageNumberDropDownList.SelectedIndex = dvUser.PageIndex;



pageCountLabel.Text = dvUser.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 (dvUser.DataItemCount > 0)
{

if (pageNumberDropDownList.SelectedIndex < dvUser.PageCount ||

pageNumberDropDownList.SelectedIndex >= 0)
{

dvUser.PageIndex = pageNumberDropDownList.SelectedIndex;
this.GetData();
this.UserId = Convert.ToInt32(dvUser.Rows[0].Cells[1].Text);
SetViewState();
}

}

}

}
#endregion
protected void dvUser_DataBound(object sender, EventArgs e)
{

InitialiseDetailsViewPagerRow(dvUser.TopPagerRow);
InitialiseDetailsViewPagerRow(dvUser.BottomPagerRow);
}
}
}

No comments: