ASP.NET筆記之 ListView 與 DropDownList的使用
1、Repeater用來(lái)顯示數(shù)據(jù)、ListView用來(lái)操作數(shù)據(jù)
InsertItemTemplate和updateItemTemplate
**Eval(顯示數(shù)據(jù))和Bind(雙向綁定:不僅是需要展現(xiàn),更需要把數(shù)據(jù)綁定到數(shù)據(jù)庫(kù)中)
ItemPlaceholderID:占位符,決定占位,把頭部(之上)和尾部(之下)分隔開(kāi)
ItemTemplate:展示功能
自動(dòng)生成的ListView需要調(diào)整的地方
(1、生成的樣式要提到style中,不要用內(nèi)聯(lián)的方式
(2、ItemTemplate里面一半沒(méi)必要用<asp:Label>展示只讀數(shù)據(jù),所以可以直接輸出
<%#Eval("id")%>
(3、LayoutTemplate中必須有一個(gè)ItempPlaceholderID 的服務(wù)端控件
(4、LayoutTemplate中表頭的位置要漢化,所有template中的不需顯示的字段需刪除或更改位置
2、事件
流程同Repeater:
//首先判斷數(shù)據(jù)行的類(lèi)型
e.Item.ItemType==ListViewItemType.DataItem
//把e.Item轉(zhuǎn)化成ListViewDataItem才能拿到DataItem
ListViewDataItem lvDataItem=(ListViewDataItem)e.Item;
DataRowView rowView=(DataRowView)lvDataItem.DataItem;
//獲得某一列
var xRow=(...DAL.DataSet1.T_UserRow)rowVIew.Row;
//獲得某一列的值
xRow.Age、xRow.sName...etc.
3、具體注意
(1、設(shè)定相應(yīng)的按鈕、控件、Validator為童顏的ValidationGroup,
防止不同模板中的Validator互相干擾,
(2、將Cancel按鈕中的CausesValidation="false"使得插入修改數(shù)據(jù)時(shí)
可以取消操作,這樣即使在同一個(gè)分組內(nèi)也可以不互相影響
4、給InsertItemplate增加默認(rèn)值
//在ItemCreate屬性中進(jìn)入函數(shù)
if(e.Item.ItemType==ListViewItemType.InsertItem){
TextBox AgeText=(TextBox)e.Item.FindControl("AgeID");
AgeText.Text="20";
}
5、主鍵Guid:插入到數(shù)據(jù)庫(kù)
(1、ListView的ItemInserting屬性:
//要插入到數(shù)據(jù)庫(kù)之前的數(shù)據(jù)的鍵值對(duì)
e.values["id"]=Guid.NewGuid();
(2、ListView的ItemUpdateing屬性:
e.ItemIdex
e.OldValues//更新前的值
e.NewValues["Age"]//更新后的值
e.Cancel=true;//取消非法數(shù)據(jù)插入
ObjectDataSource
綁定id為guid 類(lèi)型的時(shí)候
6、DropDrownList
(1、
//包含在DropDrownList中的項(xiàng)
<asp:ListItem value="man">男</asp:ListItem>
(2、
**后臺(tái)代碼:更新的時(shí)候
//找到ListView
//ListView1.Item[e.ItemIndex].FindControl("ID");
//它是一個(gè)DropViewList
DropDrownList d=(DropDrownList)listView1.Item[e.ItemIndex].FindControl("ID");
//賦值
e.NewValues=["字段"]=d.SelectedValue;
(3、
**后臺(tái)代碼:實(shí)現(xiàn)編輯時(shí)顯示原先的數(shù)據(jù)
//有數(shù)據(jù)行
if(e.Item.ItemType==ListVIewDataList.DataItem){
//取控件
DropDownList d=(DropDownLIst)e.Item.FindControl("ID");
if(d!=null){
//取到這一行綁定的數(shù)據(jù)
ListViewDataItem lv=(ListViewDataItem)e.Item;
DataRowItem row=(dataRowItem)lv.DataItem;
//如果這一行有數(shù)據(jù)
if(row!=null){
//讀取數(shù)據(jù)庫(kù)該Row的值
var myRow=(項(xiàng)目名稱(chēng).DAL.DataSetUsers.T_Users)row.Row;
//將讀取打偶的Row值設(shè)置為下拉菜單中的選項(xiàng)
d.SelectedValue=myRow.字段;
}
}
}
(4、 可以看不可以用 Enabled="false
友情鏈接管理:
效果:
存在問(wèn)題總結(jié):
(1、警告 1 元素“ListView”不是已知元素。原因可能是網(wǎng)站中存在編譯錯(cuò)誤,或者缺少 web.config 文件。 E:\code\Projects\WebSite_zzl01\友情鏈接\LinkUrl_Admin.aspx 39 10 友情鏈接
(2、onLinkTypeChange(this,'" + logoID.ClientID + "') 中傳給前臺(tái)javascript的ID不是客戶(hù)端的ID,會(huì)導(dǎo)致顯示和隱藏的功能無(wú)法實(shí)現(xiàn),所以增加一個(gè)myID
: logoID.Attributes["myid"] = logoID.ClientID; 來(lái)傳遞參數(shù)
LinkUrl_Admin.aspx.cs
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 友情鏈接
{
public partial class LinkUrl_Admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//ListView1的屬性ItemDataBound數(shù)據(jù)綁定每一行
//顯示數(shù)據(jù)
if (e.Item.ItemType == ListViewItemType.DataItem) {
DropDownList ddlsLinkType = (DropDownList)e.Item.FindControl("ddlsLinkType");
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
DataRowView myrow = (DataRowView)dataitem.DataItem;
if (ddlsLinkType != null && myrow != null) {
var sUrl = (友情鏈接.ADL.DataSet1.T_LinksRow)myrow.Row;
ddlsLinkType.SelectedValue = sUrl.sLinkType;
}
}
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
//插入數(shù)據(jù)
DropDownList ddlsLinkType = (DropDownList)e.Item.FindControl("ddlsLinkType");
e.Values["sLinkType"] = ddlsLinkType.SelectedValue;
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
//更新數(shù)據(jù)
DropDownList ddlsLinkType = (DropDownList)ListView1.Items[e.ItemIndex].FindControl("ddlsLinkType");
e.NewValues["sLinkType"] = ddlsLinkType.SelectedValue;
}
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem || e.Item.ItemType ==
ListViewItemType.InsertItem) {
DropDownList ddlsLinkType = (DropDownList)e.Item.FindControl("ddlsLinkType");
TextBox logoID = (TextBox)e.Item.FindControl("LogoUrlTextBox");
if (ddlsLinkType != null&&logoID!=null) {
//onchange是html中select的屬性
//onLinkTypeChange是后臺(tái)代碼調(diào)用前臺(tái)javascript中自定義的jQuery函數(shù)
logoID.Attributes["myid"] = logoID.ClientID;
ddlsLinkType.Attributes["onchange"] = "onLinkTypeChange(this,'" + logoID.ClientID + "')";
if(ddlsLinkType.SelectedValue=="Text"){
logoID.Style["display"] = "none";
}
}
}
}
}
}
LinkUrl_Admin.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinkUrl_Admin.aspx.cs" Inherits="友情鏈接.LinkUrl_Admin" %>
<!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>友情鏈接管理頁(yè)面</title>
<script src="Scripts/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
function onLinkTypeChange(urlType, logoID) {
if ($(urlType).val() == "Text") {
$("input:text[myid=" + logoID + "]").hide();
//$("#" + logoID).hide(); //傳到到客戶(hù)端不是客戶(hù)端的id
//$("#ListView1_LogoURLTextBox").hide();//真正的id
}
else {
$("input:text[myid=" + logoID + "]").show();
//$("#" + logoID).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="友情鏈接.ADL.DataSet1TableAdapters.T_LinksTableAdapter"
UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_ID" Type="Int64" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="SeoNo" Type="Int32" />
<asp:Parameter Name="SiteName" Type="String" />
<asp:Parameter Name="sLinkType" Type="String" />
<asp:Parameter Name="SiteUrl" Type="String" />
<asp:Parameter Name="LogoUrl" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="SeoNo" Type="Int32" />
<asp:Parameter Name="SiteName" Type="String" />
<asp:Parameter Name="sLinkType" Type="String" />
<asp:Parameter Name="SiteUrl" Type="String" />
<asp:Parameter Name="LogoUrl" Type="String" />
<asp:Parameter Name="Original_ID" Type="Int64" />
</UpdateParameters>
</asp:ObjectDataSource>
</div>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID"
DataSourceID="ObjectDataSource1" InsertItemPosition="LastItem"
onitemdatabound="ListView1_ItemDataBound"
oniteminserting="ListView1_ItemInserting"
onitemupdating="ListView1_ItemUpdating"
onitemcreated="ListView1_ItemCreated">
<EditItemTemplate>
<tr style="background-color: #999999;">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
</td>
<td>
<asp:TextBox ID="SeoNoTextBox" runat="server" Text='<%# Bind("SeoNo") %>' />
</td>
<td>
<asp:TextBox ID="SiteNameTextBox" runat="server"
Text='<%# Bind("SiteName") %>' />
</td>
<td>
<asp:DropDownList ID="ddlsLinkType" runat="server">
<asp:ListItem Value="Text">文本</asp:ListItem>
<asp:ListItem Value="Pic">圖片</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="SiteUrlTextBox" runat="server" Text='<%# Bind("SiteUrl") %>' />
</td>
<td>
<asp:TextBox ID="LogoUrlTextBox" runat="server" Text='<%# Bind("LogoUrl") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
<tr>
<td>
未返回?cái)?shù)據(jù)。</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="SeoNoTextBox" ValidationGroup="Insert" runat="server" Text='<%# Bind("SeoNo") %>' />
<asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="SeoNoTextBox">
</asp:RequiredFieldValidator>
<asp:CompareValidator ValidationGroup="Insert" ID="CompareValidator1" runat="server" ErrorMessage="序號(hào)必須為整數(shù)" ControlToValidate="SeoNoTextBox" Operator="DataTypeCheck" Type="Integer">
</asp:CompareValidator>
</td>
<td>
<asp:TextBox ID="SiteNameTextBox" ValidationGroup="Insert" runat="server" MaxLength="50"
Text='<%# Bind("SiteName") %>' />
<asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="SiteNameTextBox">
</asp:RequiredFieldValidator>
</td>
<td>
<asp:DropDownList ID="ddlsLinkType" ValidationGroup="Insert" runat="server" >
<asp:ListItem Value="Text">文本</asp:ListItem>
<asp:ListItem Value="Pic">圖片</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="SiteUrlTextBox" ValidationGroup="Insert" runat="server" Text='<%# Bind("SiteUrl") %>' />
<asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator3" runat="server" ErrorMessage="*" ControlToValidate="SiteUrlTextBox">
</asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="LogoUrlTextBox" ValidationGroup="Insert" runat="server" Text='<%# Bind("LogoUrl") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="background-color: #E0FFFF;color: #333333;">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="刪除" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編輯" />
</td>
<td>
<asp:Label ID="SeoNoLabel" runat="server" Text='<%# Eval("SeoNo") %>' />
</td>
<td>
<asp:Label ID="SiteNameLabel" runat="server" Text='<%# Eval("SiteName") %>' />
</td>
<td>
<asp:DropDownList ID="ddlsLinkType" runat="server" Enabled="false">
<asp:ListItem Value="Text">文本</asp:ListItem>
<asp:ListItem Value="Pic">圖片</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Label ID="SiteUrlLabel" runat="server" Text='<%# Eval("SiteUrl") %>' />
</td>
<td>
<asp:Label ID="LogoUrlLabel" runat="server" Text='<%# Eval("LogoUrl") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color: #E0FFFF;color: #333333;">
<th runat="server">
</th>
<th runat="server">
序號(hào)</th>
<th runat="server">
網(wǎng)站名稱(chēng)</th>
<th runat="server">
鏈接類(lèi)型</th>
<th runat="server">
網(wǎng)站網(wǎng)址</th>
<th runat="server">
logo網(wǎng)址</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</form>
</body>
</html>
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- asp.net Repeater取得CheckBox選中的某行某個(gè)值的c#寫(xiě)法
- 在asp.net中實(shí)現(xiàn)datagrid checkbox 全選的方法
- asp.net gridview中用checkbox全選的幾種實(shí)現(xiàn)的區(qū)別
- asp.net 獲取Datalist中Checkbox的值的小結(jié)
- WPF的ListView控件自定義布局用法實(shí)例
- asp.net ListView 數(shù)據(jù)綁定
- ASP.NET中ListView(列表視圖)的使用前臺(tái)綁定附源碼
- asp.net ListView交替背景顏色實(shí)現(xiàn)代碼
- WPF實(shí)現(xiàn)帶全選復(fù)選框的列表控件
相關(guān)文章
asp.net MaxLengthValidator 最大長(zhǎng)度驗(yàn)證控件代碼
如果數(shù)據(jù)庫(kù)字段為varchar或char類(lèi)型,ASP.NET控件在可輸入漢字的情況下,MaxLength屬性不能保證在保存到數(shù)據(jù)庫(kù)時(shí)不發(fā)生截?cái)噱e(cuò)誤,因此寫(xiě)了一個(gè)最大長(zhǎng)度驗(yàn)證控件,還可用于多行文本框。2009-12-12DataGridView中CheckBox實(shí)現(xiàn)某一列單選
DataGridView中CheckBox實(shí)現(xiàn)某一列單選,需要的朋友可以參考一下2013-02-02asp.net core2.2多用戶(hù)驗(yàn)證與授權(quán)示例詳解
這篇文章主要給大家介紹了關(guān)于asp.net core2.2多用戶(hù)驗(yàn)證與授權(quán)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01高效的.Net UDP異步編程實(shí)現(xiàn)分析
重點(diǎn)是怎么建立一種高效的UDP機(jī)制來(lái)實(shí)時(shí)接收服務(wù)器發(fā)送過(guò)來(lái)的數(shù)據(jù)包.本文將介紹.Net UDP異步編程如何實(shí)現(xiàn)解決方案,有需求的朋友可以參考下2012-11-11asp.net core集成kindeditor實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了asp.net core集成kindeditor實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11.NET微信開(kāi)發(fā)之PC 端微信掃碼注冊(cè)和登錄功能實(shí)現(xiàn)
這篇文章主要介紹了.NET微信開(kāi)發(fā)之PC 端微信掃碼注冊(cè)和登錄功能實(shí)現(xiàn)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09關(guān)于Swagger優(yōu)化的實(shí)戰(zhàn)記錄
Swagger是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化?RESTful風(fēng)格的Web服務(wù),下面這篇文章主要給大家介紹了關(guān)于Swagger優(yōu)化的相關(guān)資料,需要的朋友可以參考下2022-04-04開(kāi)源跨平臺(tái)運(yùn)行服務(wù)插件TaskCore.MainForm
這篇文章主要為大家詳細(xì)介紹了開(kāi)源跨平臺(tái)運(yùn)行服務(wù)插件TaskCore.MainForm的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
使用ASP.NET模版生成HTML靜態(tài)頁(yè)面并不是難事,主要是使各個(gè)靜態(tài)頁(yè)面間的關(guān)聯(lián)和鏈接如何保持完整。本文介紹了使用ASP.NET模版生成HTML靜態(tài)頁(yè)面的五種方案2011-11-11