Archive

Archive for June, 2009

Check all checkoxes in a Repeater

June 28, 2009 1 comment

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

Advertisements
Categories: ASP.Net

Validate DropdownList using Compare Validator

June 24, 2009 1 comment

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

Categories: ASP.Net Tags:

Email Validation with Javascript

June 24, 2009 2 comments

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

Categories: ASP.Net, Javascript Tags:

Display Time with Javascript

June 13, 2009 Leave a comment

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

Categories: Javascript Tags:

Make Menu Control works in IE8

June 10, 2009 1 comment

Hi

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

Hope this helps

Good Luck

Categories: ASP.Net Tags:

Allow user to Enter Numbers only With Regular Expression Validator

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></code><code>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" ValidationExpression=<span style="color: #99cc00;">"^\d+$"</span>></asp:RegularExpressionValidator>

Hope this helps

Good Luck

Categories: ASP.Net

Hide Validator Error Message Using Javascript

June 6, 2009 4 comments

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

Categories: ASP.Net, Javascript