Banner Ad

Wednesday, August 21, 2013

Access ASP.Net Server Control in Client Side Using Javascript

By Francis   Posted at   12:18 PM   Client ID in ASP.Net No comments
              Some time, we have a situation to access the server side ASP.Net controls and its values using Client side script like java script.  In this post I’m going to explain how to do that with a simple example.

A word about Client ID:

              As all of us know, that server side controls are submitted and processed by the ASP.Net engine, which emits the respective HTML to the browser. At that time the ASP.Net engine takes the server controls and provide ID (which can be varied across asp.net versions) for it.

            Consider the below example, which has 3 text boxes, after entered values in first 2 text boxes when the user clicks the button then javascript function called which will add the values and put it in 3rd text box. These controls are Server controls.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function Add() {
            var intFirstNo = document.getElementById("<%= txtFirstNo.ClientID %>").value;
            var intSecondNo = document.getElementById("<%= txtSecondNo.ClientID %>").value;
            var intResult = parseInt(intFirstNo)  +  parseInt(intSecondNo);
            document.getElementById("<%= txtResult.ClientID %>").value = intResult;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table>
<tr>
            <td>
                Enter First Number:
            </td>
            <td>
                <asp:textbox clientidmode="Predictable" id="txtFirstNo" runat="server"></asp:textbox>
            </td>
        </tr>
<tr>
            <td>
                Enter Second Numer:
            </td>
            <td>
                <asp:textbox id="txtSecondNo" runat="server"></asp:textbox>
            </td>
        </tr>
<tr>
            <td>
                Addition is:
            </td>
            <td>
                <asp:textbox id="txtResult" runat="server"></asp:textbox>
            </td>
        </tr>
<tr>
        <td align="center" colspan="2">
        <asp:button id="btnResult" onclientclick="Add(); return false;" runat="server" text="Calculate">
        </asp:button></td>
        </tr>
</table>
</form>
</body>
</html>

Consider this java script line:

var intFirstNo = document.getElementById("<%= txtFirstNo.ClientID %>").value;

                When the above line is read by the ASP.Net engine, it will search the Server control ID “txtFirstNo” and get the respective client ID, which has been assigned by ASP.Net engine.  In other words, ASP.Net get the Client ID for that Server control “txtFirstNo” and substitute there in between the double quotes. As a result when we saw the view source of the page we can read that above one as follows:

var intFirstNo = document.getElementById("txtFirstNo").value;

We also return false (in line no 42) after call the javascript function "Add" to avoid the postback when we click the button.

Just copy the above sample in a webform and try it yourself!

Please leave your valuable comment which improves every Article!

Hope this helps a bit! 

Tuesday, August 20, 2013

Work Around: Break Point Not Hitting in Visual Studio

By Francis   Posted at   11:14 AM   Visual Studio No comments
                In my professional experience with Visual studio sometimes break point doesn’t hit when we attached to the Worker process. It just tells that Symbols are not loaded.

                 The below are the possible workaround to solve this problem (Try it one by one in this case):
  1. Clean and Rebuild the Solution/Project.
  2. Close all the windows using Window -> “Close All Documents” and restart the Visual Studio. Then Build/Rebuild the Solution/Project.
  3. Go to the Temporary Folder (which is available in the .Net Framework installed Location) and clean the Temporary files of that Solution/Project. Kill the worker process and rebuild the solution.
Hope this helps a bit!


Monday, August 19, 2013

Visual Studio Tips & Tricks: 2 – Create Virtual Directory Problem

By Francis   Posted at   12:15 PM   Visual Studio Tips and Tricks No comments
                     Few days ago, I just want to create a virtual directory for my ASP.Net web project. I just go to the “Solution Explorer” and “Right click” on the Web project in the floating menu choose the “Properties” of the Web project.

Project Properties Float Menu

              In the Properties page, Select “Web” Tab and select the option “Use Local IIS web Server” and then click “Create Virtual Directory” button. At that time the below error occurred:

Unable to create the virtual directory. To access local IIS Web sites, you must run Visual Studio in the context of an administrator account.

Virtual Directory Creation Error

Tuesday, August 13, 2013

Learn By Experience: Be Aware: Static (C#)/Shared (VB) Variables

By Francis   Posted at   12:02 PM   Static Variable No comments
                              It’s a common programming mistake all the programmers use the static/shared variables in a class. The programmer cannot identify the problem when it was used in a single user desktop application. But when you come to the web, which is a multi-user environment you will strangely come across problems. For example, one user’s data may be viewed or data may be colloid with other user’s data. 
                       Few months ago, I also encountered the same problem in a project. The main problem is that static/shared variables has been used in that particular code behind file.  So what happened is, if more than one logged in user view and edit data on that page on the same time data has been interchanged or overlapped with other user data.
                        It doesn’t mean that shared/static variables are not to be use. But using inside a class, is need more attention. The above problems are perfect live examples. 
                        We can use the Shared/Static variables in that case we need to track the number of page visits or need to be access across the classes. But be aware such kind of situations.

I hope this post help a little to refine your coding!!


Happy Coding!!

Wednesday, July 31, 2013

ASP.Net MVC - An Introduction

By Francis   Posted at   12:24 PM   MVC 3 comments
What is MVC?
             Several Months ago, I just started to learn new technologies in order to teach other thru a weekly technical session in my current company. So I decide to lean ASP.Net MVC. These points are from my learning.

So what is ASP.Net MVC?
             According to Microsoft, ASP.Net MVC is a new paradigm to design a website using Model, View and Contoller Pattern. The first 3 letters of Model, View and Controller forms the name MVC. However MVC newly introduced in ASP.Net but MVC is not a new framework it has been already used in Java and PHP etc.
Connect with Us