0

I am using asp.net(C#) web forms. I have complaints.aspx page which takes several inputs from users and then saves them upon clicking SAVE button. It's working perfectly but now I am stuck with one problem, I want to make a printout of the submitted record upon clicking the save button. It should be printed from the local printer but I couldn't find any solutions for it.

.aspx code:

<%@ 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 code:

 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";
        }
    }
pixelmeow
  • 654
  • 1
  • 9
  • 31
user3518032
  • 423
  • 8
  • 25
  • This may give you a direction: http://stackoverflow.com/questions/17148484/printing-contents-of-string-variable-to-local-printer-asp-net – FeliceM Jul 08 '14 at 12:24

1 Answers1

2

I assume after you save, there is some sort of confirm page?

If so in your Save event, you could add the following code which will bring up the print prompt for the confirmation page automatically. You can't make the client print on your own because that is controlled on their end but you can make the print dialog appear.

Add this server side code to the end of you Save event:

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

Make sure that page that is served up has the print data that you need and is formatted correctly. You can customize the css to apply a print only style as well.

Kelsey
  • 47,246
  • 16
  • 124
  • 162