保存时打印数据

时间:2014-07-08 11:31:17

标签: c# asp.net webforms

我使用的是asp.net(C#)网页表单。我有complaints.aspx页面,它从用户那里获取几个输入,然后在点击SAVE按钮时保存它们。它工作得很好,但现在我遇到了一个问题,我想点击保存按钮打印出提交的记录。它应该从本地打印机打印,但我找不到任何解决方案。

.aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="frmStaffMembers.aspx.cs" Inherits="frmStaffMembers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnUpdate").hide();
            $("#btnDelete").hide();
            $("#btnCancel").hide();
            $("#lblSelectedID-Span").hide();
            $("#txtSelectedID-Span").hide();
            var showHide = $("#HiddenFieldShowHideButtons").val();
            if (showHide == "True") {
                $("#btnUpdate").show();
                $("#btnDelete").show();
                $("#btnCancel").show();
                $("#lblSelectedID-Span").show();
                $("#txtSelectedID-Span").show();
                $("#buttonSearch").hide();
                $("#Save-Span").hide();
                $("#HiddenFieldShowHideButtons").val("False");
            }
            $("#btnUpdate").click(function () {
                $("#ConfirmMsg").text("Are you sure to update this record?");
                $("#Delete-Span").hide();
                $("#lblSelectedID-Span").hide();
                $("#txtSelectedID-Span").hide();
                $("#Save-Span").show();
                $("#buttonSearch").show();
                $("#Update-Span").show();
                $("#btnUpdate").hide();
                $("#btnDelete").hide();
                $("#btnCancel").hide();
                $("#ModalConfirmAction").modal({ show: true })
            });

            $("#btnDelete").click(function () {
                $("#ConfirmMsg").text("Are you sure to delete this record?");
                $("#Update-Span").hide();
                $("#lblSelectedID-Span").hide();
                $("#txtSelectedID-Span").hide();
                $("#Delete-Span").show();
                $("#Save-Span").show();
                $("#buttonSearch").show();
                $("#btnDelete").hide();
                $("#btnUpdate").hide();
                $("#btnCancel").hide();
                $("#ModalConfirmAction").modal({ show: true })
            });

            $("#btnCancel").click(function () {
                $("#btnUpdate").hide();
                $("#btnDelete").hide();
                $("#btnCancel").hide();
                $("#lblSelectedID-Span").hide();
                $("#txtSelectedID-Span").hide();
                $("#buttonSearch").show();
                $("#Save-Span").show();
                ClearTextBoxes();
            });
        });

        function ClearTextBoxes() {
            $("input[type=text]").each(function () {
                $(this).val("");

            });
        }
    </script>


<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

            <div class="widget">
                <div class="widget-header">
                        <div class="title">
                            Staff Members
                        <span id="sectionDetails" class="mini-title" runat="server"> Enter/Edit record </span>
                        </div> <!-- widget title-->
                </div> <!-- widget header -->

                        <div class="widget-body">
                        <div class="row-fluid">
      <!-- cons start -->
                        <div class="shortcut-group">
                           <asp:HiddenField ID="HiddenFieldSetMessage" runat="server" 
                                ClientIDMode="Static"/>
                            <asp:HiddenField ID="HiddenFieldShowMessage" runat="server" 
                                ClientIDMode="Static"/>
                            <asp:HiddenField ID="HiddenFieldShowHideButtons" runat="server" 
                                ClientIDMode="Static" />
     <span id="lblSelectedID-Span"><label id="lblSelectedID" runat="server">Selected ID</label></span>
    <span id="txtSelectedID-Span"><asp:TextBox ID="txtSelectedID" runat="server" ReadOnly="True" 
                                CssClass="textField_width"></asp:TextBox></span>

    &nbsp;
    <label>Member Name</label>
    <asp:TextBox ID="txtMemberName" runat="server" ValidationGroup="GroupCompanies" 
                                CssClass="textField_width"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                                ControlToValidate="txtMemberName" ErrorMessage="Member Name is Required" 
                                ForeColor="Red" ValidationGroup="grpValidationTextboxes">Member Name is Required
    </asp:RequiredFieldValidator>
    <br />
    <label>Member CNIC</label>
    <asp:TextBox ID="txtMemberCNIC" runat="server" CssClass="textField_width"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                                ControlToValidate="txtMemberCNIC" ErrorMessage="Member CNIC is Required" 
                                ForeColor="Red" ValidationGroup="grpValidationTextboxes">Member CNIC is Required
    </asp:RequiredFieldValidator>
    <br />
    <label>ContactNo</label>
    <asp:TextBox ID="txtContactNo" runat="server" CssClass="textField_width"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                                ControlToValidate="txtContactNo" ErrorMessage="Contact No is Required" 
                                ForeColor="Red" ValidationGroup="grpValidationTextboxes">Conatact No is Required
    </asp:RequiredFieldValidator>
    <br />
    <label>Head's Designation</label>
    <asp:DropDownList ID="ddlHeadDesignations" runat="server" width="220px">
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
         ControlToValidate="ddlHeadDesignations" InitialValue="0" ErrorMessage="Selection is Required" 
         ForeColor="Red" ValidationGroup="grpValidationTextboxes">Selection is Required
    </asp:RequiredFieldValidator>
    <br />
    <label>Is Morning?</label>
    <asp:DropDownList ID="ddlMorningEvening" runat="server" width="220px">
        <asp:ListItem Value="-1" Text="-Select-"></asp:ListItem>
        <asp:ListItem Value="1" Text="Yes"></asp:ListItem>
        <asp:ListItem Value="0" Text="No"></asp:ListItem>
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
         ControlToValidate="ddlMorningEvening" InitialValue="-1" ErrorMessage="Selection is Required" 
         ForeColor="Red" ValidationGroup="grpValidationTextboxes">Selection is Required
    </asp:RequiredFieldValidator>
    <br />
    <br/>
    <span id="Save-Span">
    <asp:Button ID="btnSave"  CssClass="btn btn-success" runat="server" Text="Save" 
        onclick="btnSave_Click" ValidationGroup="grpValidationTextboxes" ClientIDMode="Static" />
        </span>
    <input type="button" id="btnUpdate" class="btn btn-success" value="Update"/>&nbsp
    <input type="button" id="btnDelete" class="btn btn-danger" value="Delete"/>&nbsp
    <input type="button" id="btnCancel" class="btn btn-success" value="Cancel" />
    <a href="#myModal" id="buttonSearch" role="button" class="btn btn-success" data-toggle="modal" style="padding: 6px;">Search Records</a>&nbsp;     

                                     </div> <!-- shourtcut group">
                        <!-- cons end -->

                                                </div> <!-- row fluid -->
                    </div> <!-- widget body-->

            </div> <!-- widget-->

        <div class="clearfix"></div>

      <!-- Modal -->

    <div id="myModal" style="width:700px important;" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Search Records</h3>
    </div>

    <div class="modal-body">
     <p>Please enter staff member's name to search relevant record/Press search button with empty field to view all records</p>
     <label>Staff Member</label>
     <asp:TextBox ID="txtStaffMemberNamePopUp" runat="server"></asp:TextBox>
         <asp:Button ID="btnSearchPopup" CssClass="btn btn-success pad_mar" runat="server" Text="Search" 
        onclick="btnSearchPopup_Click" />

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSearchPopup"  EventName = "Click"/>
        <asp:AsyncPostBackTrigger ControlID = "gridviewStaffMembers" EventName="PageIndexChanging" />
            <asp:AsyncPostBackTrigger ControlID="gridviewStaffMembers" 
                EventName="SelectedIndexChanging" />
        </Triggers>
        <ContentTemplate>
          <asp:Label ID="lblMsgPopUp" runat="server" 
            Text="No records found for the specified criteria" Visible="False" 
            ForeColor="Red"></asp:Label>
        <asp:GridView ID="gridviewStaffMembers" CellPadding="20" runat="server" 
        AutoGenerateSelectButton="True" Visible="False"
                                GridLines="None"  
                                AllowPaging="True"  
                                CssClass="mGrid"  
                                PagerStyle-CssClass="pgr"  
                                AlternatingRowStyle-CssClass="alt" 
                                OnPageIndexChanging="gridviewStaffMembers_PageIndexChanging" >
                                <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
                                <FooterStyle BorderWidth="0px" />

                                <PagerStyle CssClass="pgr"></PagerStyle>

                                <RowStyle BorderWidth="0px" CssClass="row" Wrap="True" />
                                <SelectedRowStyle CssClass="selected-row" ForeColor="White" />
        </asp:GridView>


        </ContentTemplate>

        </asp:UpdatePanel>

    </div>

    <div class="modal-footer">
        <asp:Button ID="btnClose" runat="server" Text="Close" CssClass="btn btn-success" data-dismiss="modal" aria-hidden="true"  />
    <asp:Button ID="btnEdit" CssClass="btn btn-success" runat="server" Text="Edit" onclick="btnEdit_Click"/>

    </div>
    </div>

 <!-- this is confirmation modal-->
    <div id="ModalConfirmAction" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="H1">Confirmation</h3>
    </div>
    <div class="modal-body">

     <p id="ConfirmMsg"></p>

    </div>
    <div class="modal-footer">
        <span id="Delete-Span">

           <asp:Button ID="btnDeletePopUp"  CssClass="btn btn-danger" runat="server" 
        Text="Delete" onclick="btnDeletePopUp_Click" />
        </span>
        <span id="Update-Span">
        <asp:Button ID="btnUpdatePopUp" CssClass="btn btn-success" runat="server" Text="Update" 
        onclick="btnUpdatePopUp_Click" />
        </span>
        <asp:Button ID="btnClosePopup" runat="server" Text="Cancel" 
            CssClass="btn btn-success" onclick="btnClosePopup_Click"/>

    </div>
    </div>

</asp:Content>

.cs代码:

 protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            short HeadDesignationID = Convert.ToInt16(ddlHeadDesignations.SelectedValue);
            short IsMorning = Convert.ToInt16(ddlMorningEvening.SelectedValue);
            StaffMembersBizz StaffMembersBizz = new StaffMembersBizz(txtMemberName.Text, txtMemberCNIC.Text, 
                                                    txtContactNo.Text, HeadDesignationID, IsMorning);
            ManageStaffMembers mngStaffMembers = new ManageStaffMembers();
            bool Result = mngStaffMembers.Insert(StaffMembersBizz);

            if (Result == true)
            {

                HiddenFieldSetMessage.Value = "Saved";
                HiddenFieldShowMessage.Value = "True";
                Clear(txtMemberName, txtMemberCNIC, txtContactNo);
            }
            else
            {
                HiddenFieldSetMessage.Value = "RecordAlreadyExists";
                HiddenFieldShowMessage.Value = "True";
            }
        }
        catch (Exception)
        {
            HiddenFieldSetMessage.Value = "NotSaved";
            HiddenFieldShowMessage.Value = "True";
        }
    }

1 个答案:

答案 0 :(得分:2)

我假设你保存后会有某种确认页面?

如果在您的保存事件中如此,您可以添加以下代码,这将自动显示确认页面的打印提示。您不能自己打印客户端,因为它是在最终控制的,但您可以显示打印对话框。

将此服务器端代码添加到Save event:

的末尾
ClientScript.RegisterStartupScript(this.GetType(), "FormLoading", "window.print();", true);

确保提供的页面具有您需要的打印数据并且格式正确。您也可以自定义css以应用仅打印样式。