手动绑定数据到DatList并实现编辑,删除,取消···
生活随笔
收集整理的這篇文章主要介紹了
手动绑定数据到DatList并实现编辑,删除,取消···
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
效果圖:
前臺代碼: <%@?Page?Language="C#"?AutoEventWireup="true"??CodeFile="Default.aspx.cs"?Inherits="_Default"?%>
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html?xmlns="http://www.w3.org/1999/xhtml">
<head?runat="server">
????<title>無標題頁</title>
????????????<style?type="text/css">
??????????????????????.style1
????????????????{
????????????????????width:?600px;
????????????????}
????????????????????.style3
????????????????{
????????????????????width:?150px;
????????????????}
????????</style>
????</head>
<body>
????<p>
????????<br?/>
????</p>
????<form?id="form1"?runat="server">
????<div?align="center">
????
????????<asp:DataList?ID="DataList1"?runat="server"?Width="600px"?BackColor="White"?
????????????BorderColor="#E7E7FF"?BorderStyle="None"?BorderWidth="1px"?CellPadding="3"?
????????????GridLines="Both"?Height="421px"?oncancelcommand="DataList1_CancelCommand"?
????????????ondeletecommand="DataList1_DeleteCommand"?oneditcommand="DataList1_EditCommand"?
????????????onitemcommand="DataList1_ItemCommand"?onupdatecommand="DataList1_UpdateCommand"?
????????????RepeatColumns="4"?DataKeyField="id">
????????????<FooterStyle?BackColor="#B5C7DE"?ForeColor="#4A3C8C"?/>
????????????<AlternatingItemStyle?BackColor="#F7F7F7"?/>
????????????<ItemStyle?BackColor="#E7E7FF"?ForeColor="#4A3C8C"?/>
????????????<EditItemTemplate>
??
????????????????<table?class="style1"?border="1">
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????學號:</td>
????????????????????????<td>
???????????????????????????<asp:TextBox?ID="TextBox1"?
????????????????????????????????runat="server"?Text='<%#?Eval("id")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????姓名:</td>
????????????????????????<td>
?????????????????????????????<asp:TextBox?ID="TextBox2"?
?????????????????????????????????runat="server"?Text='<%#?Eval("name")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????性別:</td>
????????????????????????<td>
?????????????????????????????<asp:TextBox?ID="TextBox3"?
?????????????????????????????????runat="server"?Text='<%#?Eval("sex")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????年齡:</td>
????????????????????????<td>
??????????????????????<asp:TextBox?ID="TextBox4"?
?????????????????????????????????runat="server"?Text='<%#?Eval("age")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????專業:</td>
????????????????????????<td>
???????????????????????????<asp:TextBox?
????????????????????????????????ID="TextBox5"?runat="server"?Text='<%#Eval("department")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????班級:</td>
????????????????????????<td>
?????????????????????????????<asp:TextBox?ID="TextBox6"?
?????????????????????????????????runat="server"?Text='<%#?Eval("grade")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style2"?colspan="2">
????????????????????????????<asp:LinkButton?ID="LinkButton1"?runat="server"?CommandName="update">保存</asp:LinkButton>
????????????????????????????
????????????????????????????<asp:LinkButton?ID="LinkButton2"?runat="server"?CommandName="cancel">取消</asp:LinkButton>
????????????????????????</td>
????????????????????</tr>
????????????????</table>
????????????</EditItemTemplate>
????????????<SelectedItemStyle?BackColor="#738A9C"?Font-Bold="True"?ForeColor="#F7F7F7"?/>
???????????????
????????????<HeaderTemplate>
????????????????學生基本情況
????????????</HeaderTemplate>
???????????????
????????????<HeaderStyle?BackColor="#4A3C8C"?Font-Bold="True"?ForeColor="#F7F7F7"?/>
????????????<ItemTemplate>
????????????????
????????????????<table?class="style1"?border="1">
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????學號:</td>
????????????????????????<td>
????????????????????????????<%#DataBinder.Eval(Container.DataItem,"id")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????姓名:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"name")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????性別:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"sex")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????年齡:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"age")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????專業:</td>
????????????????????????<td>
????????????????????????????<%#DataBinder.Eval(Container.DataItem,"department")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????班級:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"grade")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style2"?colspan="2">
????????????????????????????<asp:LinkButton?ID="LinkButton1"?runat="server"?CommandName="edit">修改</asp:LinkButton>
????????????????????????????
????????????????????????????<asp:LinkButton?ID="LinkButton2"?runat="server"?CommandName="delete">刪除</asp:LinkButton>
????????????????????????</td>
????????????????????</tr>
????????????????</table>
????????????????
????????????</ItemTemplate>
????????</asp:DataList>
????
????</div>
????</form>
</body>
</html>
后臺代碼:
using?System;
using?System.Configuration;
using?System.Data;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.HtmlControls;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Data.SqlClient;
public?partial?class?_Default?:?System.Web.UI.Page?
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(!IsPostBack)
????????{
????????????Bind();
????????}
????}
????private?void?Bind()
????{
????????SqlConnection?conn?=?new?SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
????????SqlDataAdapter?adp?=?new?SqlDataAdapter("select?*?from?information",conn);
????????DataSet?dataset?=?new?DataSet();
????????conn.Open();
????????try
????????{
????????????adp.Fill(dataset,?"information");
????????????DataList1.DataSource?=?dataset;
????????????DataList1.DataKeyField?=?"id";
????????????DataList1.DataBind();
????????}
????????catch?(Exception?erro)
????????{
????????????Response.Write("錯誤"?+?erro);
????????}
????????finally
????????{
????????????conn.Close();
????????}
????}
????protected?void?DataList1_CancelCommand(object?source,?DataListCommandEventArgs?e)
????{
????????DataList1.EditItemIndex?=?-1;
????????Bind();
????}
????protected?void?DataList1_DeleteCommand(object?source,?DataListCommandEventArgs?e)
????{
????????SqlConnection?conn?=?new?SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
????????SqlCommand?comm=new?SqlCommand("delete?from?information?where?id='"+DataList1.DataKeys[e.Item.ItemIndex]+"'",conn);
????????conn.Open();
????????int?i=Convert.ToInt32(?comm.ExecuteNonQuery());
????????if?(i?>?0)
????????{
????????????Response.Write("<script>return?alert('刪除成功')");
????????????DataList1.EditItemIndex?=?-1;
????????????Bind();
????????}
????????else
????????{
????????????Response.Write("<script>return?alert('刪出失敗')");
????????}
????????conn.Close();
????}
????protected?void?DataList1_EditCommand(object?source,?DataListCommandEventArgs?e)
????{
????????DataList1.EditItemIndex?=?e.Item.ItemIndex;
????????Bind();
????}
????protected?void?DataList1_UpdateCommand(object?source,?DataListCommandEventArgs?e)
????{
????????string?ID?=?DataList1.DataKeys[e.Item.ItemIndex].ToString();
????????string?id?=?((TextBox)e.Item.FindControl("TextBox1")).Text;
????????string?name=((TextBox)e.Item.FindControl("TextBox2")).Text;
????????string?sex=?((TextBox)e.Item.FindControl("TextBox3")).Text;
????????string?age=((TextBox)e.Item.FindControl("TextBox4")).Text;
????????string?department=((TextBox)e.Item.FindControl("TextBox5")).Text;
????????string?grade=((TextBox)e.Item.FindControl("TextBox6")).Text;
????????SqlConnection?conn?=?new?SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
????????SqlCommand?comm?=?new?SqlCommand("update?information?set??id='"?+?id?+?"'?,?name='"?+?name?+?"'?,?sex='"?+?sex?+?"'?,?age='"+age+"'?,?department='"?+?department?+?"'?,?grade='"?+?grade?+?"'?where?id='"?+?ID?+?"'",?conn);
????????conn.Open();
????????try
????????{
????????????int?i?=?Convert.ToInt32(comm.ExecuteNonQuery());
????????????if?(i?>?0)
????????????{
????????????????Response.Write("<script?language=javascript>alert('保存成功!')</script>");
????????????}
????????????else
????????????{
????????????????Response.Write("<script?language=javascript>alert('保存失敗!')</script>");
????????????}
????????????DataList1.EditItemIndex?=?-1;
????????????Bind();
????????}
????????catch?(Exception?erro)
????????{
????????????Response.Write("錯誤信息:"?+?erro.Message);
????????}
????????finally
????????{
????????????conn.Close();
????????}
????}
????protected?void?DataList1_ItemCommand(object?source,?DataListCommandEventArgs?e)
????{
????}
}
前臺代碼: <%@?Page?Language="C#"?AutoEventWireup="true"??CodeFile="Default.aspx.cs"?Inherits="_Default"?%>
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html?xmlns="http://www.w3.org/1999/xhtml">
<head?runat="server">
????<title>無標題頁</title>
????????????<style?type="text/css">
??????????????????????.style1
????????????????{
????????????????????width:?600px;
????????????????}
????????????????????.style3
????????????????{
????????????????????width:?150px;
????????????????}
????????</style>
????</head>
<body>
????<p>
????????<br?/>
????</p>
????<form?id="form1"?runat="server">
????<div?align="center">
????
????????<asp:DataList?ID="DataList1"?runat="server"?Width="600px"?BackColor="White"?
????????????BorderColor="#E7E7FF"?BorderStyle="None"?BorderWidth="1px"?CellPadding="3"?
????????????GridLines="Both"?Height="421px"?oncancelcommand="DataList1_CancelCommand"?
????????????ondeletecommand="DataList1_DeleteCommand"?oneditcommand="DataList1_EditCommand"?
????????????onitemcommand="DataList1_ItemCommand"?onupdatecommand="DataList1_UpdateCommand"?
????????????RepeatColumns="4"?DataKeyField="id">
????????????<FooterStyle?BackColor="#B5C7DE"?ForeColor="#4A3C8C"?/>
????????????<AlternatingItemStyle?BackColor="#F7F7F7"?/>
????????????<ItemStyle?BackColor="#E7E7FF"?ForeColor="#4A3C8C"?/>
????????????<EditItemTemplate>
??
????????????????<table?class="style1"?border="1">
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????學號:</td>
????????????????????????<td>
???????????????????????????<asp:TextBox?ID="TextBox1"?
????????????????????????????????runat="server"?Text='<%#?Eval("id")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????姓名:</td>
????????????????????????<td>
?????????????????????????????<asp:TextBox?ID="TextBox2"?
?????????????????????????????????runat="server"?Text='<%#?Eval("name")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????性別:</td>
????????????????????????<td>
?????????????????????????????<asp:TextBox?ID="TextBox3"?
?????????????????????????????????runat="server"?Text='<%#?Eval("sex")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????年齡:</td>
????????????????????????<td>
??????????????????????<asp:TextBox?ID="TextBox4"?
?????????????????????????????????runat="server"?Text='<%#?Eval("age")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????專業:</td>
????????????????????????<td>
???????????????????????????<asp:TextBox?
????????????????????????????????ID="TextBox5"?runat="server"?Text='<%#Eval("department")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????班級:</td>
????????????????????????<td>
?????????????????????????????<asp:TextBox?ID="TextBox6"?
?????????????????????????????????runat="server"?Text='<%#?Eval("grade")?%>'></asp:TextBox>
????????????????????????</td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style2"?colspan="2">
????????????????????????????<asp:LinkButton?ID="LinkButton1"?runat="server"?CommandName="update">保存</asp:LinkButton>
????????????????????????????
????????????????????????????<asp:LinkButton?ID="LinkButton2"?runat="server"?CommandName="cancel">取消</asp:LinkButton>
????????????????????????</td>
????????????????????</tr>
????????????????</table>
????????????</EditItemTemplate>
????????????<SelectedItemStyle?BackColor="#738A9C"?Font-Bold="True"?ForeColor="#F7F7F7"?/>
???????????????
????????????<HeaderTemplate>
????????????????學生基本情況
????????????</HeaderTemplate>
???????????????
????????????<HeaderStyle?BackColor="#4A3C8C"?Font-Bold="True"?ForeColor="#F7F7F7"?/>
????????????<ItemTemplate>
????????????????
????????????????<table?class="style1"?border="1">
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????學號:</td>
????????????????????????<td>
????????????????????????????<%#DataBinder.Eval(Container.DataItem,"id")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????姓名:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"name")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????性別:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"sex")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????年齡:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"age")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????專業:</td>
????????????????????????<td>
????????????????????????????<%#DataBinder.Eval(Container.DataItem,"department")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style3">
????????????????????????????班級:</td>
????????????????????????<td>
?????????????????????????????<%#DataBinder.Eval(Container.DataItem,"grade")?%></td>
????????????????????</tr>
????????????????????<tr>
????????????????????????<td?class="style2"?colspan="2">
????????????????????????????<asp:LinkButton?ID="LinkButton1"?runat="server"?CommandName="edit">修改</asp:LinkButton>
????????????????????????????
????????????????????????????<asp:LinkButton?ID="LinkButton2"?runat="server"?CommandName="delete">刪除</asp:LinkButton>
????????????????????????</td>
????????????????????</tr>
????????????????</table>
????????????????
????????????</ItemTemplate>
????????</asp:DataList>
????
????</div>
????</form>
</body>
</html>
后臺代碼:
using?System;
using?System.Configuration;
using?System.Data;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.HtmlControls;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Data.SqlClient;
public?partial?class?_Default?:?System.Web.UI.Page?
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(!IsPostBack)
????????{
????????????Bind();
????????}
????}
????private?void?Bind()
????{
????????SqlConnection?conn?=?new?SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
????????SqlDataAdapter?adp?=?new?SqlDataAdapter("select?*?from?information",conn);
????????DataSet?dataset?=?new?DataSet();
????????conn.Open();
????????try
????????{
????????????adp.Fill(dataset,?"information");
????????????DataList1.DataSource?=?dataset;
????????????DataList1.DataKeyField?=?"id";
????????????DataList1.DataBind();
????????}
????????catch?(Exception?erro)
????????{
????????????Response.Write("錯誤"?+?erro);
????????}
????????finally
????????{
????????????conn.Close();
????????}
????}
????protected?void?DataList1_CancelCommand(object?source,?DataListCommandEventArgs?e)
????{
????????DataList1.EditItemIndex?=?-1;
????????Bind();
????}
????protected?void?DataList1_DeleteCommand(object?source,?DataListCommandEventArgs?e)
????{
????????SqlConnection?conn?=?new?SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
????????SqlCommand?comm=new?SqlCommand("delete?from?information?where?id='"+DataList1.DataKeys[e.Item.ItemIndex]+"'",conn);
????????conn.Open();
????????int?i=Convert.ToInt32(?comm.ExecuteNonQuery());
????????if?(i?>?0)
????????{
????????????Response.Write("<script>return?alert('刪除成功')");
????????????DataList1.EditItemIndex?=?-1;
????????????Bind();
????????}
????????else
????????{
????????????Response.Write("<script>return?alert('刪出失敗')");
????????}
????????conn.Close();
????}
????protected?void?DataList1_EditCommand(object?source,?DataListCommandEventArgs?e)
????{
????????DataList1.EditItemIndex?=?e.Item.ItemIndex;
????????Bind();
????}
????protected?void?DataList1_UpdateCommand(object?source,?DataListCommandEventArgs?e)
????{
????????string?ID?=?DataList1.DataKeys[e.Item.ItemIndex].ToString();
????????string?id?=?((TextBox)e.Item.FindControl("TextBox1")).Text;
????????string?name=((TextBox)e.Item.FindControl("TextBox2")).Text;
????????string?sex=?((TextBox)e.Item.FindControl("TextBox3")).Text;
????????string?age=((TextBox)e.Item.FindControl("TextBox4")).Text;
????????string?department=((TextBox)e.Item.FindControl("TextBox5")).Text;
????????string?grade=((TextBox)e.Item.FindControl("TextBox6")).Text;
????????SqlConnection?conn?=?new?SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
????????SqlCommand?comm?=?new?SqlCommand("update?information?set??id='"?+?id?+?"'?,?name='"?+?name?+?"'?,?sex='"?+?sex?+?"'?,?age='"+age+"'?,?department='"?+?department?+?"'?,?grade='"?+?grade?+?"'?where?id='"?+?ID?+?"'",?conn);
????????conn.Open();
????????try
????????{
????????????int?i?=?Convert.ToInt32(comm.ExecuteNonQuery());
????????????if?(i?>?0)
????????????{
????????????????Response.Write("<script?language=javascript>alert('保存成功!')</script>");
????????????}
????????????else
????????????{
????????????????Response.Write("<script?language=javascript>alert('保存失敗!')</script>");
????????????}
????????????DataList1.EditItemIndex?=?-1;
????????????Bind();
????????}
????????catch?(Exception?erro)
????????{
????????????Response.Write("錯誤信息:"?+?erro.Message);
????????}
????????finally
????????{
????????????conn.Close();
????????}
????}
????protected?void?DataList1_ItemCommand(object?source,?DataListCommandEventArgs?e)
????{
????}
}
總結
以上是生活随笔為你收集整理的手动绑定数据到DatList并实现编辑,删除,取消···的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手动绑定数据到GridView并实现编辑
- 下一篇: struts2之单个文件上传