The use method of DetailsView in ASP.net
Last Update:2017-02-28
Source: Internet
Author: User
ASPX page:
Copy Code The code is as follows:
<asp:detailsview id= "DetailsView1" runat= "Server" autogeneraterows= "False" height= "50px"
Width= "500px" onmodechanging= "detailsview1_modechanging" onitemdeleting= "detailsview1_itemdeleting"
onitemupdating= "detailsview1_itemupdating" oniteminserting= "detailsview1_iteminserting" >
<Fields>
<asp:templatefield headertext= "ID" >
<ItemTemplate>
<% #Eval ("Id")%>
</ItemTemplate>
<InsertItemTemplate>
<asp:textbox id= "Txtinsertid" text= "Insertid" runat= "Server"/>
</InsertItemTemplate>
<EditItemTemplate>
<% #Eval ("Id")%>
</EditItemTemplate>
</asp:TemplateField>
<asp:templatefield headertext= "Title" >
<ItemTemplate>
<% #Eval ("Title")%>
</ItemTemplate>
<InsertItemTemplate>
<asp:textbox id= "Txtinserttitle" text= "Inserttitle" runat= "Server"/>
</InsertItemTemplate>
<EditItemTemplate>
<asp:textbox id= "Txtedittitle" text= ' <%# Eval ("Title")%> ' runat= ' server '/>
</EditItemTemplate>
</asp:TemplateField>
<asp:templatefield headertext= "Context" >
<ItemTemplate>
<%# Eval ("Logcontext")%>
</ItemTemplate>
<InsertItemTemplate>
<asp:textbox id= "Txtinsertlogcontext" text= "Insertlogcontext" runat= "Server"/>
</InsertItemTemplate>
<EditItemTemplate>
<asp:textbox id= "Txteditlogcontext" text= ' <%# Eval ("Logcontext")%> ' runat= ' server '/>
</EditItemTemplate>
</asp:TemplateField>
<asp:templatefield headertext= "Operations" >
<ItemTemplate>
<asp:button id= "Btnedit" runat= "Server" causesvalidation= "False"
Commandname= "Edit" text= "editor"/>
<asp:button id= "btnnew" runat= "Server" causesvalidation= "False"
Commandname= "new" text= "newly created"/>
<asp:button id= "Btndelete" runat= "Server" causesvalidation= "False"
Commandname= "Delete" text= "Remove" onclientclick= "return confirm (' OK to update this student information? '); "/>
</ItemTemplate>
<InsertItemTemplate>
<asp:button id= "Btninsert" runat= "Server" causesvalidation= "True"
Commandname= "Insert" text= "inserts"/>
<asp:button id= "Btncancel" runat= "Server" causesvalidation= "False"
Commandname= ' Cancel ' text= ' Cancel '/>
</InsertItemTemplate>
<EditItemTemplate>
<asp:button id= "btnupdate" runat= "Server" causesvalidation= "True"
Commandname= "Update" text= "onclientclick=" return confirm (' OK to update this student information? '); "/>
<asp:button id= "BtnCancel2" runat= "Server" causesvalidation= "False"
Commandname= ' Cancel ' text= ' Cancel '/>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
CS File:
Copy Code The code is as follows:
Using System;
Using System.Data;
Using System.Configuration;
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.Collections.Generic;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Datatobing ();
}
}
private void Datatobing ()
{
This. Detailsview1.datasource = Datagridsource ();
This. Detailsview1.databind ();
}
Private list<log> Datagridsource ()
{
list<log> logs = new list<log> ();
for (int i = 1; i < i++)
{
Log log = new log ();
Log. Id = i;
Log. title = "title" + I;
Log. Logcontext = "Content" + I;
Logs. ADD (log);
}
return logs;
}
public class Log
{
private int id;
public int Id
{
get {return ID;}
set {id = value;}
}
private string title;
public string Title
{
get {return title;}
set {title = value;}
}
private string Logcontext;
public string Logcontext
{
get {return logcontext;}
set {Logcontext = value;}
}
}
protected void Detailsview1_modechanging (object sender, DetailsViewModeEventArgs e)
{
This. Detailsview1.changemode (E.newmode);
Datatobing ();
}
protected void Detailsview1_itemdeleting (object sender, DetailsViewDeleteEventArgs e)
{
Response.Write ("delete operation");
}
protected void Detailsview1_itemupdating (object sender, DetailsViewUpdateEventArgs e)
{
TextBox Tbtitle = (textbox) this. Detailsview1.findcontrol ("Txtedittitle");
TextBox Tblogcontext = (textbox) this. Detailsview1.findcontrol ("Txtedittitle");
Response.Write ("Update operation: Title:" + Tbtitle.text + ": Logcontext:" + tblogcontext.text);
}
protected void Detailsview1_iteminserting (object sender, DetailsViewInsertEventArgs e)
{
Response.Write ("Insert operation:");
}
}