Friday, October 21, 2011

How to hide/display item of gridview like button or Image



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

         if (e.Row.RowType == DataControlRowType.DataRow)

        {

            // Here Documentname : Is the columan name of gridview

            string docName = DataBinder.Eval(e.Row.DataItem, "Documentname").ToString();



            if (docName.Length == 0)

            {  // Here lnkDetails : is the control embeded in gridview

                ((LinkButton)e.Row.FindControl("lnkDetails")).Visible = false;



            }



        }

    }






Wednesday, August 3, 2011

Javascript for calculating age

<script type="text/javascript">
       function showAge() {
          
           var d = document.getElementById('<%= txtDateOfBirth.ClientID %>').value.split('/');
           var today = new Date();
           var bday = new Date(d[2], d[1], d[0]);
           var by = bday.getFullYear();
           var bm = bday.getMonth() - 1;
           var bd = bday.getDate();
           var age = 0; var dif = bday;
           while (dif <= today) {
               var dif = new Date(by + age, bm, bd);
               age++;
           }
           age += -2;
           document.getElementById("<%=txtCurrentAge.ClientID %>").value = age;
           alert('You are ' + age + ' years old')
       }
</script>

//On page load write this..
this.txtDateOfBirth.Attributes.Add("OnBlur", "javascript:return  showAge();");

Validation on Checkbox

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

function validateCheckBox1(source, args)
        {
            args.IsValid = document.getElementById('<%= Chk1.ClientID %>').checked;
        }
</script>

// call this with custom validator in HTML page

CheckBox ID="Chk1" runat="server" />
                      
<asp:CustomValidator runat="server" ID="CustomValidator4" Visible="true"
ClientValidationFunction="validateCheckBox2"
 ErrorMessage="Please confirm the authorisation 1" Display="Dynamic"  />

Validation in Radiobutton List


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

 function ValidateRadioButtonListSIALenece() {
            var RBL = document.getElementById('<%= RbtnSIA.ClientID %>');
            var radiobuttonlist = RBL.getElementsByTagName("input");
            var counter = 0;
            var atLeast = 1;
            for (var i = 0; i < radiobuttonlist.length; i++) {
                if (radiobuttonlist[i].checked) {

                    if (radiobuttonlist[i].value == "Yes") {
                        // alert('y');
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").value = "";
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").readOnly = false;
                        document.getElementById("<%=Img29.ClientID %>").style.visibility = 'visible';
                        document.getElementById("<%=RegularExpressionValidator10.ClientID %>").readOnly = false;
                    }
                    if (radiobuttonlist[i].value == "No") {
                        // alert('N');
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").value = "";
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").readOnly = true;
                        document.getElementById("<%=Img29.ClientID %>").style.visibility = 'hidden';
                        document.getElementById("<%=RegularExpressionValidator10.ClientID %>").readOnly = true;

                    }

                    counter++;
                }
            }
        }
</script>

I am calling this on radiobutton list click event
this.RBtnList.Attributes.Add("OnClick", "javascript:return  ValidateRadioButtonListSIALenece();");

Saturday, July 30, 2011

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
The above error will be thrown at the time of updating Library or Lists items due to authentication problem, or We didn’t have the rights to update the items.
By using the follwoing code, We can resolve this as,
Web.AllowUnsafeUpdates = true;
item["Title"]=”Sample”;
item.Update();
or we can also use the follwoing code to overcome the above error, this will affect the WebApplication. This will change the Security settings of a Web Application to allow the anonymous user to update item.
web.Site.WebApplication.FormDigestSettings.Enabled = false;
item["Title"]=”Sample”;
item.Update();
web.Site.WebApplication.FormDigestSettings.Enabled = true;
or we can change the settings in Central Administration to anonymously update the items. But this method will be dangerous.
Central Administration > Application Management > Web Application General Settings
Choose the WebApplication to overwrite the settings
Select the option Off in Web Page Security Validation as follows,

Wednesday, June 8, 2011

Date Validation at code behind in c#


        string strDOB = ""; string strDebtType = "";
        txtJudgementdate.Text = hdnJudgementdate.Value;
        txtCostCertificationDate.Text = hdnCostCertificationDate.Value;
        txtDefaultDate.Text = hdnDefaultDate.Value;
        if (ddlDay.SelectedIndex > 0)
            if (ddlMonth.SelectedIndex > 0 && ddlYear.SelectedIndex > 0)
            {
                if (ddlMonth.SelectedIndex == 4 || ddlMonth.SelectedIndex == 6 || ddlMonth.SelectedIndex == 9 || ddlMonth.SelectedIndex == 11)
                {
                    if (ddlDay.SelectedIndex > 30)
                    {
                        lblError.Text = "Please Select Valid Date of Birth.";
                        return;
                    }
                }
                else if (ddlMonth.SelectedIndex == 2)
                {
                    if (Convert.ToInt32(ddlYear.SelectedValue) % 4 == 0)
                    {
                        if (ddlDay.SelectedIndex > 29)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                    else
                    {
                        if (ddlDay.SelectedIndex > 28)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                }
               
                strDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt16(ddlMonth.SelectedIndex), Convert.ToInt16(ddlDay.SelectedValue)).ToString("dd/MM/yyyy");
                //strDOB = DateTime.Parse(ddlDay.SelectedValue + "/" + Convert.ToInt16(ddlMonth.SelectedIndex) + "/" + Convert.ToInt32(ddlYear.SelectedValue)).ToString("dd/MM/yyyy");
            }
            else
            {
                lblError.Text = "Please Select Valid Date of Birth.";
                return;
            }
        else if (ddlMonth.SelectedIndex > 0)
            if (ddlDay.SelectedIndex > 0 && ddlYear.SelectedIndex > 0)
            {
                if (ddlMonth.SelectedIndex == 4 || ddlMonth.SelectedIndex == 6 || ddlMonth.SelectedIndex == 9 || ddlMonth.SelectedIndex == 11)
                {
                    if (ddlDay.SelectedIndex > 30)
                    {
                        lblError.Text = "Please Select Valid Date of Birth.";
                        return;
                    }}
                else if (ddlMonth.SelectedIndex == 2)
                {
                    if (Convert.ToInt32(ddlYear.SelectedValue) % 4 == 0)
                    {
                        if (ddlDay.SelectedIndex > 29)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                    else
                    {
                        if (ddlDay.SelectedIndex > 28)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                }
                strDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt16(ddlMonth.SelectedIndex), Convert.ToInt16(ddlDay.SelectedValue)).ToString("dd/MM/yyyy");
            }
            else
            {
                lblError.Text = "Please Select Valid Date of Birth.";
                return;
            }
        else if (ddlYear.SelectedIndex > 0)
            if (ddlMonth.SelectedIndex > 0 && ddlDay.SelectedIndex > 0)
            {
                if (ddlMonth.SelectedIndex == 4 || ddlMonth.SelectedIndex == 6 || ddlMonth.SelectedIndex == 9 || ddlMonth.SelectedIndex == 11)
                {
                    if (ddlDay.SelectedIndex > 30)
                    {
                        lblError.Text = "Please Select Valid Date of Birth.";
                        return;
                    }}
                else if (ddlMonth.SelectedIndex == 2)
                {
                    if (Convert.ToInt32(ddlYear.SelectedValue) % 4 == 0)
                    {
                        if (ddlDay.SelectedIndex > 29)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                    else
                    {
                        if (ddlDay.SelectedIndex > 28)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                }
                strDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt16(ddlMonth.SelectedIndex), Convert.ToInt16(ddlDay.SelectedValue)).ToString("dd/MM/yyyy");
            }
            else
            {
                lblError.Text = "Please Select Valid Date of Birth.";
                return;
            }