首页 > 代码库 > 数据绑定控件LiisView制作的网站友情链接管理

数据绑定控件LiisView制作的网站友情链接管理

客户端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FriendLinks.aspx.cs" Inherits="ListView控件的绑定.FriendLinks" %><!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>    <script src="http://www.mamicode.com/js/jquery-1.4.2.js" type="text/javascript"></script>    <script type="text/javascript">        function onLinkTypeChange(src, logoTextId) {      //这里定义一个函数控制logoLink是否显示            if ($(src).val() == "text") {                 //当LinkType为text时隐藏logoLink                $("#" + logoTextId).hide();            }            else {                $("#" + logoTextId).show();            }        }    </script></head><body>    <form id="form1" runat="server">    <div>            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"             DeleteMethod="Delete" InsertMethod="Insert"             OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"             TypeName="ListView控件的绑定.DAL.DataSetLinksTableAdapters.T_LinksTableAdapter"             UpdateMethod="Update">            <DeleteParameters>                <asp:Parameter Name="Original_Id" Type="Int64" />            </DeleteParameters>            <InsertParameters>                <asp:Parameter Name="SeqNo" Type="Int32" />                <asp:Parameter Name="SiteName" Type="String" />                <asp:Parameter Name="LinkType" Type="String" />                <asp:Parameter Name="SiteURL" Type="String" />                <asp:Parameter Name="LogeURL" Type="String" />            </InsertParameters>            <UpdateParameters>                <asp:Parameter Name="SeqNo" Type="Int32" />                <asp:Parameter Name="SiteName" Type="String" />                <asp:Parameter Name="LinkType" Type="String" />                <asp:Parameter Name="SiteURL" Type="String" />                <asp:Parameter Name="LogeURL" Type="String" />                <asp:Parameter Name="Original_Id" Type="Int64" />            </UpdateParameters>        </asp:ObjectDataSource>        <br />        <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"             DataSourceID="ObjectDataSource1" EnableModelValidation="True"             InsertItemPosition="LastItem" onitemcreated="ListView1_ItemCreated"             oniteminserting="ListView1_ItemInserting"             onitemupdating="ListView1_ItemUpdating"             onitemdatabound="ListView1_ItemDataBound">            <EditItemTemplate>                <tr style="">                    <td>                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />                    </td>                    <td>                        <asp:TextBox ID="SeqNoTextBox" runat="server" Text=‘<%# Bind("SeqNo") %>‘ />                    </td>                    <td>                        <asp:TextBox ID="SiteNameTextBox" runat="server"                             Text=‘<%# Bind("SiteName") %>‘ />                    </td>                    <td>                        <asp:DropDownList ID="DropDownList1" runat="server" >                        <asp:ListItem Value="http://www.mamicode.com/image">图片</asp:ListItem>                        <asp:ListItem Value="http://www.mamicode.com/text">文本</asp:ListItem>                        </asp:DropDownList>                    </td>                    <td>                        <asp:TextBox ID="SiteURLTextBox" runat="server" Text=‘<%# Bind("SiteURL") %>‘ />                    </td>                    <td>                        <asp:TextBox ID="LogeURLTextBox" runat="server" Text=‘<%# Bind("LogeURL") %>‘ />                    </td>                </tr>            </EditItemTemplate>            <EmptyDataTemplate>                <table runat="server" style="">                    <tr>                        <td>                            未返回数据。</td>                    </tr>                </table>            </EmptyDataTemplate>            <InsertItemTemplate>                <tr style="">                    <td>                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" ValidationGroup="insert"/>                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />                    </td>                    <td>                        <asp:TextBox ID="SeqNoTextBox" runat="server" Text=‘<%# Bind("SeqNo") %>‘  ValidationGroup="insert"/>                        <asp:RequiredFieldValidator                            ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="SeqNoTextBox" ValidationGroup="insert"></asp:RequiredFieldValidator>                        <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="必须是整数" ControlToValidate="SeqNoTextBox" Type="Integer" Operator="DataTypeCheck" ValidationGroup="insert"></asp:CompareValidator>                    </td>                    <td>                        <asp:TextBox ID="SiteNameTextBox" runat="server"                             Text=‘<%# Bind("SiteName") %>‘ ValidationGroup="insert"  />                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ValidationGroup="insert" ControlToValidate="SeqNoTextBox"></asp:RequiredFieldValidator>                    </td>                    <td>                        <asp:DropDownList ID="DropDownList1" runat="server" ValidationGroup="insert" >                        <asp:ListItem Value="http://www.mamicode.com/image">图片</asp:ListItem>                        <asp:ListItem Value="http://www.mamicode.com/text">文本</asp:ListItem>                        </asp:DropDownList>                    </td>                    <td>                        <asp:TextBox ID="SiteURLTextBox" runat="server" Text=‘<%# Bind("SiteURL") %>‘  ValidationGroup="insert"/>                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*" ValidationGroup="insert" ControlToValidate="SiteURLTextBox"></asp:RequiredFieldValidator>                    </td>                    <td>                        <asp:TextBox ID="LogeURLTextBox" runat="server" Text=‘<%# Bind("LogeURL") %>‘ ValidationGroup="insert" />                    </td>                </tr>            </InsertItemTemplate>            <ItemTemplate>                <tr style="">                    <td>                        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />                    </td>                    <td>                        <asp:Label ID="SeqNoLabel" runat="server" Text=‘<%# Eval("SeqNo") %>‘ />                    </td>                    <td>                        <asp:Label ID="SiteNameLabel" runat="server" Text=‘<%# Eval("SiteName") %>‘ />                    </td>                    <td>                        <asp:DropDownList ID="DropDownList1" runat="server" Enabled="false">                        <asp:ListItem Value="http://www.mamicode.com/image">图片</asp:ListItem>                        <asp:ListItem Value="http://www.mamicode.com/text">文本</asp:ListItem>                        </asp:DropDownList>                    </td>                    <td>                        <asp:Label ID="SiteURLLabel" runat="server" Text=‘<%# Eval("SiteURL") %>‘ />                    </td>                    <td>                        <asp:Label ID="LogeURLLabel" runat="server" Text=‘<%# Eval("LogeURL") %>‘ />                    </td>                </tr>            </ItemTemplate>            <LayoutTemplate>                <table runat="server">                    <tr runat="server">                        <td runat="server">                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="">                                <tr runat="server" style="">                                    <th runat="server">                                    </th>                                    <th runat="server">                                        序号</th>                                    <th runat="server">                                        网站名</th>                                    <th runat="server">                                        链接类型</th>                                    <th runat="server">                                        网站地址</th>                                    <th runat="server">                                        Logo链接</th>                                </tr>                                <tr ID="itemPlaceholder" runat="server">                                </tr>                            </table>                        </td>                    </tr>                    <tr runat="server">                        <td runat="server" style="">                        </td>                    </tr>                </table>            </LayoutTemplate>                    </asp:ListView>        </div>    </form></body></html>

  

  服务端代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;namespace ListView控件的绑定{    public partial class FriendLinks : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)        {            if(IsValid)            {                DropDownList ddlLinkType = (DropDownList)e.Item.FindControl("DropDownList1");                e.Values["LinkType"] = ddlLinkType.SelectedValue;            }                   }        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)        {            if (e.Item.ItemType == ListViewItemType.InsertItem)            {                DropDownList ddlLinkType = (DropDownList)e.Item.FindControl("DropDownList1");                TextBox txtBox = (TextBox)e.Item.FindControl("LogeURLTextBox");                if (ddlLinkType != null && txtBox != null )                {                    ddlLinkType.Attributes["onchange"] = "onLinkTypeChange(this,‘" + txtBox.ClientID + "‘)";//不知为什么加上这句话就不能插入                    if (ddlLinkType.SelectedValue =http://www.mamicode.com/="text")                    {                        ddlLinkType.Attributes["display"] = "none";                    }                }                           }        }        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)        {            DropDownList ddlLinkType = (DropDownList)ListView1.Items[e.ItemIndex].FindControl("DropDownList1");            e.NewValues["LinkType"] = ddlLinkType.SelectedValue;        }        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)        {            if (e.Item.ItemType == ListViewItemType.DataItem )            {                DropDownList ddlLinkType = (DropDownList)e.Item.FindControl("DropDownList1");                ListViewDataItem lvdi = (ListViewDataItem)e.Item;                DataRowView links = (DataRowView)lvdi.DataItem;                if (ddlLinkType != null && links!=null)                {                        var link = (ListView控件的绑定.DAL.DataSetLinks.T_LinksRow)links.Row;                        ddlLinkType.SelectedValue = http://www.mamicode.com/link.LinkType;>