欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET笔记之 ListView 与 DropDownList的使用

程序员文章站 2024-03-31 16:49:16
1、repeater用来显示数据、listview用来操作数据 insertitemtemplate和updateitemtemplate**eval(显示数据)和b...

1、repeater用来显示数据、listview用来操作数据

ASP.NET笔记之 ListView 与 DropDownList的使用

insertitemtemplate和updateitemtemplate
**eval(显示数据)和bind(双向绑定:不仅是需要展现,更需要把数据绑定到数据库中)

itemplaceholderid:占位符,决定占位,把头部(之上)和尾部(之下)分隔开
itemtemplate:展示功能

自动生成的listview需要调整的地方
(1、生成的样式要提到style中,不要用内联的方式
(2、itemtemplate里面一半没必要用<asp:label>展示只读数据,所以可以直接输出
<%#eval("id")%>
(3、layouttemplate中必须有一个itempplaceholderid 的服务端控件
(4、layouttemplate中表头的位置要汉化,所有template中的不需显示的字段需删除或更改位置

2、事件
流程同repeater:

//首先判断数据行的类型
e.item.itemtype==listviewitemtype.dataitem

//把e.item转化成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、设定相应的按钮、控件、validator为童颜的validationgroup,
防止不同模板中的validator互相干扰,
(2、将cancel按钮中的causesvalidation="false"使得插入修改数据时
可以取消操作,这样即使在同一个分组内也可以不互相影响

ASP.NET笔记之 ListView 与 DropDownList的使用

4、给insertitemplate增加默认值
//在itemcreate属性中进入函数
if(e.item.itemtype==listviewitemtype.insertitem){
textbox agetext=(textbox)e.item.findcontrol("ageid");
agetext.text="20";
}

5、主键guid:插入到数据库

(1、listview的iteminserting属性:
//要插入到数据库之前的数据的键值对
e.values["id"]=guid.newguid();

(2、listview的itemupdateing属性:
e.itemidex
e.oldvalues//更新前的值
e.newvalues["age"]//更新后的值
e.cancel=true;//取消非法数据插入

objectdatasource
绑定id为guid 类型的时候

 6、dropdrownlist

ASP.NET笔记之 ListView 与 DropDownList的使用
(1、
//包含在dropdrownlist中的项
<asp:listitem value="man">男</asp:listitem>

(2、
**后台代码:更新的时候
//找到listview
//listview1.item[e.itemindex].findcontrol("id");
//它是一个dropviewlist
dropdrownlist d=(dropdrownlist)listview1.item[e.itemindex].findcontrol("id");
//赋值
e.newvalues=["字段"]=d.selectedvalue;

(3、
**后台代码:实现编辑时显示原先的数据
//有数据行
if(e.item.itemtype==listviewdatalist.dataitem){
//取控件
dropdownlist d=(dropdownlist)e.item.findcontrol("id");

if(d!=null){
//取到这一行绑定的数据
listviewdataitem lv=(listviewdataitem)e.item;
datarowitem row=(datarowitem)lv.dataitem;
//如果这一行有数据
if(row!=null){
//读取数据库该row的值
var myrow=(项目名称.dal.datasetusers.t_users)row.row;

//将读取打偶的row值设置为下拉菜单中的选项
d.selectedvalue=myrow.字段;
}
}
}

(4、 可以看不可以用 enabled="false

 

友情链接管理:

效果:

ASP.NET笔记之 ListView 与 DropDownList的使用

存在问题总结:

(1、警告 1 元素“listview”不是已知元素。原因可能是网站中存在编译错误,或者缺少 web.config 文件。 e:\code\projects\website_zzl01\友情链接\linkurl_admin.aspx 39 10 友情链接

(2、onlinktypechange(this,'" + logoid.clientid + "') 中传给前台javascript的id不是客户端的id,会导致显示和隐藏的功能无法实现,所以增加一个myid

:   logoid.attributes["myid"] = logoid.clientid; 来传递参数

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数据绑定每一行
             //显示数据
             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)
         {
             //插入数据
             dropdownlist ddlslinktype = (dropdownlist)e.item.findcontrol("ddlslinktype");
             e.values["slinktype"] = ddlslinktype.selectedvalue;   
         }

         protected void listview1_itemupdating(object sender, listviewupdateeventargs e)
         {
             //更新数据
             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是后台代码调用前台javascript中自定义的jquery函数

                     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>友情链接管理页面</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(); //传到到客户端不是客户端的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>
                         未返回数据。</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="序号必须为整数" 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">
                                     序号</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="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>