Category Archives: DetailsView Example

How to Change DetailsView Pager Text in ASP.Net C# ?

In this asp.net post we learn how to change DetailsView control Pager Text in ASP.Net C#.

Design the ASP.Net web form with one Button Control and one DetailsView control in ASP.Net.

First set AllowPaging =True of DetailsView control.

Change DetailsView Pager Text in ASP.Net C#

Change DetailsView Pager Text in ASP.Net C#

Now, Set DetailsView control Mode property = NextPreviousFirstLast.

Change DetailsView Pager Text in ASP.Net C#

Change DetailsView Pager Text in ASP.Net C#

Change DetailsView Pager Text in ASP.Net C#

Change DetailsView Pager Text in ASP.Net C#

Write the Below server code in Button Click event.

 protected void btnSave_Click(object sender, EventArgs e)
{
DetailsView1.PagerSettings.FirstPageText = “First”;
DetailsView1.PagerSettings.NextPageText = “Next”;
DetailsView1.PagerSettings.PreviousPageText = “Previous”;
DetailsView1.PagerSettings.LastPageText = “Last”;
}

 

Change DetailsView Pager Text in ASP.Net C#

Change DetailsView Pager Text in ASP.Net C#

Change DetailsView Pager Text in ASP.Net C#

Change DetailsView Pager Text in ASP.Net C#

I hope this DetailsView control Change Pager Text in ASP.Net will help you..

How to Edit, Update, Delete Record in DetailsView in ASP.Net

Here, we will learn how to edit, update, delete record to DetailsView in ASP.Net C#.

How to Display record in DetailsView in ASP.Net ?

How to do paging in DetailsView control in ASP.Net ?

First Bind Data to DetailsView control from sql server..

For Editing set AutogenerateEditButton = True at property of DetailsView control.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

Now, we have Edit button on DetailsView control in ASP.Net.

When we click Edit Button that time we need to change mode of DetailsView control for Editing Records.

The CurrentMode of DetailsView contorl is ReadOnly we change the mode to Edit for Editing Record..

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

write below code at ModeChanging Event of DetailsView control for go to Edit mode.

We handle Edit Click Event and Cancel Click Event here…

 protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
BindDetailsView();
}
else if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
BindDetailsView();
}
}

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

Now click Edit button to enable Edit mode of Details View control….

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

If you click Cancel Button then DetailsView back to original  Readonly mode.

Here, you can change value of Name and City click update button to update Record to database.

For update we take here ItemUpdating Event…

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

write below code at ItemUpdating Event of DetailsView Control in ASP.Net.

protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int idd =Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text);
TextBox name = DetailsView1.Rows[1].Cells[1].Controls[0] as TextBox;
TextBox city = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox;

SqlDataAdapter SQLAdapter = new SqlDataAdapter(“update UserMst set name='” + name.Text + “‘, city='” + city.Text + “‘ where id=”+idd+””, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
BindDetailsView();
}

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

The updated result is :

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.

Now, we will learn to Delete Record from DetailsView in ASP.Net..

Here, set AutogenerateDeleteButton = True of DetailsView control.

detailview13

write code at ItemDeleting Event of DetialsView control for Delete record from DetialsView.

DetailsView in ASP.Net

DetailsView in ASP.Net

write below code at ItemDeleting event for Delete Record form Database….

 protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
int idd = Convert.ToInt32(DetailsView1.DataKey[e.RowIndex].ToString());

SqlDataAdapter SQLAdapter = new SqlDataAdapter(“delete from UserMst where id=” + idd + “”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);

BindDetailsView();
}

I hope you enjoy with lean this ASP.Net Edit, update, delete Detialsview example…..

Paging in DetailsView control in ASP.Net C#

We have already learned to bind and display record to DetailsView control in ASP.Net.

In this post we will learn to do paging in DetailsView control in ASP.Net.

How to Display record in DetailsView in ASP.Net ?

First Set DetailsView property AllowPaging = True

How to do paging in asp.net c#

How to do paging in asp.net c#

Now bind the data to DetailsView control by clicking the Save Button.

protected void btnSave_Click(object sender, EventArgs e)
{

SqlConnection SQLConn = new SqlConnection(“Data Source=COMPUTER_1\\SQLEXPRESS;Initial Catalog=BOOK;Integrated Security=True”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“insert into UserMst values (‘”+  txtname.Text +”‘,’”+ txtcity.Text +”‘)”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
BindDetailsView();

}
private void BindDetailsView()
{

SqlConnection SQLConn = new SqlConnection(“Data Source=COMPUTER_1\\SQLEXPRESS;Initial Catalog=BOOK;Integrated Security=True”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“select * from UserMst”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
DetailsView1.DataSource = DT;
DetailsView1.DataBind();

}

Now, we have to write code at PageIndexChanging Event of DetailsView in ASP.Net.

How to do paging in asp.net c#

How to do paging in asp.net c#

write below code at PageIndexChanging Event of DetailsView for show next record in DetailsView control.

 protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
BindDetailsView();
}

How to do paging in asp.net c#

How to do paging in asp.net c#

Hope this ASP.Net DetailsView paging Example will help you…….

 

How to use DetailsView control in asp.net C#.

DetailsView control used to display a single database record of table layout in ASP.Net.

Here we will learn how to use DetailsView control in ASP.Net using C#.

STEP 1:  Open Visual Studio and Design asp.net webform like below screen.

<table>
<tr>
<td align=”right”>
Name :</td>
<td>
<asp:TextBox ID=”txtname” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td align=”right”>
City :</td>
<td>
<asp:TextBox ID=”txtcity” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID=”btnSave” runat=”server” onclick=”btnSave_Click” Text=”SAVE” />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:DetailsView ID=”DetailsView1″ runat=”server”
BackColor=”LightGoldenrodYellow” BorderColor=”Tan” BorderWidth=”1px”
CellPadding=”2″ DataKeyNames=”ID” ForeColor=”Black” GridLines=”None”
Height=”50px” onitemdeleting=”DetailsView1_ItemDeleting”
onitemupdating=”DetailsView1_ItemUpdating”
onmodechanging=”DetailsView1_ModeChanging”
onpageindexchanging=”DetailsView1_PageIndexChanging” Width=”125px”>
<FooterStyle BackColor=”Tan” />
<PagerStyle BackColor=”PaleGoldenrod” ForeColor=”DarkSlateBlue”
HorizontalAlign=”Center” />
<HeaderStyle BackColor=”Tan” Font-Bold=”True” />
<EditRowStyle BackColor=”DarkSlateBlue” ForeColor=”GhostWhite” />
<AlternatingRowStyle BackColor=”PaleGoldenrod” />
</asp:DetailsView>
</td>
</tr>
</table>

The ASP.Net DetailsView Example Design Output is:

How to use DetailsView Control in ASP.Net C#

How to use DetailsView Control in ASP.Net C#

Now, After doing this next step to bind the DetailsView control from Database.

Here, we first insert record in to database and fetch record from database and display it to DetailsView control in ASP.Net.

write below code on SAVE button for insert and bind data to DetailsView in ASP.net.

protected void btnSave_Click(object sender, EventArgs e)
{

SqlConnection SQLConn = new SqlConnection(“Data Source=COMPUTER_1\\SQLEXPRESS;Initial Catalog=BOOK;Integrated Security=True”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“insert into UserMst values (‘”+  txtname.Text +”‘,'”+ txtcity.Text +”‘)”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
BindDetailsView();

}
private void BindDetailsView()
{

SqlConnection SQLConn = new SqlConnection(“Data Source=COMPUTER_1\\SQLEXPRESS;Initial Catalog=BOOK;Integrated Security=True”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“select * from UserMst”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
DetailsView1.DataSource = DT;
DetailsView1.DataBind();
}

The Out put of DetailsView control example is

How to use DetailsView Control in ASP.Net C#

How to use DetailsView Control in ASP.Net C#

If we are using DetailsView control in our website then we can display a single record at a time, so we must do paging with DetailsView control for show all the record one by one…..

How to do paging in DetailsView control in ASP.Net ?