asp.net 2.0 技巧2
一个detailview控件,用第一笔,上一笔,下一笔,最后一笔显示记录,还有可以有编辑,添加,删除等功能,还可以根据下拉列表显示页数的一个程序.作为备用之用,而且是无刷新的.
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table>
<tr>
<td style="width: 443px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="员工号码" DataSourceID="LimingStudio" ForeColor="#333333"
GridLines="None" Height="50px" Width="608px" OnDataBound="DetailsView1_DataBound"
OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted"
OnItemUpdated="DetailsView1_ItemUpdated" OnPageIndexChanging="DetailsView1_PageIndexChanging">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle Font-Bold="True" BackColor="#D1DDF1" />
<EditRowStyle BackColor="#2461BF" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#FFE0C0" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="员工号码" HeaderText="员工号码:" InsertVisible="False" ReadOnly="True"
SortExpression="员工号码">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
<ItemStyle Width="468px" />
</asp:BoundField>
<asp:BoundField DataField="身份证号码" HeaderText="身份证号码:" SortExpression="身份证号码">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="姓名" HeaderText="姓名:" SortExpression="姓名">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:TemplateField HeaderText="性别:" SortExpression="性别">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("性别") %>'>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" SelectedValue='<%# Bind("性别") %>'>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("性别") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="地址:" SortExpression="地址">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("地址") %>' Width="312px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("地址") %>' Width="312px"></asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("地址") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码:" SortExpression="邮政编码">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField ApplyFormatInEditMode="True" DataField="出生日期" DataFormatString="{0:d}"
HeaderText="出生日期:" HtmlEncode="False" SortExpression="出生日期">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:TemplateField HeaderText="婚姻状况:" SortExpression="婚姻状况">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" SelectedValue='<%# Bind("婚姻状况") %>'>
<asp:ListItem>已婚</asp:ListItem>
<asp:ListItem>未婚</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server" SelectedValue='<%# Bind("婚姻状况") %>'>
<asp:ListItem>已婚</asp:ListItem>
<asp:ListItem>未婚</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("婚姻状况") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ApplyFormatInEditMode="True" DataField="到职日期" DataFormatString="{0:d}"
HeaderText="到职日期:" HtmlEncode="False" SortExpression="到职日期">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="起薪" HeaderText="起薪:" SortExpression="起薪">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="目前薪资" HeaderText="目前薪资:" SortExpression="目前薪资">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField ApplyFormatInEditMode="True" DataField="加薪日期" DataFormatString="{0:d}"
HeaderText="加薪日期:" HtmlEncode="False" SortExpression="加薪日期">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:TemplateField HeaderText="部门:" SortExpression="部门">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="DepartmentList"
DataTextField="部门" DataValueField="部门" SelectedValue='<%# Bind("部门") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList7" runat="server" DataSourceID="DepartmentList"
DataTextField="部门" DataValueField="部门" SelectedValue='<%# Bind("部门") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("部门") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="更新" BackColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消" BackColor="White"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert"
Text="插入" BackColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消" BackColor="White"></asp:LinkButton>
</InsertItemTemplate>
<ControlStyle ForeColor="#C00000" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="New"
Text="添加"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm('您确定要删除此笔数据纪录吗?');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" VerticalAlign="Top" />
<AlternatingRowStyle BackColor="White" />
<PagerSettings FirstPageImageUrl="~/Images/First.gif" LastPageImageUrl="~/Images/Last.gif"
Mode="NextPreviousFirstLast" NextPageImageUrl="~/Images/Next.gif" PreviousPageImageUrl="~/Images/Previous.gif" />
<PagerTemplate>
<table width="100%">
<tr>
<td width="75%">
<font color="blue">移至第 </font>
<asp:DropDownList ID="ddlPage" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlPage_SelectedIndexChanged" />
<font color="blue">笔</font>
<asp:LinkButton CommandName="Page" CommandArgument="First" ID="lnkBtnFirst" runat="server">第一笔</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="lnkBtnPrev" runat="server">上一笔</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="lnkBtnNext" runat="server">下一笔</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="lnkBtnLast" runat="server">最后一笔</asp:LinkButton>
</td>
<td width="25%">
<asp:Label ID="lblCurrentPage" runat="server" ForeColor="Black" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:DetailsView>
<asp:SqlDataSource ID="LimingStudio" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
DeleteCommand="DELETE FROM [章立民工作室] WHERE [员工号码] = @员工号码" InsertCommand="INSERT INTO [章立民工作室] ([身份证号码], [姓名], [性别], [地址], [邮政编码], [出生日期], [婚姻状况], [到职日期], [起薪], [目前薪资], [加薪日期], [部门]) VALUES (@身份证号码, @姓名, @性别, @地址, @邮政编码, @出生日期, @婚姻状况, @到职日期, @起薪, @目前薪资, @加薪日期, @部门)"
SelectCommand="SELECT * FROM [章立民工作室] " UpdateCommand="UPDATE [章立民工作室] SET [身份证号码] = @身份证号码, [姓名] = @姓名, [性别] = @性别, [地址] = @地址, [邮政编码] = @邮政编码, [出生日期] = @出生日期, [婚姻状况] = @婚姻状况, [到职日期] = @到职日期, [起薪] = @起薪, [目前薪资] = @目前薪资, [加薪日期] = @加薪日期, [部门] = @部门 WHERE [员工号码] = @员工号码">
<DeleteParameters>
<asp:Parameter Name="员工号码" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="身份证号码" Type="String" />
<asp:Parameter Name="姓名" Type="String" />
<asp:Parameter Name="性别" Type="String" />
<asp:Parameter Name="地址" Type="String" />
<asp:Parameter Name="邮政编码" Type="String" />
<asp:Parameter Name="出生日期" Type="DateTime" />
<asp:Parameter Name="婚姻状况" Type="String" />
<asp:Parameter Name="到职日期" Type="DateTime" />
<asp:Parameter Name="起薪" Type="Decimal" />
<asp:Parameter Name="目前薪资" Type="Decimal" />
<asp:Parameter Name="加薪日期" Type="DateTime" />
<asp:Parameter Name="部门" Type="String" />
<asp:Parameter Name="员工号码" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="身份证号码" Type="String" />
<asp:Parameter Name="姓名" Type="String" />
<asp:Parameter Name="性别" Type="String" />
<asp:Parameter Name="地址" Type="String" />
<asp:Parameter Name="邮政编码" Type="String" />
<asp:Parameter Name="出生日期" Type="DateTime" />
<asp:Parameter Name="婚姻状况" Type="String" />
<asp:Parameter Name="到职日期" Type="DateTime" />
<asp:Parameter Name="起薪" Type="Decimal" />
<asp:Parameter Name="目前薪资" Type="Decimal" />
<asp:Parameter Name="加薪日期" Type="DateTime" />
<asp:Parameter Name="部门" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DepartmentList" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
SelectCommand="SELECT DISTINCT [部门] FROM [章立民工作室]"></asp:SqlDataSource>
<asp:Label ID="ErrorMessageLabel" runat="server" ForeColor="Red" Width="480px"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</div>
</form>
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
// 取得显示页数的那一列。
DetailsViewRow pagerRow = this.DetailsView1.BottomPagerRow;
// 取得"第一笔"、"上一笔"、"下一笔"与"最后一笔"的超级链接按钮。
LinkButton lnkBtnFirst = (LinkButton)(pagerRow.Cells[0].FindControl("lnkBtnFirst"));
LinkButton lnkBtnPrev = (LinkButton)(pagerRow.Cells[0].FindControl("lnkBtnPrev"));
LinkButton lnkBtnNext = (LinkButton)(pagerRow.Cells[0].FindControl("lnkBtnNext"));
LinkButton lnkBtnLast = (LinkButton)(pagerRow.Cells[0].FindControl("lnkBtnLast"));
// 设定何时应该启用或停用"第一笔"、"上一笔"、"下一笔"与"最后一笔"的超级链接按钮。
if (this.DetailsView1.PageIndex == 0)
{
lnkBtnFirst.Enabled = false;
lnkBtnPrev.Enabled = false;
}
else if (this.DetailsView1.PageIndex == this.DetailsView1.PageCount - 1)
{
lnkBtnLast.Enabled = false;
lnkBtnNext.Enabled = false;
}
else if (this.DetailsView1.PageCount <= 0)
{
lnkBtnFirst.Enabled = false;
lnkBtnPrev.Enabled = false;
lnkBtnNext.Enabled = false;
lnkBtnLast.Enabled = false;
}
// 从显示笔数的列中,取得显示笔数的 DropDownList 控件。
DropDownList pageList = (DropDownList)(pagerRow.Cells[0].FindControl("ddlPage"));
// 从显示笔数的列中,取得显示当前所在笔数的 Label 控件。
Label pageLabel = (Label)(pagerRow.Cells[0].FindControl("lblCurrentPage"));
// 根据所欲显示的数据来源之总笔数,来创建 DropDownList 控件的下拉菜单内容。
if (pageList != null)
{
for (int intPage = 0; intPage < this.DetailsView1.PageCount; intPage++)
{
// 创建一个 ListItem 对象来存放笔数清单。
int pageNumber = intPage + 1;
ListItem item = new ListItem(pageNumber.ToString());
// 每 10 笔设定一种背景颜色。
switch (Conversion.Fix(pageNumber / 10))
{
case 0:
case 2:
case 4:
case 6:
case 8:
item.Attributes.Add("style", "background: yellow;");
break;
case 1:
case 3:
case 5:
case 7:
case 9:
item.Attributes.Add("style", "background: cyan;");
break;
}
// 如果 ListItem 对象的笔数与当前所选取的笔数相同,
// 将该 ListItem 对象的笔数标记成被选取(Selected)的状态。
// 由于每当显示笔数的列被创建时,都需重新创建 DropDownList 控件,
// 此举将会把当前已选取的笔数状态保留在 DropDownList 控件中。
if (intPage == this.DetailsView1.PageIndex)
{
item.Selected = true;
}
// 把 ListItem 对象的笔数内容加入到 DropDownList 控件的项目集合中。
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
// 取得当前所在的笔数。
int currentPage = this.DetailsView1.PageIndex + 1;
// 更新显示当前所在笔数的信息。
pageLabel.Text = "位置:" + currentPage.ToString() + "∕" +
this.DetailsView1.PageCount.ToString();
}
}
// 当用户从下拉式清单方块中选取不同的笔数时就会执行此事件处理函数。
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
{
// 取得显示笔数的那一列。
DetailsViewRow pagerRow = this.DetailsView1.BottomPagerRow;
// 从显示笔数的列中,取得显示笔数的 DropDownList 控件。
DropDownList pageList = (DropDownList)(pagerRow.Cells[0].FindControl("ddlPage"));
// 将 GridView 移至用户所选取的笔数。
this.DetailsView1.PageIndex = pageList.SelectedIndex;
this.ErrorMessageLabel.Text = "";
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
// 在 DetailsView 控件中切换至其它页面后,
// 就将 DetailsView 控件切换至只读模式,也就是一般的显示模式。
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "在编辑数据时发生错误,请确认所输入之数据的格式是否正确。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已经成功编辑数据...";
}
}
protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "因为数据表上拥有外部索引键条件约束,因此无法删除。您只能删除没有关联数据记录的数据记录。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已经成功删除资料...";
}
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "在添加数据时发生错误,请确认所输入之数据的格式是否正确。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已经成功添加资料...";
}
}
asp.net 2.0 技巧2相关推荐
- 你可能已经知道或者不知道的ASP.NET 2.0技巧(转载)
1.在CallBack之后保持滚动条的位置 在Asp.Net1.1中,CallBack之后保持滚动条的位置是一件非常痛苦的事情,特别是页中有一个Grid并且想要编辑特定的行.为了不停留在想要得行,页面 ...
- Enterprise Library 2.0 技巧(3):记录ASP.NET站点中未处理的异常
这篇文章不能算是Enterprise Library 2.0的一个技巧,只是Logging Application Block的一个简单应用而已,在这里我们使用Logging Application ...
- asp.net 2.0常见问题技巧1
asp.net 2.0常见问题技巧1 常见的一个应用场景,就是gridview中,当库存量少于某个数时,背景颜色先变色 还有就是对某一列统计其总和,显示在页脚里,下面分别阐述之 首先是当库存小于某个 ...
- ASP.NET 2.0编程技巧之用Response对象输出图像
一. 简介 作为ASP.NET基本对象之一的Response对象不但可以通过Write()方法直接在页面上输出字符串数据,而且还可以使用BinaryWrite()方法直接显示二进制表示的数据,如图像. ...
- asp.net 2.0 下的一个小技巧
在.net下,如果你编译过网站再一次上传时,此时用户访问你的网站时就会出现应用程序错误之类的提示,可不可以设一个"网站维护中"的提示呢? 当然可以了! ----- 在asp.net ...
- 用好ASP.NET 2.0的URL映射
简介: URL映射是ASP.NET 2.0中提供的新特性.URL映射技术帮助我们将一个特定URL映射为另一个URL.为了帮助理解,我们假设你在站点有一个叫Homepage.aspx的页面来访问主页,所 ...
- ASP.NET 2.0 中的新增安全功能
发布日期: 8/26/2004 | 更新日期: 8/26/2004 Stephen Walther Microsoft Corporation 适用于: Microsoft ASP.NET 2.0 M ...
- ASP.NET 2.0站点登录、导航与权限管理
(一)登录 asp.net 2.0中,新增了许多新的功能和控件.其中,新增的登陆控件可谓使得WEB应用的设计更加得心应手.什么是登陆控件呢?就是我们平常在WEB应用中经常要用到的用户注册,登陆,忘记密 ...
- asp.net性能的技巧
1. 数据库访问性能优化 数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资源.ASP.NET中提 供了连接池 ...
最新文章
- python用for循环一直出现最后一个值_python中for循环的list最后一个数据总会覆盖前面的数据...
- HyperPocket:生成点云网络
- seaborn使用axes_dict函数获取displot函数生成的图像所有标题信息、使用set_title函数自定义设置多面板直方图标题(Multi-panel histogram‘s title)
- writelines是python对文件的写操作方法_Python File writelines() 方法
- golang go build 报错 import cycle not allowed
- threejs加载obj模型_Vulkan编程指南(章节31-载入模型)
- ADAS关键技术剖析、应用现状以及发展趋势
- C语言数组的一些运算*a,a+1,a+1,a+0
- spring中getBeansWithAnnotation(Class<? extends Annotation> annotationType)方法
- Ubuntu12.04 LTS 忘记登录密码的解决方法
- leetcode 1600. 皇位继承顺序(dfs)
- 提升存储设备的吞吐量
- 让垃圾分类开发“极快致简”的好物件,零基础的开发小白也能轻松驾驭它!
- pandas 数据分析 相关性_Pandas库学习笔记2-Pandas数据特征分析
- 王莉:将开发文档英文化和本地化,我们努力让OpenHarmony走向全球
- Android CompoundButton
- ADADELTA: AN ADAPTIVE LEARNING RATE METHOD
- 论文阅读:VITON: An Image-based Virtual Try-on Network(基于图像的虚拟试衣网络)
- 卡巴斯基密码管理器新版面世,再也不必担心账户安全
- 论火力发电锅炉之改进
热门文章
- EF optimize the perfermance
- 可动态扩展的数据库模型设计
- 20个顶尖产品经理都在用的APP
- 【产品】腾讯内部的顶级产品课:灵动在细节
- IOS用户界面的新视觉比例
- Java高并发和多线程系列 - 1. 线程基本概念
- MyBatis的DAO接口中参数传递建议使用map类型的原因
- C#-DataTable分页代码
- iTween基础之Color(变换颜色)
- 解决Ubuntu下 Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR)