代码改变世界

数据控件的使用心得

2010-01-23 19:34  三皮开发时  阅读(259)  评论(0编辑  收藏  举报

1.DataList--实现了跨表传值方法  

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="DataList" %>

<!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">
        .tab1 tr td
        {
            width: 900px;
            text-align: center;
        }
        .tab1 tr th
        {
            width: 900px;
            text-align: center;
        }
        .edit tr td
        {
            width: 100px;
        }
        .select tr td
        {
            width: 400px;
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="div1" class="tab1">
        <asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" OnDeleteCommand="DataList1_DeleteCommand"
            OnItemCommand="DataList1_ItemCommand">
            <EditItemTemplate>
                <table id="edit" class="edit">
                    <tr>
                        <td>
                            voteItemID:
                        </td>
                        <td>
                            <asp:Label ID="lab1" runat="server" Text='<%#Eval("voteItemID") %>'></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            voteID:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("voteID") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            voteContent:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("voteContent") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            voteTotal:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("voteTotal") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="button1" runat="server" Text="更新" CommandName="upda" CommandArgument='<%#Eval("voteItemID") %>' />
                        </td>
                        <td>
                            <asp:Button ID="button4" runat="server" Text="取消" CommandName="cancel" />
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
            <HeaderTemplate>
                <table>
                    <tr>
                        <th>
                            项ID
                        </th>
                        <th>
                            voteID
                        </th>
                        <th>
                            内容
                        </th>
                        <th>
                            投票数
                        </th>
                        <th>
                            详细信息
                        </th>
                        <th>
                            编辑
                        </th>
                        <th>
                            删除
                        </th>
                    </tr>
                </table>
            </HeaderTemplate>
            <SelectedItemTemplate>
                <table id="select" class="select">
                    <tr>
                        <td>
                            <%#Eval("voteItemID") %>的详细信息
                        </td>
                    </tr>
                    <tr>
                        <td>
                            voteID:<asp:Label ID="lblVoteId" runat="server" Text='<%#Eval("voteID") %>'></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            voteTitle:<asp:Label ID="Label9" runat="server" Text='<%#GetModel(Convert.ToInt32(Eval("voteID"))).VoteTitle.ToString()%>'></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="btn23" runat="server" Text="退出浏览详细信息" CommandName="exit" />
                        </td>
                    </tr>
                </table>
            </SelectedItemTemplate>
            <ItemTemplate>
                <table>
                    <tr onmouseover="this.style.backgroundColor='#3366FF'"   onmouseout="this.style.backgroundColor='' ">
                        <td>
                            <asp:Label ID="label1" runat="server" Text='<%#Eval("voteItemID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("voteID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("voteContent") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label4" runat="server" Text='<%#Eval("voteTotal") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:LinkButton ID="LinkButton1" runat="server" Text="详细信息" CommandName="information"></asp:LinkButton>
                        </td>
                        <td>
                            <asp:LinkButton ID="LinkButton2" runat="server" Text="编辑" CommandName="ed"></asp:LinkButton>
                        </td>
                        <td>
                            <asp:LinkButton ID="LinkButton3" runat="server" Text="删除" OnClientClick="return confirm('确认删除?')"
                                CommandName="del" CommandArgument='<%#Eval("voteItemID") %>'></asp:LinkButton>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>
后台:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;

public partial class DataList : System.Web.UI.Page
{

    public string strCon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();

        }

    }
    /// <summary>
    /// 绑定
    /// </summary>
    public void Bind()
    {

        SqlConnection con = new SqlConnection(strCon);

        SqlCommand cmd = new SqlCommand("select * from tb_VoteItem", con);
        cmd.CommandText = "select * from tb_VoteItem";

        SqlDataAdapter adapt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapt.Fill(ds);

        DataList1.DataSource = ds.Tables[0].DefaultView;
        DataList1.DataBind();

    }

    public class Model
    {
        private int _voteID;
        private string _voteTitle;

        public int VoteID
        {
            get { return _voteID; }
            set { _voteID = value; }
        }

        public string VoteTitle
        {
            get { return _voteTitle; }
            set { _voteTitle = value; }
        }
    }

    public Model GetModel(int voteID)
    {
        Model model=new Model ();
        SqlConnection con=new SqlConnection (strCon);

        SqlCommand cmd = new SqlCommand();
       cmd.Connection=con;

       if (cmd.Connection.State != ConnectionState.Open)
           cmd.Connection.Open();

       cmd.CommandText = "SELECT DISTINCT tb_Vote.voteTitle, tb_VoteItem.voteID FROM tb_Vote CROSS JOIN tb_VoteItem WHERE tb_Vote.voteID ="+voteID;
      
        SqlDataReader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
      
        FillModel(ref model,dr);
        return model;

    }

    protected void FillModel(ref Model model, SqlDataReader dr)
        {
       
            try
            {
                if (dr.Read())
                {
                    model =new Model ();
                    model.VoteID=Convert.ToInt32(dr["voteID"]);
                    model.VoteTitle=dr["voteTitle"].ToString();
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                dr.Dispose();
            }
        }   
   
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {

    }

    //啥操作都可在这完成 --GridView 不行和ListView部分行部分不行
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "ed")
        {
            DataList1.EditItemIndex = e.Item.ItemIndex;
            Bind();
        }
        if (e.CommandName == "cancel")
        {
            DataList1.EditItemIndex = -1;
            Bind();

        }
        if (e.CommandName == "upda")
        {
            string voteID = ((TextBox)e.Item.FindControl("TextBox1")).Text.Trim();
            string voteContent = ((TextBox)e.Item.FindControl("TextBox2")).Text.Trim();
            string voteTotal = ((TextBox)e.Item.FindControl("TextBox3")).Text.Trim();
            string str = "update  tb_VoteItem set voteID='" + voteID + "', voteContent='" + voteContent + "', voteTotal='" + voteTotal + "'  where voteItemID=" + e.CommandArgument;
            SqlConnection con = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand(str, con);
            if (cmd.Connection.State != ConnectionState.Open)
                cmd.Connection.Open();
            if (cmd.ExecuteNonQuery() > 0)
            {
                Response.Write("<script>alert('更新成功 !')</script>");
                DataList1.EditItemIndex = -1;
                Bind();
                cmd.Connection.Close();
            }
            else
            {
                Response.Write("<script>alert('更新失败 !')</script>");
            }

        }
        if (e.CommandName == "del")
        {
            SqlConnection con = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand("delete tb_VoteItem where voteItemID=" + e.CommandArgument, con);

            if (cmd.Connection.State != ConnectionState.Open)
                cmd.Connection.Open();
            if (cmd.ExecuteNonQuery() > 0)
            { Response.Write("<script>alert('删除成功!')</script>"); Bind(); }
            else Response.Write("<script>alert('删除失败 !')</script>");
            cmd.Connection.Close();
        }
        if (e.CommandName == "information")
        {
            DataList1.SelectedIndex = e.Item.ItemIndex;
            Bind();
        }
        if (e.CommandName == "exit")
        {
            DataList1.SelectedIndex = -1;
            Bind();
        }


    } 

}

2.ListView

前台:

<%@ 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>
    <link href="css/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListView ID="ListView1" runat="server" OnItemEditing="ListView1_ItemEditing"
            OnItemCommand="ListView1_ItemCommand" OnSelectedIndexChanging="ListView1_SelectedIndexChanging"
            OnItemInserting="ListView1_ItemInserting">
            <ItemTemplate>
                <table id="item" class="item">
                    <tr onmouseover="this.style.backgroundColor='#3366FF'"   onmouseout="this.style.backgroundColor='' ">
                        <td>
                            <asp:Label ID="labVote" runat="server" Text='<%#Eval("voteItemID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="labVoteTitle" runat="server" Text='<%#Eval("voteID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblContent" runat="server" Text='<%#(Eval("voteContent").ToString().Length>5)?Eval("voteContent").ToString().Substring(0,5)+"...":Eval("voteContent") %>'
                                ToolTip='<%#Eval("voteContent") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblTotal" runat="server" Text='<%#Eval("voteTotal") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:LinkButton ID="lnkbtnSelect" runat="server" Text="详细信息" CommandName="select"></asp:LinkButton>
                        </td>
                        <td>
                            <asp:LinkButton ID="lnkbtnEdit" runat="server" Text="编辑" CommandName="edit"></asp:LinkButton>
                        </td>
                        <td>
                            <asp:LinkButton ID="lnkbtnDel" runat="server" Text="删除" CommandName="del" CommandArgument='<%#Eval("voteItemID") %>'
                                OnClientClick="return confirm('确认删除?')"></asp:LinkButton>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
            <EmptyItemTemplate>
                <table runat="server">
                    <tr>
                        <td>
                            未返回数据
                        </td>
                    </tr>
                </table>
            </EmptyItemTemplate>
            <InsertItemTemplate>
                <table id="insert" class="insert">
                    <tr onmouseover="this.style.backgroundColor='#3366FF'"   onmouseout="this.style.backgroundColor='' ">
                        <td colspan="2">
                            添加信息
                        </td>
                    </tr>
                    <tr onmouseover="this.style.backgroundColor='#3366FF'"   onmouseout="this.style.backgroundColor='' ">
                        <td>
                            voteID:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr onmouseover="this.style.backgroundColor='#3366FF'"   onmouseout="this.style.backgroundColor='' ">
                        <td>
                            voteContent:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr onmouseover="this.style.backgroundColor='#3366FF'"   onmouseout="this.style.backgroundColor='' ">
                        <td>
                            voteTotal:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr onmouseover="this.style.backgroundColor='#3366FF'"   onmouseout="this.style.backgroundColor='' ">
                        <td colspan="2">
                            <asp:Button ID="btnAdd" runat="server" Text="添加" CommandName="insert" />
                        </td>
                    </tr>
                </table>
            </InsertItemTemplate>
            <LayoutTemplate>
                <table runat="server" id="layout" class="layout">
                    <tr runat="server">
                        <td runat="server">
                            <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr runat="server">
                                    <th runat="server">
                                        项ID
                                    </th>
                                    <th runat="server">
                                        投票ID
                                    </th>
                                    <th id="Th2" runat="server">
                                        投票选项内容
                                    </th>
                                    <th id="Th3" runat="server">
                                        投票总和
                                    </th>
                                    <th runat="server">
                                        详细信息
                                    </th>
                                    <th runat="server">
                                        编辑
                                    </th>
                                    <th id="Th1" runat="server">
                                        删除
                                    </th>
                                </tr>
                                <tr id="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <EditItemTemplate>
                <table id="edit" class="edit">
                    <tr>
                        <td>
                            项ID
                        </td>
                        <td>
                            <asp:Label ID="txtVoteId" runat="server" Text='<%#Eval("voteItemID") %>'></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            投票ID
                        </td>
                        <td>
                            <asp:TextBox ID="txtVoteTitle" runat="server" Text='<%#Eval("voteID") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            投票选项
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("voteContent") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            投票总数
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("voteTotal") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="btnUpdate" runat="server" Text="更新" CommandName="up" CommandArgument='<%#Eval("voteItemID") %>' />
                        </td>
                        <td>
                            <asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="can" />
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
            <SelectedItemTemplate>
                <table id="sel" class="sel">
                    <tr>
                        ?查看详细信息<td>
                            <asp:Label ID="lblVoteId" runat="server" Text='<%#Eval("voteItemID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblVoteTitle" runat="server" Text='<%#Eval("voteID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("voteContent") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("voteTotal") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblDis" runat="server" Text="详细信息专栏"></asp:Label>
                        </td>
                        <td>
                            <asp:Button ID="btn1" runat="server" Text="退出Select模板" CommandName="es" />
                        </td>
                    </tr>
                </table>
            </SelectedItemTemplate>
        </asp:ListView>
        <asp:Button ID="displayInsTem" runat="server" Text="显示插入框" OnClick="displayInsTem_Click" />
        <asp:Button ID="noneInsTem" runat="server" Text="隐藏插入框" OnClick="noneInsTem_Click" />
    </div>
    </form>
</body>
</html>

后台:

和DataList 差不多 ,只不过 编辑模板、选择模板、插入模板 要在他提供的事件中写并且固定CommondName

是编辑的就edit  等。 另外插入模板 显示形式是: <asp:ListView ID="ListVIew1" **** InsertItemTemplatePosition=>

自己选就可以出来,也可以在后台控制 button 事件下   ListView1.InsertItemTemplatePosition=InsertItemTemplatePosition.属性; 既可.

其他的操作,如删除的操作,更新的操作等。都可以在ItemCommand下完成.