`

asp.net 2.0 技巧2

    博客分类:
  • .NET
阅读更多

   一个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>
                                                &nbsp;<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 =

分享到:
评论

相关推荐

    Programming Microsoft ASP.NET 2.0 Applications - Advanced Topics

    2. **控件与数据绑定**:书中会详细介绍ASP.NET 2.0的各种服务器控件,如GridView、DetailsView、FormView等,以及如何利用数据绑定机制(例如,DataSource控件)来连接数据库并展示数据。 3. **状态管理**:ASP...

    Wrox Asp.Net 2.0 Instant Results(asp.net2.0经典案例教程)

    2. **数据绑定模型**:ASP.NET 2.0引入了新的数据绑定语法,使得数据源与控件之间的绑定更加灵活和简洁。例如,数据绑定表达式和对象数据源控件的使用,使得动态数据显示变得更加容易。 3. **母版页**:母版页是ASP...

    ASP.NET2.0 经典案例教程

    通过探索这些项目的设计和代码,读者可快速掌握使用ASP.NET 2.0开发应用程序的技巧,从空白开始创建自己的网站,或在自己开发的网站中重用这些项目提供的功能。本书共分为12章,每章通过一个项目实现一种常见的网站...

    精通ASP.NET2.0+SQL Server 2005项目开发 源码.rar

    《精通ASP.NET 2.0 + SQL Server 2005项目开发》是一部深度解析Web应用程序构建的教程,尤其关注ASP.NET 2.0框架与SQL Server 2005数据库系统的结合应用。该资源包含9个精心设计的案例,旨在帮助开发者逐步提升在...

    ASP.NET 2.0网络编程自学手册

    《ASP.NET 2.0网络编程自学手册》是一本针对初学者和有一定基础的开发者设计的全面教程,旨在帮助读者掌握ASP.NET 2.0框架下的网络编程技术。该书涵盖了从基础知识到高级特性的全面内容,是学习ASP.NET 2.0不可或缺...

    ASP.NET2.0网站开发全程解析[收集].pdf

    本书的章节结构清晰,每章都遵循提出问题、分析解决方案、实际操作的模式,使读者能够逐步理解并掌握ASP.NET 2.0的开发技巧。书中的实例代码可以在指定的合作站点上下载,以便读者进行实践和学习。 对于寻求提升ASP...

    asp.net2.0 开发技术大全(光盘)

    "asp.net2.0 开发技术大全"很可能是一本详尽介绍ASP.NET 2.0开发的书籍,包含实例、最佳实践以及常见问题解决方案等内容。 标题中的“开发技术大全”暗示了书的内容可能覆盖了以下几个关键知识点: 1. **基础概念*...

    ASP.NET第一步(基于C#和ASP.NET 2.0).rar

    在探索这些内容时,你可能会遇到诸如页面生命周期的理解、异常处理的技巧、数据库访问的最佳实践等问题,这些都是 ASP.NET 开发者必须掌握的关键点。随着学习的深入,你将能够创建复杂的Web应用程序,并逐步适应ASP...

    ASP.NET 2.0入门与提高系列课程(5):ASP.NET 2.0配置与部署

    总之,这个课程涵盖了ASP.NET 2.0配置和部署的关键方面,对于初学者来说,这是一个很好的起点,可以帮助他们快速掌握在真实环境中运行和管理ASP.NET应用程序的技巧。通过深入学习和实践,开发者可以构建高效、安全且...

    ASP.NET 2.0网页制作彻底研究(程序代码)

    2. **Web控件**:ASP.NET 2.0引入了大量的服务器控件,如TextBox、Button、Label等,它们使得开发者可以像操作Windows桌面应用一样构建Web页面。 3. **视图状态**:视图状态是ASP.NET中一个重要的概念,用于在...

    ASP.NET2.0数据库项目案例导航

    ASP.NET 2.0 是微软开发的一个用于构建Web应用程序的框架,它在.NET Framework 2.0...通过这些案例,学习者不仅能掌握ASP.NET 2.0的基本编程技巧,还能了解到如何将这些技术应用于实际项目中,提升解决复杂问题的能力。

    Asp.net 2.0高级编程

    2. **数据绑定**:ASP.NET 2.0的数据绑定机制允许开发者将数据源(如数据库、XML或对象)与控件直接关联,实现了数据的动态呈现,无需编写大量的代码。 3. **母版页(Master Pages)**:母版页是ASP.NET 2.0中的一...

    asp.net2.0电子商务开发实战

    2. **数据访问技术**:ASP.NET 2.0提供了多种数据访问方式,如ADO.NET、Entity Framework等。书中可能讲解如何与数据库进行交互,包括连接、查询、事务处理和对象关系映射(ORM)。 3. **会员与角色管理**:在电子...

    完全手册ASP.NET2.0网路开发详解(源码)

    《完全手册ASP.NET 2.0 网络开发详解》是一本深入解析ASP.NET 2.0技术的专业书籍,附带的源码文件旨在帮助读者更好地理解和实践书中的理论知识。这本书涵盖了ASP.NET 2.0的核心概念、开发工具、架构设计、页面生命...

    Asp.Net2.0网站开发实战.iso

    通过《Asp.Net2.0网站开发实战》这个教程,学习者可以系统地掌握Asp.Net 2.0的开发技巧,包括页面生命周期、控件编程、数据库操作、安全性实践等多个方面。同时,配合压缩包中的文件"aspxon.com_AspNet20WzKfSzIsO"...

    ASP.NET 2.0快速入门教程(微软MSDN课程)

    ASP.NET 2.0是微软推出的用于构建动态网站、Web应用程序和Web服务的开发框架,它是.NET Framework的重要组成部分。这个快速入门教程旨在帮助初学者和有一定经验的开发者快速掌握ASP.NET 2.0的核心概念和技术。 一、...

    《精通ASP.NET2.0企业级项目开发

    文件名“精通asp.net2.0企业级项目开发”很可能包含了具体的项目实例,涵盖了如电子商务网站、内容管理系统、企业内部应用等多种应用场景。读者可以通过这些案例学习到如何设计数据库、实现业务逻辑、优化性能、处理...

    ASP.NET 2.0网站开发实例教程[电子教案]

    总而言之,"ASP.NET 2.0网站开发实例教程[电子教案]"涵盖了ASP.NET 2.0的关键特性和实际开发技巧,对于希望进入或提升ASP.NET开发技能的学习者来说,是一份非常有价值的参考资料。通过学习这份教程,开发者不仅可以...

    圣殿祭司的ASP.NET 2.0开发详解c# 5

    《圣殿祭司的ASP.NET 2.0开发详解c# 5》是一份深入探讨ASP.NET 2.0框架与C#编程语言结合使用的专业文档。ASP.NET 2.0是微软.NET Framework的重要组成部分,它为Web应用程序的开发提供了强大而高效的工具集。C# 5则是...

    ASP.NET 2.0动态网站开发教程(很实用)

    ASP.NET 2.0是微软推出的用于构建Web应用程序的框架,它是.NET Framework的一部分,提供了丰富的功能和工具,使得开发者能够高效地创建动态、交互式的Web应用。本教程将深入探讨ASP.NET 2.0的核心概念和技术,帮助你...

Global site tag (gtag.js) - Google Analytics