Wednesday, November 26, 2014

Difference between Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\") and Server.MapPath("/")


Server.MapPath specifies the relative or virtual path to map to a physical directory.
  • Server.MapPath(".")1 returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
An example:
Let's say you pointed a web site application (http://www.example.com/) to
C:\Inetpub\wwwroot
and installed your shop application (sub web as virtual directory in IIS, marked as application) in
D:\WebApps\shop
For example, if you call Server.MapPath in following request:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
then:
  • Server.MapPath(".")1 returns D:\WebApps\shop\products
  • Server.MapPath("..") returns D:\WebApps\shop
  • Server.MapPath("~") returns D:\WebApps\shop
  • Server.MapPath("/") returns C:\Inetpub\wwwroot
  • Server.MapPath("/shop") returns D:\WebApps\shop
If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path were a full, virtual path.
If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed.

--reference stackkoverflow article.

Tuesday, December 3, 2013

jQuery fast review

Here I have provided some jQuery properties which will give you overview & to quick learn.

While writing jQuery we need to remember two things i.e  
      #"Find element" .. and
      #"do something to it"..
Find element : find a element by selector..
i.e. $("")  --- selector
jQuery selectors are used to "find" (or select) HTML elements based on their id, classes, types, attributes, values of attributes.

You can select a element by using 
    "element id" using "#"                     -- eg: $("#divid")
    "class attribute" using "." (dot)        -- eg: $(".classname")
    "element tag"  using tag name         -- eg: $("table"), $("div"),..

Examples:
$("#contentid")  -- get element with id "contentid" .
$("li:first")          -- get first list item.
$("tr:odd")         -- get odd numbered table rows
$(this).hide()      -- hides the current element.
$("p").hide()      -- hides all <p> elements.
$(".test").hide()  -- hides all elements with class="test".
$("#test").hide() -- hides the element with id="test".

Properties :
Here are some of properties which are most using..

Hide all div's with jQuery :
$("div").hide( );

You can also find element by string selectors together
$("#myid, .myclass, table")

Add Class:
$("div").addclass("myclass");
here selector find element div and add class attribute "myclass" to it.

jQuery API
Chain method
$("div").addclass("myclass").fadeout();

•Moving Elements:
append(), appendTo(), before(), after(),
•Attributes
css(), attr(), html(), val(), addClass()
•Traversing
find(), is(), prevAll(), next(), hasClass()
•Events
bind(), trigger(), unbind(), live(), click()
•Ajax
get(), getJSON(), post(), ajax(), load()
•Effects
show(), fadeOut(), toggle(), animate()

...post incomplete, updating

Monday, December 2, 2013

Float div position to top when you scroll

In the below code I have explained about Maintaining div tag at top of the screen when scrolling..

IF my current position is greater or equal to the “sticker” position, give the sticker div a class of “stick”.  This changes the CSS of the div to have a FIXED position as long as the viewport is lower than the position of the sticker.





HTML - Take a div tag which contain the data which you want to float.

<div id="sticker">
    ...start scrolling..
   //Add your Code block here...
</div>

CSS 
Styles applied to div#sticker and the class “.stick
"stick" class is most important, which maintain position when you scroll.

div#sticker {
    padding:20px;
    margin:20px 0;
    background:#AAA;
    width:190px;
}
.stick {
    position:fixed;
    top:0px;

}


jQuery – Calculates the position of the sticker div and makes its position fixed if the page has scrolled that far

$(document).ready(function() {
    var s = $("#sticker");
    var pos = s.position();                    
    $(window).scroll(function() {
        var windowpos = $(window).scrollTop();
        s.html("Distance from top:" + pos.top + "<br />Scroll position: " + windowpos);
        if (windowpos >= pos.top) {
            s.addClass("stick");
        } else {
            s.removeClass("stick"); 
        }

    });

Tuesday, September 24, 2013

Typed and UnTyped Dataset


Untyped dataSet you can created in following manner by creating object of DataSet Class.

Dataset has one table with column name firstName.

DataSet ds = new DataSet();

DataTable dt = new DataTable();

DataColumn one = new DataColumn("FirstName");

dt.Columns.Add(one);

ds.Tables.Add(dt);


You can add typed dataset by following manner,

On Solution Explorer, RightClick -> Add New Item then select DataSet.xsd after that from server explorer you can drag the object or you can manuall also create table. and give "Select Command, InsertCommand , UpdateCommand and DeleteCommand", when you call the method, DataSet.AcceptChanges(); , respective command will be executed based on the Rowstate.

Dynamically Enable or Disable Required Field Validator


To enable or disable the required field validator control based on any selection
Enable or Disable ASP.Net Validation on client side

//Syntax:
ValidatorEnable(ValidatorContronName,Boolean);

//Explanation:
ValidatorContronName - This is ClientID of the Validation control.
Boolean - true(Enable) / false(Disable)

//Example:
rfvOther: is a Required Field Validator
ValidatorEnable(document.getElementById('<%=rfvOther.ClientID%>'), false);


Explonation#2:
'ValidatorEnable' or 'ValidatorUpdateDisplay' will immediately validate the associated control and show any validation messages. If this is not wanted because you just want to toggle the enabled/disabled switch but wait until form submission to validate, then the 2nd method of calling enabled on the object is the preferred method. If you do want immediate validation, then this can be done in a single line of code passing in the ID of the RequiredFieldValidator as displayed below:
ValidatorEnable($get('<%=RequiredFieldValidator1.ClientID %>'), true);
However, if you want only to enable/disable the validator, use the code below and do not make any additional calls to the built in JS functions. The error from previous posts states to set the .enable property yet there is no such thing. You must set the .enabled property on the server control. The code below shows this:
var validator = $get('<%=RequiredFieldValidator1.ClientID %>');
validator.enabled = true;

Monday, September 23, 2013

Difference between DataTable and DataView

Simply, DataView is customized view of a DataTable for sorting, filtering, searching, editing, and navigation. The DataView does not store data, but instead represents a connected view of its corresponding DataTable.

when you want to run a query and show the subset of data in a control, a DataView could help you.

DataTable

A datatable is an in-memory representation of a single database table. You can think of it as having columns and rows in the same way. The DataTable is a central object in the ADO.NET library. Other objects that use the DataTable include the DataSet and the DataView.

DataView

A dataview is a view on a datatable, a bit like a sql view. It allows you to filter and sort the rows - often for binding to a windows form control.

Additionally, a DataView can be customized to present a subset of data from the DataTable. This capability allows you to have two controls bound to the same DataTable, but showing different versions of the data. For example, one control may be bound to a DataView showing all of the rows in the table, while a second may be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property which returns the default DataView for the table.

Example:
using(SqlConnection cn = GetConnection())
 {
     cn.Open();
     SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers", cn);
     DataTable dt = new DataTable();
     da.Fill(dt);

     // At this point dt is filled with datarows extracted from the database in no particular order 
     // And the DefaultView presents the same record organization (or lack of), but...

     // Order on the default view by CustomerName
     dt.DefaultView.Sort = "CustomerName";
     foreach(DataRowView rv in dt.DefaultView)
          Console.WriteLine(rv["CustomerName"].ToString();

     // A new dataview with only a certain kind of customers ordered by name
     DataView dvSelectedCust = new DataView(dt, "CreditLevel = 1", "CustomerName", DataViewRowState.Unchanged);
     foreach(DataRowView rv in dvSelectedCust)
          Console.WriteLine(rv["CustomerName"],ToString();



 }

Difference between DataView and DataTable
DataView

1.Read-only i.e., DataView can be used to select the data.
2.Is a reference to an existing DataTable. Cannot be populated from scratch; must be instantiated with a reference to an existing DataTable.
3.Data is a reference to an existing DataTable, and does not consume space.
4.Can sort or filter rows without modifying the underlying data. Rows and columns can be hidden and revealed repeatedly.
5.Can return a DataTable version of the view
6.A live reference to a DataTable; any changes in the DataTable data is immediately reflected in the view.
7.Supports calculated columns, which are columns with a value calculated on the fly by combining or manipulating other columns.
8.Can hide or show selected columns

DataTable

1.Read/Write i.e., Datatable can be used to edit or select or delete or insert a data.
2.Can be created empty and then populated
3.Data takes storage space.
4.Can add/edit/delete rows, columns, and data, and all changes are persistent.
5.Can be cloned
6.Is source data; does not contain references
7.Does not support calculated columns
8.No row or column hiding


Thanks,
Reference from Forums,Sites.google,Stackoverflow.