ASP.NET中ListView(列表视图)的使用前台绑定附源码
程序员文章站
2024-03-04 17:50:12
1.a,运行效果图 1.b,源代码 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" co...
1.a,运行效果图
1.b,源代码
<%@ page language="c#" autoeventwireup="true" codefile="droplvw.aspx.cs" inherits="droplvw" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
请选择大分类:<asp:dropdownlist id="drop" runat="server" autopostback="true"
datasourceid="sqldatasource1" datatextfield="categoryname"
datavaluefield="categoryid">
</asp:dropdownlist>
<asp:sqldatasource id="sqldatasource1" runat="server"
connectionstring="<%$ connectionstrings:northwindconnectionstring %>"
selectcommand="select [categoryid], [categoryname] from [categories]">
</asp:sqldatasource>
<hr />
该分类下的产品:<br />
<asp:listview id="lvw" runat="server" datakeynames="productid"
datasourceid="sqldatasource2">
<itemtemplate>
<tr style="">
<td>
<asp:label id="productidlabel" runat="server" text='<%# eval("productid") %>' />
</td>
<td>
<asp:label id="productnamelabel" runat="server"
text='<%# eval("productname") %>' />
</td>
<td>
<asp:label id="categoryidlabel" runat="server"
text='<%# eval("categoryid") %>' />
</td>
<td>
<asp:label id="unitpricelabel" runat="server" text='<%# eval("unitprice") %>' />
</td>
</tr>
</itemtemplate>
<alternatingitemtemplate>
<tr style="">
<td>
<asp:label id="productidlabel" runat="server" text='<%# eval("productid") %>' />
</td>
<td>
<asp:label id="productnamelabel" runat="server"
text='<%# eval("productname") %>' />
</td>
<td>
<asp:label id="categoryidlabel" runat="server"
text='<%# eval("categoryid") %>' />
</td>
<td>
<asp:label id="unitpricelabel" runat="server" text='<%# eval("unitprice") %>' />
</td>
</tr>
</alternatingitemtemplate>
<emptydatatemplate>
<table runat="server" style="">
<tr>
<td>
未返回数据。</td>
</tr>
</table>
</emptydatatemplate>
<insertitemtemplate>
<tr style="">
<td>
<asp:button id="insertbutton" runat="server" commandname="insert" text="插入" />
<asp:button id="cancelbutton" runat="server" commandname="cancel" text="清除" />
</td>
<td>
</td>
<td>
<asp:textbox id="productnametextbox" runat="server"
text='<%# bind("productname") %>' />
</td>
<td>
<asp:textbox id="categoryidtextbox" runat="server"
text='<%# bind("categoryid") %>' />
</td>
<td>
<asp:textbox id="unitpricetextbox" runat="server"
text='<%# bind("unitprice") %>' />
</td>
</tr>
</insertitemtemplate>
<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">
productid</th>
<th runat="server">
productname</th>
<th runat="server">
categoryid</th>
<th runat="server">
unitprice</th>
</tr>
<tr id="itemplaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</layouttemplate>
<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:label id="productidlabel1" runat="server"
text='<%# eval("productid") %>' />
</td>
<td>
<asp:textbox id="productnametextbox" runat="server"
text='<%# bind("productname") %>' />
</td>
<td>
<asp:textbox id="categoryidtextbox" runat="server"
text='<%# bind("categoryid") %>' />
</td>
<td>
<asp:textbox id="unitpricetextbox" runat="server"
text='<%# bind("unitprice") %>' />
</td>
</tr>
</edititemtemplate>
<selecteditemtemplate>
<tr style="">
<td>
<asp:label id="productidlabel" runat="server" text='<%# eval("productid") %>' />
</td>
<td>
<asp:label id="productnamelabel" runat="server"
text='<%# eval("productname") %>' />
</td>
<td>
<asp:label id="categoryidlabel" runat="server"
text='<%# eval("categoryid") %>' />
</td>
<td>
<asp:label id="unitpricelabel" runat="server" text='<%# eval("unitprice") %>' />
</td>
</tr>
</selecteditemtemplate>
</asp:listview>
<asp:sqldatasource id="sqldatasource2" runat="server"
connectionstring="<%$ connectionstrings:northwindconnectionstring %>"
selectcommand="select [productid], [productname], [categoryid], [unitprice] from [products] where ([categoryid] = @categoryid)">
<selectparameters>
<asp:controlparameter controlid="drop" defaultvalue="1" name="categoryid"
propertyname="selectedvalue" type="int32" />
</selectparameters>
</asp:sqldatasource>
</div>
</form>
</body>
</html>
/web.config
<connectionstrings>
<add name="northwindconnectionstring" connectionstring="data
source=.;initial catalog=northwind;integrated security=true"
providername="system.data.sqlclient"/>
</connectionstrings>
1.c,资源下载
1.b,源代码
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="droplvw.aspx.cs" inherits="droplvw" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
请选择大分类:<asp:dropdownlist id="drop" runat="server" autopostback="true"
datasourceid="sqldatasource1" datatextfield="categoryname"
datavaluefield="categoryid">
</asp:dropdownlist>
<asp:sqldatasource id="sqldatasource1" runat="server"
connectionstring="<%$ connectionstrings:northwindconnectionstring %>"
selectcommand="select [categoryid], [categoryname] from [categories]">
</asp:sqldatasource>
<hr />
该分类下的产品:<br />
<asp:listview id="lvw" runat="server" datakeynames="productid"
datasourceid="sqldatasource2">
<itemtemplate>
<tr style="">
<td>
<asp:label id="productidlabel" runat="server" text='<%# eval("productid") %>' />
</td>
<td>
<asp:label id="productnamelabel" runat="server"
text='<%# eval("productname") %>' />
</td>
<td>
<asp:label id="categoryidlabel" runat="server"
text='<%# eval("categoryid") %>' />
</td>
<td>
<asp:label id="unitpricelabel" runat="server" text='<%# eval("unitprice") %>' />
</td>
</tr>
</itemtemplate>
<alternatingitemtemplate>
<tr style="">
<td>
<asp:label id="productidlabel" runat="server" text='<%# eval("productid") %>' />
</td>
<td>
<asp:label id="productnamelabel" runat="server"
text='<%# eval("productname") %>' />
</td>
<td>
<asp:label id="categoryidlabel" runat="server"
text='<%# eval("categoryid") %>' />
</td>
<td>
<asp:label id="unitpricelabel" runat="server" text='<%# eval("unitprice") %>' />
</td>
</tr>
</alternatingitemtemplate>
<emptydatatemplate>
<table runat="server" style="">
<tr>
<td>
未返回数据。</td>
</tr>
</table>
</emptydatatemplate>
<insertitemtemplate>
<tr style="">
<td>
<asp:button id="insertbutton" runat="server" commandname="insert" text="插入" />
<asp:button id="cancelbutton" runat="server" commandname="cancel" text="清除" />
</td>
<td>
</td>
<td>
<asp:textbox id="productnametextbox" runat="server"
text='<%# bind("productname") %>' />
</td>
<td>
<asp:textbox id="categoryidtextbox" runat="server"
text='<%# bind("categoryid") %>' />
</td>
<td>
<asp:textbox id="unitpricetextbox" runat="server"
text='<%# bind("unitprice") %>' />
</td>
</tr>
</insertitemtemplate>
<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">
productid</th>
<th runat="server">
productname</th>
<th runat="server">
categoryid</th>
<th runat="server">
unitprice</th>
</tr>
<tr id="itemplaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</layouttemplate>
<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:label id="productidlabel1" runat="server"
text='<%# eval("productid") %>' />
</td>
<td>
<asp:textbox id="productnametextbox" runat="server"
text='<%# bind("productname") %>' />
</td>
<td>
<asp:textbox id="categoryidtextbox" runat="server"
text='<%# bind("categoryid") %>' />
</td>
<td>
<asp:textbox id="unitpricetextbox" runat="server"
text='<%# bind("unitprice") %>' />
</td>
</tr>
</edititemtemplate>
<selecteditemtemplate>
<tr style="">
<td>
<asp:label id="productidlabel" runat="server" text='<%# eval("productid") %>' />
</td>
<td>
<asp:label id="productnamelabel" runat="server"
text='<%# eval("productname") %>' />
</td>
<td>
<asp:label id="categoryidlabel" runat="server"
text='<%# eval("categoryid") %>' />
</td>
<td>
<asp:label id="unitpricelabel" runat="server" text='<%# eval("unitprice") %>' />
</td>
</tr>
</selecteditemtemplate>
</asp:listview>
<asp:sqldatasource id="sqldatasource2" runat="server"
connectionstring="<%$ connectionstrings:northwindconnectionstring %>"
selectcommand="select [productid], [productname], [categoryid], [unitprice] from [products] where ([categoryid] = @categoryid)">
<selectparameters>
<asp:controlparameter controlid="drop" defaultvalue="1" name="categoryid"
propertyname="selectedvalue" type="int32" />
</selectparameters>
</asp:sqldatasource>
</div>
</form>
</body>
</html>
/web.config
复制代码 代码如下:
<connectionstrings>
<add name="northwindconnectionstring" connectionstring="data
source=.;initial catalog=northwind;integrated security=true"
providername="system.data.sqlclient"/>
</connectionstrings>
1.c,资源下载
上一篇: 经典算法:基数排序的小例子
下一篇: css 博客分类: 编程基础