现在的位置: 首页 > 综合 > 正文

DataList嵌套问题 如何删除内层子DataList的记录

2013年10月31日 ⁄ 综合 ⁄ 共 5101字 ⁄ 字号 评论关闭

因为一个网友的提问,做了下面的这个Demo,完成了删除内层子DataList的记录,示例数据来源为pubs数据库的authors(主表)和titleauthor(从表),同时用另一种方法解决了如果表的主键有多个组成,如何进行删除

问题描述:   
    我在后台做栏目管理的设置,就做二级栏目就可以了,数据库中一个表实现的。使用DATALIST嵌套,外层和内层列表实现删除一级根栏目和二级子栏目的功能,一级栏目可以实现,ID号很好获取,但是内层的怎么都获取不到ID值所以无法删除。

//aspx

<%@ Page language="c#" Codebehind="DataListDemo.aspx.cs" AutoEventWireup="false" Inherits="DataListDemo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <script language="javascript">
   function DeleteTitle(au_id, title_id)
   {
    if (confirm('真的删除?'))
    {
     document.getElementById('auid').value = au_id;
     document.getElementById('titleid').value = title_id;
     __doPostBack('DeleteTitle', '');
    }
   }
  </script>
 </HEAD>
 <body>
  <form id="Form1" method="post" runat="server">
   <input type="hidden" ID="titleid" runat="server" NAME="title_id"> <input type="hidden" ID="auid" runat="server" NAME="au_id">
   <asp:LinkButton ID="DeleteTitle" runat="server" OnClick="DeleteTitle_Click" Visible="False" />
   <asp:datalist id="DataList1" runat="server" DataKeyField="au_id" BorderColor="#CC9966" BorderStyle="None"
    BackColor="White" CellPadding="4" GridLines="Both" BorderWidth="1px">
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
    <ItemTemplate>
     au_id:
     <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "au_id")%>'>
     </asp:Label><BR>
     au_lname:
     <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "au_lname")%>'>
     </asp:Label><BR>
     <asp:LinkButton id="lbtnDelete" runat="server" CommandName="delete">删除</asp:LinkButton>
     <asp:DataList id=DataList2 runat="server" DataKeyField="Title_id" DataSource='<%# GetTitleID(DataBinder.Eval(Container.DataItem, "au_id").ToString()) %>' BorderColor="#CC9966" BorderStyle="None" BackColor="White" CellPadding="4" GridLines="Both" BorderWidth="1px">
      <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
      <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
      <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
      <ItemTemplate>
       title_id:
       <asp:Label id="Label4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title_id")%>'>
       </asp:Label><BR>
       <asp:HyperLink Runat="server" NavigateUrl='<%# "javascript:DeleteTitle(/"" + DataBinder.Eval(Container.DataItem, "au_id") + "/",/"" +DataBinder.Eval(Container.DataItem, "title_id") + "/")" %>' Text="删除" ID="Hyperlink1"/>
      </ItemTemplate>
      <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
     </asp:DataList>
    </ItemTemplate>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
   </asp:datalist></form>
 </body>
</HTML>

//aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class DataListDemo : System.Web.UI.Page
{
 protected System.Web.UI.HtmlControls.HtmlInputHidden auid;
 protected System.Web.UI.HtmlControls.HtmlInputHidden titleid;
 protected System.Web.UI.WebControls.LinkButton DeleteTitle;
 protected System.Web.UI.WebControls.DataList DataList1;

 private void BindList()
 {
  SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs");
  SqlDataAdapter da = new SqlDataAdapter("select * from authors", cn);
  DataSet ds = new DataSet();
  cn.Open();
  da.Fill(ds);
  cn.Close();
  DataList1.DataSource = ds;
  DataList1.DataBind();

 }

 private void Page_Load(object sender, System.EventArgs e)
 {
  if(!IsPostBack)
  {
   BindList();
  }
 }

 #region Web 窗体设计器生成的代码
 override protected void OnInit(EventArgs e)
 {
  InitializeComponent();
  base.OnInit(e);
 }
 
 private void InitializeComponent()
 {   
  this.Load += new System.EventHandler(this.Page_Load);

 }
 #endregion

 public DataView GetTitleID(string au_id)
 {
  SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs");
  SqlDataAdapter da = new SqlDataAdapter("select au_id, title_id from titleauthor where au_id = @au_id", cn);
  da.SelectCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = au_id;
  DataSet ds = new DataSet();
  cn.Open();
  da.Fill(ds);
  cn.Close();
  return ds.Tables[0].DefaultView;
 }

 protected void DeleteTitle_Click(object sender, EventArgs e)
 {
  string au_id = auid.Value;
  string title_id = titleid.Value;
  
  SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs");
  SqlCommand cmd = new SqlCommand("delete from titleauthor where au_id = @au_id and title_id = @title_id", cn);
  cmd.Parameters.Add("@au_id", SqlDbType.VarChar).Value = au_id;
  cmd.Parameters.Add("@title_id", SqlDbType.VarChar).Value = title_id;
  cn.Open();
  cmd.ExecuteNonQuery();
  cn.Close();
  BindList();
 }

}

 

抱歉!评论已关闭.