Monday, January 31, 2011

How to check file Exists or not in asp.net

FileExists.aspx 
<%@ Page Language="C#" %>  
<%@ Import Namespace="System.IO" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e) {  
        System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("Text.Txt"));  
        Label1.Text = "Your File Exists?: " +  
            file.Exists.ToString();  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>FileInfo class example: how to check file Exists or not in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Crimson"></asp:Label>  
    </div>  
    </form>  
</body>  
</html>  


Thursday, January 13, 2011

To set change ScrollContents property programmatically in CollapsiblePanelExtender

CollapsiblePanelExtender




<%@ Page Language="C#" AutoEventWireup="true" %>


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        CollapsiblePanelExtender1.ScrollContents = true;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Ajax CollapsiblePanelExtender - How to set change ScrollContents property programmatically in CollapsiblePanelExtender</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DodgerBlue; font-style:italic;">
            Ajax CollapsiblePanelExtender - How to set change
            <br /> ScrollContents property programmatically in
            <br /> CollapsiblePanelExtender in asp.net
        </h2>
        <hr width="500" align="left" color="SkyBlue" />
        <asp:ScriptManager
            ID="ScriptManager1"
            runat="server"
            >
        </asp:ScriptManager>
        <cc1:CollapsiblePanelExtender
            ID="CollapsiblePanelExtender1"
            runat="server"
            TargetControlID="Panel2"
            ExpandControlID="Panel1"
            CollapseControlID="Panel1"
            TextLabelID="Label1"
            ExpandedText="-"
            CollapsedText="+"
            Collapsed="true"
            ExpandedSize="250"
            >
        </cc1:CollapsiblePanelExtender>
        <asp:Label
            ID="Label1"
            runat="server"
            ForeColor="DodgerBlue"
            Font-Italic="false"
            Font-Names="Comic Sans MS"
            Font-Size="XX-Large"
            >
        </asp:Label>
        <asp:Panel
            ID="Panel1"
            runat="server"
            Width="100"
            BackColor="Salmon"
            BorderColor="DarkRed"
            BorderWidth="2"
            HorizontalAlign="Center"
            BorderStyle="Solid"
            >
            <asp:Image
                ID="Image1"
                runat="server"
                ImageUrl="~/Image/Hamster.jpg"
                Height="75"
                />
        </asp:Panel>
        <asp:Panel
            ID="Panel2"
            runat="server"
            Width="350"
            BorderColor="Purple"
            BorderWidth="2"
            BackColor="RosyBrown"
            HorizontalAlign="Center"
            >
            <asp:Image
                ID="Image2"
                runat="server"
                ImageUrl="~/Image/Hamster.jpg"
                Height="325"
                />
        </asp:Panel>
    </div>
    </form>
</body>
</html>

In AJAX AutoCompleteExtender TextBox CompletionList Width

Progress Image in AutoComplete TextBox


<head runat="server">
    <title>Progress Image in AutoComplete TextBox</title>
<style>
        .AutoExtender
        {
            font-family: Verdana, Helvetica, sans-serif;
            font-size: .8em;
            font-weight: normal;
            border: solid 1px #006699;
            line-height: 20px;
            padding: 10px;
            background-color: White;
            margin-left:10px;
        }
        .AutoExtenderList
        {
            border-bottom: dotted 1px #006699;
            cursor: pointer;
            color: Maroon;
        }
        .AutoExtenderHighlight
        {
            color: White;
            background-color: #006699;
            cursor: pointer;
        }
        #divwidth
        {
          width: 150px !important;    
        }
        #divwidth div
       {
        width: 150px !important;   
       }
 </style>
</head>

 Put a div with id "divwidth" above the html source of autocomplete extender.

<div ID="divwidth"></div>
and add this line in autocomplete extender HTML source 
CompletionListElementID="divwidth"
The complete html source of AutoComplete Extender 

<asp:TextBox ID="txtAutoComplete" runat="server" Width="252px">
</asp:TextBox>   
<div ID="divwidth"></div>
<ajaxToolkit:AutoCompleteExtender runat="server" 
             ID="AutoComplete1"
             BehaviorID="autoComplete"
             TargetControlID="txtAutoComplete"
             ServicePath="AutoComplete.asmx" 
             ServiceMethod="GetCompletionList"
             MinimumPrefixLength="1" 
             CompletionInterval="10"
             EnableCaching="true"
             CompletionSetCount="12"
             CompletionListCssClass="AutoExtender"
             CompletionListItemCssClass="AutoExtenderList"
             CompletionListHighlightedItemCssClass
             ="AutoExtenderHighlight"
             CompletionListElementID="divwidth">
<ajaxToolkit:AutoCompleteExtender>

Wednesday, January 12, 2011

Storing Data in Veiw State ::

ViewStateDataStore.aspx





<%@ Page Language="C#" %>  

<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
        ViewState["Name"] = TextBox1.Text.ToString();  
        Label1.Text = "Your name saved in ViewState.";  
    }  
    protected void Button2_Click(object sender, System.EventArgs e)  
    {  
        string name = (string)ViewState["Name"];  
          
        Label1.Text = "Hi " + name + "! we found your name in ViewState.";  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>asp.net ViewState example: how to store (save) data in ViewState</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Red">asp.net ViewState example: data store</h2>  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            ForeColor="Teal"  
            >  
        </asp:Label>  
        <br /><br />  
        <asp:Label ID="Label2" runat="server" Text="Your Name" ForeColor="Green">  
        </asp:Label>  
        <asp:TextBox ID="TextBox1" runat="server" BackColor="Green" ForeColor="AliceBlue">  
        </asp:TextBox>  
        <br />  
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="Save data in ViewState"  
            OnClick="Button1_Click"  
            Font-Bold="true"  
            ForeColor="DarkGreen"  
            />  
        <asp:Button   
            ID="Button2"   
            runat="server"   
            Text="Read data from ViewState"  
            OnClick="Button2_Click"  
            Font-Bold="true"  
            ForeColor="DarkGreen"  
            />  
    </div>  
    </form>  
</body>  
</html>

Monday, January 10, 2011

Driving directions from Google Maps in XML format

The Google maps return this document in result of Http request sent by the user which contains the data for direction and distance. You will see the implementation and translation when you walk in to the code. Use Linq allows XML data to be queried by using the standard query operators as well as tree-specific operators that provide XPath-like navigation through descendants, ancestors, and siblings. It simplifies working with XML data without having to resort to using additional language syntax like XPath or XQuery. You can use LINQ to XML to perform LINQ queries over XML that you retrieve from the file system, from a remote web service, or from in-memory XML content. 
Coding ::
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Xml;
 XNamespace obj_XNameSpace = XNamespace.Get("http://earth.google.com/kml/2.0");
string Des_Source_URL = "http://maps.google.com/?q=From " + txtDestination.Text + " to " + txtSource.Text + "&output=kml&view=text";
IEnumerable<XElement> nodes = from n in Des_Source_document.Descendants(obj_XNameSpace + "Placemark")    
select n;
var Select_Name_Doc = from n in nodes
where ((from x in n.Descendants(obj_XNameSpace + "name"select x).Count() > 0)
&& ((from y in n.Descendants(obj_XNameSpace + "description")
select y).Count() > 0)

select n;

foreach (var node in Select_Name_Doc)
{
    DataRow dr = obj_datatble.NewRow();
    dr["Value"] = node.Descendants(obj_XNameSpace + "name").First().Value;
    dr["Distance"] = node.Descendants(obj_XNameSpace + "description").First().Value;
    obj_datatble.Rows.Add(dr);
}
GridView1.DataSource = obj_datatble;
GridView1.DataBind();

About the Code ::
1: XNamespace this will works as a namespace for which we will use to parse the KML for the specified Uniform Resource Identifier.
2: Browser Qurey www.maps.google.com/?q=from "from_address" to "to_address" "&output=kml&view=text"; (where "from" and "to" are the keywords).
3: The "output" parameter retrieves the result in the given format which will be KML in this case.
4: 
The Xdocument.Descendants() Returns a collection of the descendant elements for this document or element, in document order.
5: 
After getting all the vlaues in Select_Name_Doc I declare a cusom DataTable with two rows "value" and "distance" and then storing vlaue of each node in these rows and then add in to a DataTable inside foreach loop. After it's done adding the rows, I assigned the DataTable to GridView as  DataSource and then bind the datagrid.