December 2011 | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

How to Find Vowels in a String in JavaScript

Wednesday

Find  total number of vowels in a string in JavaScript:
                                                     This article explain how to find Vowels in a String and how to find total number of vowels in a string in JavaScript. I have written simple script for checking vowels in a string in JavaScript. I have used regular expression.





<html>
<head>

    <script>
function findVowels() {

var str=document.getElementById('name').value;  

   var vowelC = 0;
var total_findVowels="";
     for (var i = 0; i < str.length; i++) {

     if (str.charAt(i).match(/[a-zA-Z]/) != null) {

        // findVowels

        if (str.charAt(i).match(/[aeiouAEIOU]/))
  {

 total_findVowels=total_findVowels+str.charAt(i);            
 vowelC++;

        }

     }
   }
    document.getElementById('findVowels').value=total_findVowels;
 document.getElementById('findVowels_count').value=vowelC;
   alert("Total Number of findVowels: " + vowelC);
 

}
    </script>

</head>
<body>
<div style="background-color: skyblue; padding:20px">
    <table border="0" cellpadding=3" cellspacing="3">
        <tr>
            <td>
                Enter Input :
            </td>
            <td>
                <input type='text' value='' id='name' name='name'>
            </td>
        </tr>
        <tr>
            <td>
                Vowels Count :
            </td>
            <td>
                <input type='text' readonly="true" value='' id='findVowels_count' name='findVowels_count'>
            </td>
        </tr>
        <tr>
            <td>
                Vowels :
            </td>
            <td>
                <input type='text' readonly="true" value='' id='findVowels' name='findVowels'>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type='button' value='Check' onclick="javascript:findVowels();">
            </td>
        </tr>
    </table>
    </div>
</body>
</html>
               

C# Get All Files from a Folder

Tuesday

C# Get All Files from a Folder:
                                                This post shows how to get list of file names from a directory (including subdirectories). You can filter the list by specific extension.
To get file names from the specified directory, use static method Directory.Get­Files. Lets have these files and subfolders in "d:\YourDir" folder:

Get files from directory
Method Directory.GetFiles returns string array with files names (full paths).

C# Code
using System.IO;
string[] filePaths = Directory.GetFiles(@"d:\Images\");
// returns:
// "d:\Images\codingcluster.png"
// "d:\Images\home.jpg"


Get files from directory (with specified extension)
You can specify search pattern. You can use wildcard specifiers in the search pattern, e.g. „*.png“ to select files with the extension or „a*“ to select files beginning with letter, a“.

C# Code
string[] filePaths = Directory.GetFiles(@"d:\Images\", "*.png");
// returns:
// "c:\Images\codingcluster.png"

Get files from directory (including all sub directories)
If you want to search also in subfolders use parameter Search Option. A­ll Directories.

C# Code
string[] filePaths = Directory.GetFiles(@"d:\Images\", "*.png",
                                         SearchOption.AllDirectories);
// returns:
// "d:\Images\dcodingcluster.png"
// "d:\Images\Posts\header.png"

Share this post if it is useful to you. Thanks!.

how to delete files in folder and subfolders in C#

c# - how to delete all files and folders in a directory?:
This examples shows how to delete all files (*.*) from a folder in C#.
First, you need to get the list of file names from the specified directory (using static method Directory.Get­Files. Then delete all files from the list.
Delete all files
C# code
using System.IO;
string[] filePaths = Directory.GetFiles(@"d:\Images\");
foreach (string filePath in filePaths)
  File.Delete(filePath);

Delete all files (one-row example)
To delete all files using one code line, you can use Array.ForEach with combination of anonymous method.
C# code
Array.ForEach(Directory.GetFiles(@"d:\Images\"),
              delegate(string path) { File.Delete(path); });
Share this post, if it is useful to you. Thanks!.

Set UITextField Maximum Length in iphone

Wednesday

iPhone SDK: Set Max Character length TextField:
                                                                       The UITextField class has no max length property, it's simple to get this functionality by setting the text field's delegate

Step 1: Implement the UITextFieldDelegate protocol and tag into your text field(s).

Step 2: Add the following line in to top of your .m file

#define MAX_LENGTH 10


Step 3: Implement the following method textField:shouldChangeCharactersInRange:replacementString

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= MAX_LENGTH && range.length == 0)
    {
        return NO; // return NO to not change text
    }
    else
    {
     return YES;
    }
}

Iphone- Make a UITextField move up when keyboard is present

Tuesday


UITextField: move view when keyboard appears
                                                                           For showing the text fields without being hidden by the keyboard, the standard way is to move up/down the view having text fields whenever the keyboard is shown.
               
          
The following simple steps is used to move up/down the UITextField in your iPhone.

Step 1: Set the UITextFieldDelegates, and tag the textFields (in your .h file)

Step 2: Place "CGFloat animatedDistance;" in to inside the interface (in your .h file)

Step 3:  Define the following values in top of your .m file (after @implimentation)    

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

Step 4: And finally add the following code into your .m file         
 
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;

if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = animatedDistance;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self.view setFrame:viewFrame];

[UIView commitAnimations];
}

(void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self.view setFrame:viewFrame];

[UIView commitAnimations];
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

if (theTextField) {
[theTextField resignFirstResponder];
// Invoke the method that does whatever
}
return NO;
}
Please share this post. If it's useful to you.

Send Email Attachments from a Form using Asp.Net and C#

Monday

how to send mail with attachment in asp.net using c#:
                                                                         In this tutorial you will learn how to send attachment with email in asp.net using c#. To send an email with attachments, the ASP.NET process (or the ASP.NET impersonated account) will need permission to read the file, and attach it to the MailMessage class. You can attach a  file using FileUpload Control and put the file in memory stream. In this example I'm using smtp.gmail.com as my SMTP server.  You can put your gmail login credentials and send mail with attachment.

In .aspx file

 <table style="padding-left: 10px; background-color: #4B8DF8; color: #fff; font-weight: bold;">
        <tr>
            <td colspan="2">
                <span class="primary"><b>Mail With Attachment:</b></span>
            </td>
        </tr>
        <tr>
            <td style="width: 30%">
                From :
            </td>
            <td>
                <asp:TextBox ID="txtFrom" SkinID="textbox_larger" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                To :
            </td>
            <td>
                <asp:TextBox ID="txtTo" SkinID="textbox_larger" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Subject :
            </td>
            <td>
                <asp:TextBox ID="txtSubject" SkinID="textbox_larger" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Message :
            </td>
            <td>
                <asp:TextBox ID="txtMessage" TextMode="MultiLine" SkinID="textbox_multiline_smaller"
                    runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Attach :
            </td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td align="left">
                <asp:Button ID="btnSendMail" runat="server" SkinID="button_primary" OnClick="btnSendMailWithAttachment_Click"
                    Text="Send Mail"></asp:Button>
            </td>
        </tr>
    </table>

Sample screen:


                                     

In .aspx.cs file 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSendMailWithAttachment_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtTo.Text);
        mail.From = new MailAddress(txtFrom.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtMessage.Text;
        mail.IsBodyHtml = true;

        //Attach file using FileUpload Control and put the file in memory stream
        if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("Your gmail id", "your gmail password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);

    }
       
}

Please share this post if it's useful to you. Thanks!. 

'System.Array' does not contain a definition for 'Count'

'System.Array' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)


To resolve this server side error you just add the following namespaces,

 using System.Linq; 
at the top of your source and make sure you've got a reference to the System.Core assembly.



Convert Text to Uppercase Using JavaScript

Thursday

Converting all text in textbox into UPPERCASE / lowercase:
                                                                Converting a text string to uppercase is very easy using the JavaScript toUpperCase() method and in the same way the toLowerCase() method is used to converting a text string to lowercase. The following code and example shows how this works:
Example:




Code:

<html>
<head>
<title>Convert String</title>
<script type="text/javascript" language="javascript">
function changeCase(){
    var txt = document.getElementById("txtChangeText").value;
    var str = new String(txt);
    var changeTo = str.toUpperCase();
    document.getElementById("spnChangedCase").innerHTML = changeTo;
    // var changeTo = str.toLowerCase();//Remove the comment line to convert lowercase
    //document.getElementById("spnChangedCase").innerHTML = changeTo;
}
</script>
</head>
<body>
<div style="background-color: #A5C6FE; padding: 20px">
Type to change UPPERCASE:<input id="txtChangeText" type="text" size="20" onkeypress="changeCase();" />
<span style="color: Red; font-weight: bold" id="spnChangedCase"></span>
</div>
</body>
</html>
Try the demo:

Type to change UPPERCASE:

c# - Enabling And Disabling Buttons In GridView

Monday

                                                              In this article  a GridView that has a number of buttons that you need to enable or disable all at once and don't want to have to force a post back on your users in order to loop through them all. By using only a simple js function.

When a GridView renders on the page, it is simply an HTML table and you can traverse it just like you would any other HTML table using JavaScript.

The following code to do a basic version of this in order to enable or disable all buttons in a GridView. First the code gets an instance of the GridView using the ClientID of the GridView, then create a collection of all input controls in the GridView. Next it loops through each input control and checks the type (this is necessary because input controls include not only submit buttons but also things like textboxes). Finally we either enable or disable the submit button.

Code:
<script type="text/javascript" language="javascript">
function disableButtons() 
{
    var gridViewID = "<%=gvReporter.ClientID %>";
    var gridView = document.getElementById(gridViewID);
    var gridViewControls = gridView.getElementsByTagName("input");

    for (i = 0; i < gridViewControls.length; i++) 
    {
        // if this input type is button, disable
        if (gridViewControls[i].type == "submit") 
        {
            gridViewControls[i].disabled = true;
        }
    }
}

function enableButtons() {
    var gridViewID = "<%=gvReporter.ClientID %>";
    var gridView = document.getElementById(gridViewID);
    var gridViewControls = gridView.getElementsByTagName("input");

    for (i = 0; i < gridViewControls.length; i++) {
        // if this input type is button, disable
        if (gridViewControls[i].type == "submit") 
        {
            gridViewControls[i].disabled = false;
        }
    }
}
</script>

How to change font size of a row in tableview

Thursday

Change font size of a row in tableview:
                                       Copy and paste the following code into your  tableView's cellForRowAtIndexPath  method to change the font size of  a row in  a table view and set the number of line to display in a row.

 Code:
//Setup the cell...
cell.textLabel.font = [UIFont systemFontOfSize:12]; //Change this value to adjust size
cell.textLabel.numberOfLines = 3; //Change this value to show more or less lines.
cell.textLabel.text = @"Your Text";

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets