Yasserzaid’s Weblog

June 28, 2009

Check all checkoxes in a Repeater

Filed under: ASP.Net — yasserzaid @ 5:35 am

Hi

try this example to Check all checkoxes in a repeater

<div id=”Repeater1Div”>
 <asp:Repeater ID=”Repeater1″ runat=”server” OnItemDataBound=”Repeater1_ItemDataBound”>
  <HeaderTemplate>
   <table width=”100%” cellpadding=”0″ cellspacing=”0″>
   <tr>
    <td>
     <asp:CheckBox id=”CheckAllCheckBox” Text=”Check All” runat=”server” />
    </td>
   </tr>
   </table>
  </HeaderTemplate>
  <ItemTemplate>
   <table width=”100%” border=”2″ cellpadding=”0″ cellspacing=”0″>
    <tr>
     <td valign=”top”>
      <asp:Label id=”Label1″ Text=’<%# Eval(“SomeDatabaseField”) %>’ runat=”server”></asp:Label>
     </td>
     <td valign=”top”>
      <asp:CheckBox ID=”CheckBox1″ runat=”server” />
     </td>
    </tr>
   </table>
  </ItemTemplate>
 </asp:Repeater>
</div>

<script type=”text/javascript”>
<!–
function checkAllCheckBoxes(elementRef, parentId)
{
 var parentRef = document.getElementById(parentId);
 var inputElementArray = parentRef.getElementsByTagName(‘input’);
 for (var i=0; i<inputElementArray.length; i++)
 {
  var inputElementRef = inputElementArray[i];
  if ( inputElementRef.type == ‘checkbox’ )
  {
   inputElementRef.checked = elementRef.checked;
  }
 }
 return false;
}
function setCheckAllCheckBox(elementRef, checkAllCheckBoxID)
{
 var checkAllCheckBoxRef = document.getElementById(checkAllCheckBoxID);
 if ( elementRef.checked == false )
  checkAllCheckBoxRef.checked = false;
 return false;
}
// –>
</script>

in code behind:

private string g_headerCheckBoxId = string.Empty;

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
 if (e.Item.ItemType == ListItemType.Header)
 {
  CheckBox headerCheckBox = (CheckBox)e.Item.FindControl(“CheckAllCheckBox”);
  if (headerCheckBox != null)
  {
   string containerId = “Repeater1Div”;
   string clickHandler = string.Format(“checkAllCheckBoxes(this, ‘{0}’);”, containerId);
   headerCheckBox.Attributes.Add(“onclick”, clickHandler);
   g_headerCheckBoxId = headerCheckBox.ClientID;
  }
 }
 else if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
 {
  CheckBox checkBoxRef = (CheckBox)e.Item.FindControl(“CheckBox1″);
  if (checkBoxRef != null)
  {
   string clickHandler = string.Format(“setCheckAllCheckBox(this, ‘{0}’);”, g_headerCheckBoxId);
   checkBoxRef.Attributes.Add(“onclick”, clickHandler);
  }
 }
}

Hope this helps

Good Luck

June 24, 2009

Validate DropdownList using Compare Validator

Filed under: ASP.Net — Tags: — yasserzaid @ 7:11 am

Hi

try this example :-

<asp:DropDownList ID=”DropDownList1″ runat=”server”>          
        <asp:ListItem Value=”-1″>Select</asp:ListItem>
        <asp:ListItem>Yasser</asp:ListItem>
        <asp:ListItem>Zaid</asp:ListItem>
    </asp:DropDownList>
    <asp:CompareValidator ID=”CompareValidator1″ runat=”server” ControlToValidate=”DropDownList1″ ValueToCompare=”-1″
    ErrorMessage=”Field is required” Operator=”NotEqual” ></asp:CompareValidator>

Hope this helps

Good Luck

Email Validation with Javascript

Filed under: ASP.Net, Javascript — Tags: — yasserzaid @ 7:06 am

Hi

try this example to validate Email using Javascript

if(document.getElementById(“<%=txtEmail.ClientID %>”).value==”")
      {
                 alert(“Email id can not be blank”);
                document.getElementById(“<%=txtEmail.ClientID %>”).focus();
                return false;
      }
     var emailPat = /^(\”.*\”|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
     var emailid=document.getElementById(“<%=txtEmail.ClientID %>”).value;
     var matchArray = emailid.match(emailPat);
     if (matchArray == null)
    {
               alert(“Your email address seems incorrect. Please try again.”);
               document.getElementById(“<%=txtEmail.ClientID %>”).focus();
               return false;
    }

Hope this helps

Good Luck

June 13, 2009

Display Time with Javascript

Filed under: Javascript — Tags: — yasserzaid @ 1:00 pm

Hi

try this example to display time in format (hh:mm:ss) format using Javascript

<script language=”JavaScript”>
function tick() {
  var hours, minutes, seconds, ap;
  var intHours, intMinutes, intSeconds;
  var today;
  today = new Date();
  intHours = today.getHours();
  intMinutes = today.getMinutes();
  intSeconds = today.getSeconds();

  if (intHours == 0) {
     hours = “12:”;
     ap = “AM”;
  } else if (intHours < 12) {
     hours = intHours+”:”;
     ap = “AM”;
  } else if (intHours == 12) {
     hours = “12:”;
     ap = “PM”;
  } else {
     intHours = intHours – 12
     hours = intHours + “:”;
     ap = “PM”;
  }

  if (intMinutes < 10) {
     minutes = “0″+intMinutes+”:”;
  } else {
     minutes = intMinutes+”:”;
  }

  if (intSeconds < 10) {
     seconds = “0″+intSeconds+” “;
  } else {
     seconds = intSeconds+” “;
  }

  timeString = hours+minutes+seconds+ap;
  Clock.innerHTML = timeString;
  window.setTimeout(“tick();”, 1000);
}

window.onload = tick;

</script>

<div id=”Clock” style=”display: inline; font-size: 13pt;width: 100%; height: 19px; text-align: center”>
      <strong><font color=”#009900″></font></strong>
    </div>

Hope this helps

Good Luck

June 10, 2009

Make Menu Control works in IE8

Filed under: ASP.Net — Tags: — yasserzaid @ 8:43 am

Hi

check the following links to make menu controls works in IE8 here or here

Hope this helps

Good Luck

June 6, 2009

Allow user to Enter Numbers only With Regular Expression Validator

Filed under: ASP.Net — yasserzaid @ 8:30 pm

Hi

try this example :

Only numbers can enter into that Textbox

We can use Regular expression validator for this:
In the validation expression property keep ^\d+$.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>

Hope this helps

Good Luck

Hide Validator Error Message Using Javascript

Filed under: ASP.Net, Javascript — yasserzaid @ 4:44 pm

Hi

try this example to Hide Validator Error Message Using Javascript

<input type=’reset’ onclick=”HideValidators();” />

<script type=’text/javascript’>
function HideValidators()
{
   if (window.Page_Validators)
     for (var vI = 0; vI < Page_Validators.length; vI++)
     {
        var vValidator = Page_Validators[vI];
        vValidator.isvalid = true;
        ValidatorUpdateDisplay(vValidator);
     }
}
</script>

Hope this helps

Good Luck

Use Regular Expression Validator

Filed under: ASP.Net — Tags: — yasserzaid @ 2:13 pm

Hi

try this example to validate user input using Regular Expression Validator Control

1- minimum 6 degits (numbers only) = ^\d{6,}$

2- minimum 6 characters (digits or litters) = ^[a-z]{6,}$

3- minimum 6 digits and chars in both = ^[0-9a-z]{6,}$

Hope this helps

Good Luck

June 3, 2009

Get Month Name from SQL

Filed under: SQL Server — yasserzaid @ 7:46 am

Hi

try this example to get month name from SQL Server

SELECT DATENAME(MONTH,’08/15/1947′)

where date should be in format MM/dd/yyyy

Hope this helps

Good Luck

June 1, 2009

Validate FileUpload Extension

Filed under: ASP.Net, Javascript — yasserzaid @ 6:17 am

Hi

try this example to Validate FileUpload Extension using Javascript

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1″ runat=”server”>
    <title>Untitled Page</title>
    <script type =”text/javascript”>
    var validFiles=["bmp","gif","png","jpg","jpeg"];
        function OnUpload()
        {
          var obj = document.getElementById(“<%=FileUpload1.ClientID%>”);
          var source=obj.value;
          var ext=source.substring(source.lastIndexOf(“.”)+1,source.length).toLowerCase();
          for (var i=0; i<validFiles.length; i++)
          {
            if (validFiles[i]==ext)
                break;
          }
          if (i>=validFiles.length)
          {
            alert(“This not a valid file upload file with an extention of one of the following:\n\n”+validFiles.join(“, “));
            return false;
          }
          return true;
         }
    </script>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:FileUpload ID=”FileUpload1″ runat=”server” />
        <asp:Button ID=”Button1″ runat=”server” Text=”Button” OnClientClick =”return OnUpload();”  />
    </div>
    </form>
</body>
</html>

Hope this helps

Refrence her

Good Luck

Blog at WordPress.com.