How to get random item from JavaScript array

Hi,

Today I have a requirement in project to retrieve a random value from javascript array. We can do this by using different method.

I have used below code for it.

HTML Button:




Here I have called “ShowArrayItem” function on click event.

Javascript Function:


function ShowArrayItem()
{
    alert(items[Math.floor(Math.random() * items.length)]);
}

Here is the javascript static array; you can define it as per your requirement;


var items = Array('sachin', 'sehwag', 'virat', 'yuvraj', 'raina', 'dhoni', 'jadeja');

When you click on button then it will take random value from the javascript items array.

You can also check it online demo from here, random item from JavaScript array

That’s it.

how to display footer at bottom of page

Hi Friends,

Once in my project, I came across with CSS is dynamic layout. I need to develop footer which stick to the bottom of a page. For example, consider the condition of regardless the height of the content of a page, the footer should stick to the bottom of the page.

When an HTML page contains a small amount of content then footer comes halfway up the page leaving a blank space underneath. This can look bad, particularly on a large screen.

There are several ways to make a footer stick to the bottom of a page using CSS, but the cleanest and most well tested method I have come across is CSS Sticky Footer.

You will need to structure your webpage in the following way in order to manage footer at the bottom of page.

This is your HTML part:

 
Page Content Comes Here ....... Page Content Comes Here .......
Page Content Comes Here ....... Page Content Comes Here .......

Page Content Comes Here ....... Page Content Comes Here .......
Page Content Comes Here ....... Page Content Comes Here .......

Page Content Comes Here ....... Page Content Comes Here .......
Page Content Comes Here ....... Page Content Comes Here .......

Page Content Comes Here ....... Page Content Comes Here .......
Page Content Comes Here ....... Page Content Comes Here .......

This is your CSS part:

html,body {
	margin:0;
	padding:0;
	height:100%;
}
#container {
	min-height:100%;
	position:relative;
}
#header {
	background:#c2dcf4;
	padding:10px;
}
#content {
	padding:10px 10px 80px 10px;
}
#footer {
	background:#ccc;
	width:100%;
	height:80px;
	position:absolute;
	bottom:0;
	left:0;
	padding:0px 10px 0px 10px;
}

The above CSS will keep your footer at the bottom of the page but you will need to make a few tweaks to the code.

You need to make sure that height of your footer and bottom margin of your content should be same. you can see in the above example.

You can see online demo from here, sticky_footer

Hope this will help you !!!.

How to show table cell element as a separate table row element

Hi friends,

Once in my project I want to show table cell element as a separate table row element So I have searched on it and find the solution on same.

Let me share it with you; we can do it by using css style properties

See the below example:



First Second Third Fourth

Here, you can see that there are one table row and four table cell. Now I want to all table cell as a separate table row element.

You can see that I have defined class in table cell i.e. “row”. In this class i have display element as a table-row which fulfill my requirements.

Hope this will also work for you !!!!.

How to download file in asp.net

Hi Friends,

Today I am going to describe file download functionality in asp.net.

we can download file using different methods in asp.net.

Today I am going to describe Response.TransmitFile method of asp.net

Lets see how can we do it


public void Downloadfile(string fileName, string filePath )
{
    FileInfo file = new FileInfo(filePath + "\\" + fileName);
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.TransmitFile(filePath + "\\" + fileName);
    Response.Flush();
    Response.End(); 
}

You can download file easily by using above function.
Note:
1. You need to pass fileName and filePath parameters
2. Here, I have used general content type “application/octet-stream”. Change it as per your requirement
3. Add reference for System.IO

Hope this will help you !!!!

how to call server side button event in jquery dialog

Hi All,

Generally we are using jquery modal dialog for modal popup window in our application.

Sometimes we need to call server side button event from modal popup window and it creates problem for us.

Today I am going to describe how can we call asp.net button event from jquery modal popup window.

lets take an simple example:

Asp.Net Button Code:



Jquery Modal Popup Script:


 

Code Behind Button Event:


protected void Button1_Click(object sender, EventArgs e)
{
    //your button click event code comes here
    //you can do anything here
}

Please note that you need to add jquery modal popup related script reference in your code.

how to send attachment in asp.net mail

Hello Friends,

I have write an article on mail sending functionality but i didn’t add attachment in it.
You can check it from here

Today I am going to describe how to send attachment in mail using asp.net
see the below code for mail attachment functionality:


    MailMessage Msg = new MailMessage();  
    Attachment attchment = new Attachment(physicalfilepathcomeshere);
    Msg.Attachments.Add(attchment);
    Msg.From = new MailAddress("fromemailaddress");  
    Msg.To.Add("toemailaddress");  
    Msg.Subject = "Define your subject line";  
    Msg.Body = "Content of body";  
  
    SmtpClient smtp = new SmtpClient();  
    smtp.Host = "smtp.gmail.com";  
    smtp.Port = 587;  
    smtp.Credentials=new System.Net.NetworkCredential("gmailID","gmailpassword");  
    smtp.EnableSsl = true;  
    smtp.Send(Msg);  

you need to provide physical file path in place of “physicalfilepathcomeshere”.

Hope this will help you in sending mail attachment.

IE11 and __doPostBack is undefined

Hi Friends,

Today I have experienced one strange issue on my project. In my local machine everything is working fine but when I have uploaded it on server then it gives me an error.

I have tested it in other browsers and everything going fine but when I tested it on IE11 then it raise the __dopostback error.

I got one link from microsoft regarding this error http://support.microsoft.com/kb/2836939/en-us.

I have updated .net framework version 4 and it solves my problem.

Hope this will also help you !!!

How to validate alphanumeric password in asp.net

Hi Friends,

In my previous post, we have seen asp.net custom validator example.
Now today I am going to describe asp.net RegularExpression validator control.

I have taken simple example for this validation. you can validate different things by using this control like email validation, special character validation, alphanumeric validation etc.

Lets see how can we validate alphanumeric password with minimum 5 characters long.

Password Textbox:


RegularExpression Validator:


Button:


now when I click on the button then it validates the password textbox whether user enters it as alphanumeric or not

Hope this will help you !!!….

Checkbox custom validation in asp.net

Hello,

In this post I will explain how to validate ASP.Net Checkbox control using ASP.Net Custom Validator and JavaScript.

Javascript:

function CheckboxValidation(sender, args) {
            if (document.getElementById("<%=YourCheckboxControlID.ClientID %>").checked == true) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        } 

Checkbox:


asp:CheckBox ID="YourCheckboxControlID" runat="server" />

Custom Validator:



Button:




Here, You can see the I have used ClientValidationFunction property of asp.net CustomValidator. Which is used to validate control by using javascript function. So when I click on the button then it validates the checkbox whether it is is checked or not.

Hope this will help you !!!….

Difference between settimeout and setinterval in javascript

Hi Frineds,

Most of people are confused in javascript timer functions. I got many questions on this.

So today I am going to describe the differences between setInterval and setTimeout, which are both time interval based code execution methods of the JavaScript language.

How does the JavaScript function setTimeout work?
setTimeout() : It is used to execute a certain function at the end of given time.
below is the syntax for this function:

var timerVariable = setTimeout(func|code, delay)

func|code
    Function variable or the string of code to execute.
delay
    The delay in milliseconds, 1000 milliseconds = 1 second.

The execution will be performed once after the specified delay.

For example, the code below fires an alert after three seconds:

function functionName() { 
  alert('JavaScript setTimeout example')
}
setTimeout(functionName, 3000)

How does the JavaScript function setInterval work?
setInterval() : It repeats itself at regular time that is passed to this function.
below is the syntax for this function:

var timerVariable = setInterval(func|code, delay)

func|code
    Function variable or the string of code to execute.
delay
    The delay in milliseconds, 1000 milliseconds = 1 second.

The execution will be performed continuously every time the number of delay.

For example, the code below fires an alert on every three seconds:

function functionName() { 
  alert('JavaScript setInterval example')
}
setInterval(functionName, 3000)

Now the question is; how can we stop the execution which was started by the setTimeout and setInterval functions.

For this, we can use clearTimeout() and clearInterval() functions.

below is the syntax for this functions:

clearTimeout(timerVariable) AND
clearInterval(timerVariable)

Hope you enjoyed this article !!!