asp.net 2.0 技巧2

来源:互联网 发布:ecs windows 编辑:程序博客网 时间:2024/05/01 18:50

   一个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 == 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 = "已经成功添加资料...";
        }
    }