Archive

Archive for November, 2014

Disable F5 keyword using JavaScript in ASP.Net

November 17, 2014 Leave a comment

Hi all,

try this example to Disable F5 keyword using JavaScript in ASP.Net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CloseBrowserTest.aspx.cs" Inherits="CloseBrowserTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">;
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
document.attachEvent("onkeydown", my_onkeydown_handler)
function my_onkeydown_handler()
{
if(event.keyCode == 116)
{
event.keyCode = 0;
event.returnValue = false;
}
}
</script>

</head>
<body onload="return my_onkeydown_handler()">
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

Hope this helps

Good Luck.

Categories: ASP.Net, Javascript

Bind DropdownList from SharePoint List

November 14, 2014 Leave a comment

Hi all,

try this example to Bind DropdownList from SharePoint List

if (!Page.IsPostBack)
{
DataSet ds = new DataSet();
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList list = myWeb.Lists["ListName"];
DataTable DTable_List = list.Items.GetDataTable();
DTable_List.TableName = "MyDTable";
ds.Tables.Add(DTable_List);
DropDownList.DataSource = ds.Tables["MyDTable"];
DropDownList.DataTextField = "FieldName";
DropDownList.DataValueField = "FieldName";
DropDownList.DataBind();
DropDownList.SelectedIndex = 0;
}

Hope this helps

Good Luck

Categories: SharePoint 2010

Captcha Code With Refresh and Validation Using Javascript

November 10, 2014 Leave a comment

Hi all,

try this example to create Captcha code With Refresh and Validation Using JavaScript With ASP.Net

— ASP.Net


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function DrawCaptcha() {
            var a = Math.ceil(Math.random() * 9) + '';
            var b = Math.ceil(Math.random() * 9) + '';
            var c = Math.ceil(Math.random() * 9) + '';
            var d = Math.ceil(Math.random() * 9) + '';
            var e = Math.ceil(Math.random() * 9) + '';
            var code = a + ' ' + b + ' ' + c + ' ' + d + ' ' + e;
            document.getElementById("imgCaptcha1").src = 'images/letters/' + a + '.jpg';
            document.getElementById("imgCaptcha2").src = 'images/letters/' + b + '.jpg';
            document.getElementById("imgCaptcha3").src = 'images/letters/' + c + '.jpg';
            document.getElementById("imgCaptcha4").src = 'images/letters/' + d + '.jpg';
            document.getElementById("imgCaptcha5").src = 'images/letters/' + e + '.jpg';
            document.getElementById("<%= txtCaptcha.ClientID %>").value = code;
        }

        function ValidateLoginForm(sender, args) {
            var str_input = document.getElementById('<%= txbRegCode.ClientID %>').value;
            var str_captcha = document.getElementById('<%= txtCaptcha.ClientID %>').value;
            if (!ValidCaptcha(str_captcha, str_input)) {
                args.IsValid = false;
                document.getElementById("<%= txbRegCode.ClientID %>").focus();
                True = false;
                return false;
            }
            else {
                args.IsValid = true;
                True = true;
                return true;
            }
        }
        // Validate the Entered input aganist the generated security code function   
        function ValidCaptcha(captcha, input) {
            var str1 = removeSpaces(captcha);
            var str2 = removeSpaces(input);
            if (str1 == str2) return true;
            return false;
        }
        // Remove the spaces from the entered and generated code
        function removeSpaces(string) {
            return string.split(' ').join('');
        }
    </script>
</head>
<body onload="DrawCaptcha();">
    <form id="form1" runat="server" >

        <table>
            <tr>
                <td>

                </td>
                <td>

                </td>
            </tr>
            <tr>
                <td>Capatcha Code</td>
                <td>
                    <asp:TextBox ID="txbRegCode" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txbRegCode" Display="Dynamic" 						ErrorMessage="RequiredFieldValidator" SetFocusOnError="True">Enter Code</asp:RequiredFieldValidator>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidateLoginForm" ControlToValidate="txbRegCode" 				ErrorMessage="CustomValidator">In-valid Code</asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2">
               <div id="dlCaptch2">
                        <div dir="rtl">
                            <table width="60px" align="right" height="18px" border="1" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td width="100px" align="center">
                                        <table width="100px" height="18px" border="0" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td>
                                                    <img id="imgCaptcha5" alt="" width="18px" height="18px" src="" border="0px" />
                                                </td>
                                                <td>
                                                    <img id="imgCaptcha4" alt="" width="18px" height="18px" src="" border="0px" />
                                                </td>
                                                <td>
                                                    <img id="imgCaptcha3" alt="" width="18px" height="18px" src="" border="0px" />
                                                </td>
                                                <td>
                                                    <img id="imgCaptcha2" alt="" width="18px" height="18px" src="" border="0px" />
                                                </td>
                                                <td>
                                                    <img id="imgCaptcha1" alt="" width="18px" height="18px" src="" border="0px" />
                                                </td>
                                            </tr>
                                        </table>
                                        
                                        <asp:TextBox ID="txtCaptcha" runat="server" style="display:none" ></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                            <asp:HiddenField ID="CodeValue" runat="server" />
                            <div>
                                &nbsp; <a onclick="DrawCaptcha();" style="cursor: pointer" class="reg-link">Try Another Code </a>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="Button1" runat="server" Text="Send Message" OnClick="Button1_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblMesssage" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

— In Code behind :

 protected void Button1_Click(object sender, EventArgs e)
{
char[] charsToTrim = { ' ', '\t' };
if (txbRegCode.Text.Trim() == txtCaptcha.Text.Replace(&amp;quot; &amp;quot;, String.Empty))
{
lblMesssage.Text = &amp;quot;Valid Code&amp;quot;;
}
else
{
lblMesssage.Text = &amp;quot;Invalid Code&amp;quot;;
}
}

Hope this helps

You can download example from here

Good Luck.

Categories: ASP.Net, Javascript