Sunday, February 6, 2011

Creating Simple Poll Vote system using text file in back end.

On Code bind :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int yes;
        int no;
        string filename = Server.MapPath("filesave.txt");
        StreamReader objreader = new StreamReader(filename);
        string line = objreader.ReadLine();
        string[] args= line.Split('|');
        yes = int.Parse(args[0].ToString());
        no = int.Parse(args[1].ToString());
        label1.Text = Convert.ToString(yes + no);
        objreader.Close();
    }
    protected void rdboncheckedchanged(object sender, EventArgs e)
    {
        if (rdbone.Checked)
        {
            getPercentage(0);//yes=0;No=1;
        }
    }
    protected void rdbtwooncheckedchanged(object sender, EventArgs e)
    {
        if (rdbtwo.Checked)
        {
            getPercentage(1);//yes=0;No=1;
        }
    }
 
    public void getPercentage(int value)
    {
        string filename = Server.MapPath("filesave.txt");
        StreamReader objreader = new StreamReader(filename);
        string line = objreader.ReadLine();
        int yes;
        int no;
        string[] args= line.Split('|');
        yes = int.Parse(args[0].ToString());
        no = int.Parse(args[1].ToString());
        double percYes = 0.0;
        double percNo = 0.0;
        if (value == 0)
        {
            yes = yes + 1;
            percYes = 100 * Math.Round((Convert.ToDouble(yes) / Convert.ToDouble((no + yes))), 2);
            percNo = 100 * Math.Round((Convert.ToDouble(no) / Convert.ToDouble((no + yes))), 2);
        }
        else if (value == 1)
        {
            no = no + 1;
            percYes = 100 * Math.Round((Convert.ToDouble(yes) / Convert.ToDouble((no + yes))), 2);
            percNo = 100 * Math.Round((Convert.ToDouble(no) / Convert.ToDouble((no + yes))), 2);
        }
        objreader.Close();
        StreamWriter objWriter = new StreamWriter(filename);
        objWriter.WriteLine(yes + "|" + no);
        objWriter.Close();
        rdbone.Visible = false;
        rdbtwo.Visible = false;
        img1.Visible = true;
        img2.Visible = true;
        label1.Text = Convert.ToString(yes + no);
        img1.Width = Convert.ToInt32(percYes);
        img2.Width = Convert.ToInt32(percNo);
        lbl1.Text = Convert.ToInt32(percYes) + "%";
        lbl2.Text = Convert.ToInt32(percNo) + "%";
    }
}


On .aspx page :

<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
#clsbold
{
font-weight:bold;
font-family:Trebuchet MS
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div  id="clsbold">
    <p>Do you like Asp.net??? </p>
    <table>
    <tr>
    <td style="width: 40px; height: 40px;">Yes
    <br /><br />
    No
    </td>
    <td style="width: 13px; text-align:center; height: 40px; ">:
     <br /><br />
     :
    </td>
    <td style="width: 200px; height: 40px;">
     <asp:RadioButton ID="rdbone" GroupName="yesno" runat="server" OnCheckedChanged="rdboncheckedchanged"
AutoPostBack="true"/>
     <img src="poll.gif" alt="" runat="server" id="img1" height="20" visible="false"/>
     <asp:Label ID="lbl1"  runat="server" ></asp:Label>
     <br />
    <br />
    <asp:RadioButton ID="rdbtwo" GroupName="yesno" runat="server" OnCheckedChanged="rdbtwooncheckedchanged"
AutoPostBack="true"/>
      <img src="poll.gif" alt="" runat="server" id="img2" height="18"  visible="false"/>
      <asp:Label ID="lbl2"  runat="server" ></asp:Label>
    </td>
    </tr>
    </table>
    <div style="height:30px; margin-top:10px;">
    Total Poll Votes: <asp:Label runat="server" ID="label1" Text=""></asp:Label>
        </div>
        </div>
    </form>
</body>
</html>

Saturday, January 15, 2011

Java Script Functions

1. Functions to count the text entered.

Note: Where "bl1" is the asp:lable

<script type="text/javascript">
function count(text)
{
if(text != null)
{
document.getElementById('bl1').innerHTML  = text.length;
}
}
</Script>

2. Validation for US Phone number : Format: (111)-111-1111

<script type="text/javascript">
function numvalidate(text)
{
var test_str = /^([\(]{1}[0-9]{3}[\)]{1}[\-]{1}[0-9]{3}[\-]{1}[0-9]{4})$/
if(test_str.test(text))
{
return true;
}
else
{
document.getElementById('bl1').innerHTML  = "please enter proper value";
return false;
}
}
</script>

3. Text input should be in "alphabets and space"

<script type="text/javascript">
function functions(text)
{
var test_str = /^[a-zA-Z ]+$/;
if(test_str.test(text))
{
return true;
}
else
{
alert("Invalid name");
return false;
}
}
</script>

4. Function to validate SSN:

<script type="text/javascript">
 function checkSsn(ssn)
 {
 var RE_SSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/;
 if (RE_SSN.test(ssn))
 {
  alert("VALID SSN");
 }
 else
 {
  alert("INVALID SSN");
 }
</script>

5. Function to validate userid that is either email Id or user name.

function validatemail(text)
{
var test_str = /^\w+[\w-\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$/
if(text != null)
{
if (text.indexOf("@") == -1)
{
//do nothing
}
else
{
if(test_str.test(text))
{
return true;
}
else
{
window.alert("invalid EmailId");
return false;
}
}
}
else
{
window.alert("enter user name or email id");
return false;
}
}

Thursday, January 6, 2011

Including Captcha in Asp.net Application

Step1: Download MSCaptcha.dll from the Internet. Include it in your bin folder.


Step2: In Web.config file paste these line.


<httpHandlers>
<add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>
</httpHandlers>


Step3:  Place this in aspx page where you want to include Captcha.


<cc1:CaptchaControl ID="ccJoin" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="5" CaptchaHeight="40" CaptchaWidth="150" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240" />

Step4:  Put a textbox somewhere in your page where your user must enter what he sees in your captcha.

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

Step5:  Put this in your .cs file to validate user input.


ccJoin.ValidateCaptcha(txtCap1.Text);
if (!ccJoin.UserValidated)
{
//Inform user that his input was wrong ...

return;

}

divyashree.k