问题描述:    
    我在后台做栏目管理的设置,就做二级栏目就可以了,数据库中一个表实现的。使用DATALIST嵌套,外层和内层列表实现删除一级根栏目和二级子栏目的功能,一级栏目可以实现,ID号很好获取,但是内层的怎么都获取不到ID值所以无法删除。求救:很急,希望给出解决方法最好有代码实例。谢谢。解决后立刻结贴。我使用的是1.1框架,C#!!

解决方案 »

  1.   

    你的删除按钮是两个DataList里面都有,还是只有外面的DataList有?
      

  2.   

    我也想问下:你的删除按钮是两个DataList里面都有,还是只有外面的DataList有?
      

  3.   

    外面一层DATALIST的删除是删除根栏目的。
    内层DATALIST也有删除按钮,删除是子栏目的!!
      

  4.   

    给你一个例子,实现了内层的删除,外层的你就自己解决一下吧,用的是pubs数据库
    //aspx
    <%@ Page language="c#" Codebehind="DataListDemo.aspx.cs" AutoEventWireup="false" Inherits="WebForm1" %>
    <!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>
      

  5.   

    //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 WebForm1 : 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();
    }
    }
      

  6.   

    <asp:LinkButton ID="DeleteTitle" runat="server" OnClick="DeleteTitle_Click" Visible="False" />
    中间通过这个转了一道子,不太方便哟?!
      

  7.   

    现在报错了:
    <asp:HyperLink ID="del_childboard" Text="删除" Runat="server" NavigateUrl='<%# javascript:DeleteTitle(\""+DataBinder.Eval(Container.DataItem,"board_id")+"\") %>'>删除栏目</asp:HyperLink></font></TD>
    行 101: </TR>
    行 102: </TABLE>错误是:编译器错误信息: CS1010: 常数中有换行符
      

  8.   

    外层栏目已经实现了删除,现在就是要获取子栏目ID进行删除。
    amandag(高歌) 兄弟的方法我已经领会了,做的时候出现了上面的问题!
      

  9.   

    这个地方有点麻烦,其实是因为这段javascript中单引号和双引号嵌套到第三层的原因,我没有找到合适的解决方法,因为偶的javascript也就是入门。如果出错了把这段javascript再复制粘贴一下...
      

  10.   

    amandag高歌的方法不错,顶一个!