Saturday, 31 May 2014

How to add Global.asax in Visual Studio 2012

This question and this post talks about Global.asax template missing in Visual Studio 2012. I have a similar problem in Visual Studio 2012. What you can do is download the default template of Global.asax file and add it to the project. Simply download and copy it to the project. Then choose right click “Add Existing”, select the two files and you are done.

Global.asax download

Make sure you change the namespace in this file to what you are using in your project.





That’s it!!…..Happy Programming...

Friday, 30 May 2014

Password and Confirm Password validation in ASP.NET using JAVASCRIPT

If you have a form with a ‘confirm password’ field and you want to match that with a simple Javascript code rather than to use a heavy validation than this solution will helpful for you. In this, 'onkeyup' event was used to match both fields while user entering input. if you want to match it after textbox loses their focus, you have to call it on 'onblur' event, nothing more than this. You have to do something Like this:-

aspx code

<form id="form1" runat="server">
        <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
        <asp:TextBox ID="txtPassword" ClientIDMode="Static" runat="server"></asp:TextBox>
        <asp:Label ID="lblConfirmPassword" runat="server" Text="Confirm Password"></asp:Label>
        <asp:TextBox ID="txtConfirmPassword" ClientIDMode="Static" runat="server" onkeyup="matchPassword();"></asp:TextBox>
    </form>

Javascript function

    <script type="text/javascript">
        function matchPassword() {
          //store both values into variables.
        var password1 = document.getElementById('txtPassword');
        var password2 = document.getElementById('txtConfirmPassword');

          //set color to differentiate both conditions.
        var matchColor = "#66cc66";
        var unmatchColor = "#ff6666";

        if (password1.value.trim() == password2.value.trim()) {
              //if passwords match. 

            password2.style.backgroundColor = matchColor;
        } else {
              //if passwords do not match.
            password2.style.backgroundColor = unmatchColor;
        }
      }
    </script>
matched password output

odd password output

That’s it!!…..Happy Programming...

Tuesday, 27 May 2014

Difference Between Eval() and Bind() in Asp.Net

In this article, i am going to explain difference between Eval() and Bind(). The main difference between the two is Bind must be assigned to a property of server side control (runat="server") while you can assign Eval to server side or client side control. See implementation below:-

Example 1 :- We can do this
      <asp:TemplateField HeaderText="Price">
                  <ItemTemplate>
                      <%# Eval("price") %>
                  </ItemTemplate>
      </asp:TemplateField>

Example 2 :- We can't do this
      <asp:TemplateField HeaderText="Price">
                  <ItemTemplate>
                      <%# Bind("price") %>
                  </ItemTemplate>
      </asp:TemplateField>
Output of Example 2
Example 3 :- We can do this
  <asp:TemplateField HeaderText="Price">
       <ItemTemplate>
            <asp:Label ID="lblPrice" runat="server" Text='<%# Bind("price") %>'></asp:Label>
       </ItemTemplate>
  </asp:TemplateField>
The difference between Example 2 and Example 3 was nothing expect one server side control. As i already told you, Bind must be assigned to a property of server side controls, that's why Example 3 is perfect but Example 2 not.


That’s it!!…..Happy Programming...

Saturday, 24 May 2014

How to align text under each image in CSS

In this article, i am going to give some overview on how to align text under image in CSS.

HTML Code

    <div class="images">
                <ul>
                    <li><span><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a></span>
                        <a href="">See More...</a>
                    </li>
                    <li><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a>
                        <a href="">See More...</a>
                    </li>
                    <li><span><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a></span>
                        <a href="">See More...</a>
                    </li>
                    <li><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a>
                        <a href="">See More...</a>
                    </li>
                    <li><span><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a></span>
                        <a href="">See More...</a>
                    </li>
                </ul>
    </div>


CSS Code

.images {
display:block;
}

.images ul li {
display:inline;
list-style:none;
float:left;
text-align:center;
color:#000;
margin:0 10px 10px 0;
}

.images ul li a img {
border:6px solid #e1d9ec;
border-radius:50%;
width:150px;
height:150px;
}

.images ul li a {
display:block;
margin-bottom:5px;
opacity:0.8;
font-family:Verdana;
font-size:12px;
text-decoration:none;
letter-spacing:1px;
}

.images ul li a:hover {
opacity:1.0;
}

Output



That’s it!!…..Happy Programming...

Friday, 23 May 2014

File upload control with UpdatePanel in ASP.NET

In this article I am explaining a common issue faced by many developers. The issue is how to use FileUpload control in ASP.Net UpdatePanel. I think, two Situations you might face when using it with UpdatePanel.


Situation 1

If the postback is caused by the control which placed inside UpdatePanel, FileUpload is always empty when it has come to the server, regardless whether a file has been selected by user or not.
Example:
       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>

Solution

Thats because the FileuUpload does not retain the file inside the UpdatePanel.Workaround is to set a PostBackTrigger on the button that updates the UpdatePanel.
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="btnSave" />
            </Triggers>
        </asp:UpdatePanel>

Situation 2

FileUpload does not work if it is loaded not on the initial page load but appears only after asynchronous update of the page part. For example, Panel is invisible at the load time and is shown after clicking on btnShow button.
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="btnShow" runat="server" Text="Show" OnClick="btnShow_Click" />
                <asp:Panel ID="pnlUpload" runat="server" Visible="False">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                </asp:Panel>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="btnSave" />
            </Triggers>
        </asp:UpdatePanel>

Solution

Workaround is to add a 'multipart' attribute to your form because during asynchronous postback the form is not updated. That is why it is required to set the form content type explicitly.
<form id="form1" runat="server" enctype="multipart/form-data">
OR
// set it on Page_load
Page.Form.Attributes.Add("enctype", "multipart/form-data");



That’s it!!…..Happy Programming...

Thursday, 22 May 2014

How to Generate Video Thumbnail with Title from url in C# ASP.NET

In this article, i am going to give overview on how to generate thumbnail dynamically. 
aspx page
<asp:Image ID="imgThumbnail" runat="server" />

aspx.cs page
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindThumbnailWithTitle();
        }
    }

    void BindThumbnailWithTitle()
    {
        string videoUrl = "https://www.youtube.com/watch?v=pA9QUnloZtw";
        imgThumbnail.ImageUrl = GenerateThumbnail(videoUrl);
        GenerateTitle(videoUrl);
    }

    // generate and return thumbnail by just getting the video url
    public string GenerateThumbnail(string videoUrl)
    {
        string imgLink = "http://img.youtube.com/vi/"; // Common part of every thumbnail url
        string[] videoLink = videoUrl.Split(new char[] { '=', '&' }); //Split code part from url
        imgLink = imgLink + videoLink[1] + "/1.jpg"; // '/1.jpg' is to choose thumbnail image
        return imgLink;
    }

    private void GenerateTitle(string url)
    {
        if (!string.IsNullOrEmpty(url))
        {
            string content = string.Empty;
            string firstCharacters = url.Substring(0, 3);
            if (firstCharacters.ToLower() == "www")
            {
                url = "http://" + url;
            }

            if (firstCharacters.ToLower() == "htt" || firstCharacters.ToLower() == "www")
            {
                try
                {
                    WebRequest webRequest = WebRequest.Create(url);
                    WebResponse webResponse = webRequest.GetResponse();
                    StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                    content = sr.ReadToEnd();// Content of page

                    //Split the content between the title tag
                    content = content.Substring(content.IndexOf("<title>") + 7);
                    content = content.Substring(0, content.IndexOf("</title>"));

                    //display it by using write method
                    Response.Write(content);
                }

                catch (Exception)
                {
                    content = "Url Provided by you is invalid";
                    Response.Write(content);
                }
            }

            else
            {
                content = "Url Provided by you is invalid";
                Response.Write(content);
                return;
            }
        }
    }


That’s it!!…..Happy Programming...