Archive

Archive for August, 2013

Client Side Validate CheckBoxList using Custom Validator in ASP.Net

August 20, 2013 Leave a comment

Hi all,

try this example to Validate CheckBoxList using Custom Validator Control

1) Open VS 2008 and create a new web site and add new web page

2) From ToolBox Drag and Drop CheckBoxList , CustomValidator and Button Control into your page


<script type="text/javascript">
 function ValidateModuleList(source, args) {
 var chkListModules = document.getElementById('<%= chkModuleList.ClientID %>');
 var chkListinputs = chkListModules.getElementsByTagName("input");
 for (var i = 0; i < chkListinputs.length; i++) {
 if (chkListinputs[i].checked) {
 args.IsValid = true;
 return;
 }
 }
 args.IsValid = false;
 }
</script>

<asp:checkboxlist id="chkModuleList" runat="server">
 <asp:listitem text="Red" value="1"></asp:listitem>
 <asp:listitem text="Green" value="2"></asp:listitem>
 <asp:listitem text="Blue" value="3"></asp:listitem>
 <asp:listitem text="Yellow" value="4"></asp:listitem>
 <asp:listitem text="Orange" value="5"></asp:listitem>
 <asp:listitem text="Black" value="6"></asp:listitem>
</asp:checkboxlist>
<asp:customvalidator runat="server" forecolor="Red" id="cvmodulelist"
clientvalidationfunction="ValidateModuleList"
errormessage="Please select atleast one Color."></asp:customvalidator>

Checkboxlist-Validation-CodingFusion

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript

How to Delete Membership Role

August 15, 2013 Leave a comment

Hi all,

try this example to delete membership role from Code

First we need to check if there is any user assigned to that role we want to delete


//Check if there are any users within your Role
if(Role.GetUsersInRole("YourRole").Count() <= 0)
{
 //If not - delete it
 Role.DeleteRole("YourRole");
}

Hope this helps

Good Luck.

Categories: ASP.Net

Auto UnLock User using Membership

August 6, 2013 Leave a comment

Hi all,

try this example for Unlocking Users Automatically Since we obviously don’t want our users locked out forever, and most likely we don’t want to manually unlock their accounts every time something goes wrong we need to implement some code to unlock their accounts after a set time period. In this case, I am going to lock the user out for a number of minutes equal to the password attempt window starting immediately when they are locked out.

1) Open VS 2008 and create a new web site and add new web Page

2) From ToolBox drag and Drop Login control Page

3) Right click the Login Control and select Properties, In the Properties window click the Events icon

the following code to the LoggingIn event method:


//Check to see if the current user exists
if (Membership.GetUser(Login1.UserName) != null)
{
 //Check to see if the user is currently locked out
 if (Membership.GetUser(Login1.UserName).IsLockedOut)
 {
 //Get the last lockout date from the user
 DateTime lastLockout = Membership.GetUser(Login1.UserName).LastLockoutDate;
 //Calculate the time the user should be unlocked
 DateTime unlockDate = lastLockout.AddMinutes(Membership.PasswordAttemptWindow);

 //Check to see if it is time to unlock the user
 if (DateTime.Now > unlockDate)
 Membership.GetUser(Login1.UserName).UnlockUser();
 }
}

Hope this helps

Good Luck.

Categories: ASP.Net

Validate Age when Date is selected in ASP.Net AJAX CalendarExtender Control

August 1, 2013 Leave a comment

Hi all,

try this example to Validate Age when Birth Date is selected in ASP.Net AJAX Calendar Extender Control

1) Open VS 2008 and create a new website

2) Add new web page and from ToolBox drag and drop Script Manager, TextBox and AJAX Calender Controls

For Example we want to stop user if his/her Age is less than 20 years


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" />
 <asp:TextBox ID="txtDate" runat="server" />
 <asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/Images/Calendar.png" />
 <cc1:CalendarExtender ID="CalendarExtender1" OnClientDateSelectionChanged="DateSelectionChanged"
 runat="server" TargetControlID="txtDate" PopupButtonID="imgPopup" />
 <script type="text/javascript">
 function DateSelectionChanged(e) {
 var today = new Date();
 var dob = e.get_selectedDate();
 var months = (today.getMonth() - dob.getMonth() + (12 * (today.getFullYear() - dob.getFullYear())));
 var age = Math.round(months / 12);
 if (age < 20) {
 alert("Error Age is less than 20");
 }
 }
 </script>
 </form>
</body>
</html>

Hope this helps

Good Luck.

Categories: AJAX, ASP.Net, Javascript