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 !!!

How to hide close button from title bar in jquery UI Dialog

Hi Friends,

Today I have a requirement to hide the jquery ui dialog close button from title bar.

we can do this easily by using jquery css but this will hide globally from our project.

I want to hide it in just one page and other page will show the button when user opens the modal dialog.

I have used below code for above functionality.



    
    
    

Here, I have used jquery function on open event for hiding a close button; see the below line
open: function (event, ui) { $(this).closest(‘.ui-dialog’).find(‘.ui-dialog-titlebar-close’).hide(); }, which fulfill my requirement.

This is html part of my page :


Sample demo of jquery modal popup

click here top open dialog box

Sample content of modal popup window comes her

This is sample jquery modal popup window...

Design this as per your requirement


This is online demo for your help, jquery_title

Hope this will help you !!!

Bind Dropdown in Gridview

Hello All,

I want to share example of bind dropdown in gridview. In Edit mode when you want to show your value in dropdown it will help you.

To add dropdown use following code for gridview.


            
                
                    
                        
                    
                    
                        <%# Eval("BusinessEntityID")%>
                    
                
                
                    
                        Title
                    
                    
                        <%# Eval("Title") %>
                    
                    
                        
                        
                        
                    
                
                
                    
                        First Name
                    
                    
                        <%# Eval("FirstName") %>
                    
                    
                        
                    
                
                
                    
                        Last Name
                    
                    
                        <%# Eval("LastName") %>
                    
                    
                        
                    
                
                
                    
                        Address
                    
                    
                        <%# Eval("AddressLine1") %>
                    
                    
                        
                    
                
                
                    
                        City
                    
                    
                        <%# Eval("City") %>
                    
                    
                        
                    
                
                
                    
                        Postal Code
                    
                    
                        <%# Eval("PostalCode") %>
                    
                    
                        
                    
                
                
            
        

Now, add following code in cs file. We are using Edit event and BindDropDown function to achieve our goal. In Edit mode you can use other objects too like checkbox, radiobutton etc…

protected void gvData_Editing(object sender, GridViewEditEventArgs e)
    {
        gvData.EditIndex = e.NewEditIndex;
        BindData();

        HiddenField hdTitle = (HiddenField)gvData.Rows[e.NewEditIndex].FindControl("hdTitle");
        DropDownList ddlTitle = (DropDownList)gvData.Rows[e.NewEditIndex].FindControl("ddlTitle");

        BindDropDown(ddlTitle);

        if (ddlTitle != null)
            ddlTitle.SelectedValue = hdTitle.Value;
    }

private void BindDropDown(DropDownList ddl)
    {
        ddl.Items.Add(new ListItem("-- Select --", ""));
        ddl.Items.Add(new ListItem("Mr.", "Mr"));
        ddl.Items.Add(new ListItem("Mrs.", "Mrs"));
        ddl.Items.Add(new ListItem("Miss", "Miss"));
        ddl.Items.Add(new ListItem("Dr.", "Dr"));
        ddl.Items.Add(new ListItem("Sr.", "Sr"));
    }

protected void gvData_Updating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = gvData.Rows[e.RowIndex];
        int id = 0;
        int.TryParse(e.Keys["BusinessEntityID"].ToString(), out id);

        TextBox txtFirstName = (TextBox)row.FindControl("txtFirstName");
        TextBox txtLastName = (TextBox)row.FindControl("txtLastName");
        TextBox txtAddressLine1 = (TextBox)row.FindControl("txtAddressLine1");
        TextBox txtCity = (TextBox)row.FindControl("txtCity");
        TextBox txtPostalCode = (TextBox)row.FindControl("txtPostalCode");
        DropDownList ddlTitle = (DropDownList)row.FindControl("ddlTitle");

        DBM.SqlCommandEx sqlCmd = new DBM.SqlCommandEx(
                    String.Format("UPDATE Person.Person" +
                    " SET firstName='{0}', lastName='{1}', title='{3}'" +
                    " WHERE BusinessEntityID = {2}"
                    , txtFirstName.Text, txtLastName.Text, id, ddlTitle.SelectedValue) +

                    String.Format(" UPDATE Person.Address" +
                    " SET addressLine1='{0}', city='{1}', postalCode='{2}'" +
                    " WHERE addressID = (SELECT addressID FROM Person.BusinessEntityAddress WHERE BusinessEntityID = {3})"
                    , txtAddressLine1.Text, txtCity.Text, txtPostalCode.Text, id)

                    );

        DataTable i = sqlCmd.GetDataTable();

        gvData.EditIndex = -1;
        BindData();
    }

Now, all done you can run it and see when you are in edit mode you will see your dropdown with selected value. I am attaching example with this article. Demo

How to get IP address in asp.net

Hello Friends,

Today I am going to represent article on how to get client IP address using asp.net.

We know the asp.net method “Request.UserHostAddress” for retrieving a client IP address.
This method gets the IP address of the current request. It uses the UserHostAddress property in the ASP.NET framework. This is usually the user’s ISP’s IP address, not exactly the user’s machine IP address.

we can retrieve same by using other request server variables i.e HTTP_X_FORWARDED_FOR and REMOTE_ADDR

REMOTE_ADDR : This method is same as UserHostAddress. it returns the IP address of the proxy server/router, not the client machine.

HTTP_X_FORWARDED_FOR : By using this method we can get client IP address if user is behind the proxy server. This variable contains a list of comma separated IP addresses in the format client IP, proxy1 IP, proxy2 IP, …

See the below code for retrieving a client machine IP address:


 var ipAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null
                  && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "")
                 ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
                 : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        if (ipAddress.Contains(","))
            ipAddress.Split(',').First().Trim();

Here you can see that, First I have check HTTP_X_FORWARDED_FOR because if user behind the proxy then we get the client machine IP address otherwise it returns the null.

Hope this will help !!!

Simple html tooltip using css3

Hi Friends,

Sometimes we need to show content details as a tooltip in our project. We can do this by different ways.

Today I am going to show simple html tooltip using css3.

I have just use the title tag of anchor tag for showing the detail content in tooltip.

Lets see how can we implement it;




Define above style in html header section




Here, I have used anchor tag with title attribute which is used for showing the detail content as a tooltip.

You can see the demo from here, css3-tooltip

How do I get human readable file size through javascript

Hi Friends,

Few days ago, I have write blog on human readable file size through c#.

Today I am going to represent same functionality using javascript.

For the above requirement, I have created one general function.

See the below code:

function GetFileSize(size)
{
     var fileUnits = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
     var fuCount = 0;

     while (size >= 1024)
     {
                fuCount++;
                //size /= 1024;
                size = Math.round(size / 1024)
      }

      return size + fileUnits[fuCount];
}

Put above function in your page header section.

you can call it as like below


var filesize = GetFileSize(file.size);
alert(filesize);

Here, I have passed file size as a parameter.