日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

手动绑定数据到DatList并实现编辑,删除,取消···

發(fā)布時(shí)間:2025/6/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手动绑定数据到DatList并实现编辑,删除,取消··· 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
效果圖:

前臺(tái)代碼: <%@?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>無標(biāo)題頁(yè)</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">
????????????????????????????學(xué)號(hào):
</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">
????????????????????????????專業(yè):
</td>
????????????????????????
<td>
???????????????????????????
<asp:TextBox?
????????????????????????????????ID
="TextBox5"?runat="server"?Text='<%#Eval("department")?%>'></asp:TextBox>
????????????????????????
</td>
????????????????????
</tr>
????????????????????
<tr>
????????????????????????
<td?class="style3">
????????????????????????????班級(jí):
</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>
????????????????????????????
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
????????????????????????????
<asp:LinkButton?ID="LinkButton2"?runat="server"?CommandName="cancel">取消</asp:LinkButton>
????????????????????????
</td>
????????????????????
</tr>
????????????????
</table>
????????????
</EditItemTemplate>
????????????
<SelectedItemStyle?BackColor="#738A9C"?Font-Bold="True"?ForeColor="#F7F7F7"?/>
???????????????
????????????
<HeaderTemplate>
????????????????學(xué)生基本情況
????????????
</HeaderTemplate>
???????????????
????????????
<HeaderStyle?BackColor="#4A3C8C"?Font-Bold="True"?ForeColor="#F7F7F7"?/>
????????????
<ItemTemplate>
????????????????
????????????????
<table?class="style1"?border="1">
????????????????????
<tr>
????????????????????????
<td?class="style3">
????????????????????????????學(xué)號(hào):
</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">
????????????????????????????專業(yè):
</td>
????????????????????????
<td>
????????????????????????????
<%#DataBinder.Eval(Container.DataItem,"department")?%></td>
????????????????????
</tr>
????????????????????
<tr>
????????????????????????
<td?class="style3">
????????????????????????????班級(jí):
</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>
????????????????????????????
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
????????????????????????????
<asp:LinkButton?ID="LinkButton2"?runat="server"?CommandName="delete">刪除</asp:LinkButton>
????????????????????????
</td>
????????????????????
</tr>
????????????????
</table>
????????????????
????????????
</ItemTemplate>
????????
</asp:DataList>
????
????
</div>
????
</form>
</body>
</html>
后臺(tái)代碼:
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(
"錯(cuò)誤"?+?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(
"錯(cuò)誤信息:"?+?erro.Message);
????????}

????????
finally
????????
{
????????????conn.Close();
????????}

????}

????
protected?void?DataList1_ItemCommand(object?source,?DataListCommandEventArgs?e)
????
{

????}

}

總結(jié)

以上是生活随笔為你收集整理的手动绑定数据到DatList并实现编辑,删除,取消···的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。