Thursday, 10 July 2014

javascript GridView , header check box checked effect to client check boxes

function SelectheaderCheckboxes(headerchk) {
            // debugger;
            var gvcheck = document.getElementById('<% =GridView1.ClientID%>');
            var i;
            //Condition to check header checkbox selected or not if that is true checked all checkboxes
            if (headerchk.checked) {
                for (i = 0; i < gvcheck.rows.length; i++) {
                    var inputs = gvcheck.rows[i].getElementsByTagName('input');
                    if (inputs != null && inputs.length != 0)
                        inputs[0].checked = true;
                }
            }
            //if condition fails uncheck all checkboxes in gridview
            else {
                for (i = 0; i < gvcheck.rows.length; i++) {
                    var inputs = gvcheck.rows[i].getElementsByTagName('input');
                    if (inputs != null && inputs.length != 0)
                        inputs[0].checked = false;
                }
            }
        }
        //function to check header checkbox based on child checkboxes condition
        function Selectchildcheckboxes(header) {
            var ck = header;
            var count = 0;
            var gvcheck = document.getElementById('<% =GridView1.ClientID%>');
            var headerchk = document.getElementById(header);
            var rowcount = gvcheck.rows.length;
            //By using this for loop we will count how many checkboxes has checked
            for (i = 1; i < gvcheck.rows.length; i++) {
                var inputs = gvcheck.rows[i].getElementsByTagName('input');
                if (inputs != null && inputs.length != 0)
                    if (inputs[0].checked) {
                    count++;
                }
            }
            //Condition to check all the checkboxes selected or not
            if (count == rowcount - 1) {
                headerchk.checked = true;
            }
            else {
                headerchk.checked = false;
            }
        }


gridview design


 <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" ShowFooter="false"
                                DataKeyNames="Employee_ID"  AllowPaging="false"  CssClass="gridstyle" OnRowDataBound="GridView1_OnRowDataBound"
                                HorizontalAlign="Center" Width="80%" EmptyDataText="Record Not found!">
                                <RowStyle HorizontalAlign="Center" CssClass="even" />
                                <HeaderStyle CssClass="header-cell" />
                                <AlternatingRowStyle HorizontalAlign="Center" CssClass="odd" />
                                <Columns>
                                    <asp:TemplateField HeaderText="S.No.">
                                        <ItemTemplate>
                                            <%#Container.DataItemIndex+1 %>
                                        </ItemTemplate>
                                        <HeaderStyle CssClass="header-cell" />
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderTemplate>
                                        <center>
                                            <asp:CheckBox ID="chAllEmp" AutoPostBack="true" onclick="javascript:SelectheaderCheckboxes(this)"
                                                Checked="true" runat="server" /></center>
                                        </HeaderTemplate>
                                        <HeaderStyle CssClass="header-cell" />
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chEmp" AutoPostBack="true" onClick="javascript:SelectheaderCheckboxes(headerchk)"
                                                Checked="true" runat="server" />
                                        </ItemTemplate>
                                        <HeaderStyle CssClass="header-cell" />
                                    </asp:TemplateField>
                                     <asp:TemplateField >
                                        <HeaderTemplate>
                                           <center >Employee Code</center>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label runat="server" ID="labEmployee_code" Visible="true" Text='<%#Bind("Employee_code") %>'></asp:Label>
                                       
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField >
                                        <HeaderTemplate>
                                           <center >Employee Name</center>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label runat="server" ID="Label1" Visible="true" Text='<%#Bind("Employee_Name") %>'></asp:Label>
                                            <asp:Label runat="server" ID="labempid" Visible="false" Text='<%#Bind("Employee_ID") %>'></asp:Label>
                                                     <asp:Label runat="server" ID="labrankid" Visible="false" Text='<%#Bind("Rank_ID") %>'></asp:Label>
                                                              <asp:Label runat="server" ID="lanLocation" Visible="false" Text='<%#Bind("Loc_Code") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderTemplate>
                                          <center >   Days</center>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="txtAmount" Text="0" onkeypress="return numeralsOnly(event)" Width="100px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>

Wednesday, 25 June 2014

java script open new page

function customOpen(url) {
            var w = window.open(url, '', 'width=1000,height=600,toolbar=0,status=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1');
            w.focus();

        }


 String url = "Sun_GL_Transection.aspx?key=" + xEmpId + "&Sid=" + Xservceid + "&Did=" + Xwebservice_dtl_id;
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "newpage", "customOpen('" + url + "');", true);

Wednesday, 4 June 2014

JavaScript Validation for DecimalWithTwoPlaces

function DecimalWithTwoPlaces(control) {
          
            // livingyearsinrajasthan(control);
            var s = document.getElementById(control).value;
            if (s != "") {
                var d = s.split(".");
                var c = d.length;
                if (c > 2) {
                    s = d[0] + "." + d[1];
                }

                if (s.indexOf(".") > -1) {
                    if (d[0] == "") {
                        s = 0 + "." + d[1];
                        document.getElementById(control).value =s;
                    }
                    if (d[1].length == 0) {
                        s = s + "00";
                        document.getElementById(control).value = s;
                    }
                    if (d[1].length == 1) {
                        s = s + "0";
                        document.getElementById(control).value = s;
                    }
                    else if (d[1].length > 2) {

                        d[1] = d[1].substring(0, 2);
                        s = d[0] + "." + d[1];
                        document.getElementById(control).value = s;
                    }
                }
                else {
                    s = s + ".00";
                    document.getElementById(control).value = s;
                }
            }
            document.getElementById(control).value = s;
        }
ex to call:
onkeypress="return DecimalWithTwoPlaces(controlName)"

JavaScript Validation for Integer

<script language="javascript" type="text/javascript">

        function numeralsOnly(e) {
            var keycode;

            if (window.event) {
                keycode = window.event.keyCode; //alert('IE');
            }
            else if (e) {
                keycode = e.which; //alert('FF');
            }

            var k = parseInt(keycode);
            if (k > 47 && k < 58 || k == 13) {
                e.returnValue = true;
                return true;
            }
            e.returnValue = false;
            return false;
        }       
    </script>
onkeypress="return numeralsOnly(event)"
[10:47:25 AM] Vivek Sahu: onkeypress="return numeralsOnly(event)"

Thursday, 29 May 2014

Datetime problem solve

Date time Format solution

//in page load
txtAnsweredOn.Attributes.Add("data-date-format", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToString().Replace("M", "m").Replace("yyyy", "yy"));

//in Bind Detail
txtAnsweredOn.Text = ((DateTime)dataReader["AnsweredOn"]).ToString("MM/dd/yyyy");


//in Submit Button
sqlCommand.Parameters.Add(new SqlParameter("@AnsweredOn", Convert.ToDateTime(txtAnsweredOn.Text).ToString("MM/dd/yyyy")));

Wednesday, 21 May 2014

Enum Entity Declaration

 public partial class Facility
    {
        public FacilityTypes FacilityType
        {
            get { return (FacilityTypes)this.FacilityTypeId; }
            set { this.FacilityTypeId = (int)value; }
        }
    }

    public enum FacilityTypes
    {
        Hostel = 1,
        Room,
        Area
    }

 FacilityTypes myVar = FacilityTypes.Hostel;
            int med = (int)FacilityTypes.Hostel;

Tuesday, 20 May 2014

GridView remove all cells but one(footer)

if (e.Row.RowType == DataControlRowType.Footer)
            {
                //remove all cells but one, so GridView1.Columns.Count-1
                for (int i = 0; i < grvLeaveDetail.Columns.Count - 1; i++)
                    e.Row.Cells.RemoveAt(0);

                e.Row.Cells[0].ColumnSpan = grvLeaveDetail.Columns.Count;
                e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;

                if (ViewStateLeaveDetail.Count == 0)
                    e.Row.Cells[0].Text = "You have to apply at least one leave";
                else
                    e.Row.Cells[0].Text = "You have applied " + DaysCount + " leave";
            }