August 24, 2012

BlackBorder around PNG Images in IE 7

When Displaying PNG Images in IE7 , I was facing the problem of a black borders around the text in the
png images .

Below solution I used to resolve the problem,
Convert png image to png 8
Open image in the Photoshop go to the menu option 'Save for Web & Devices ' Choose the below settings
in there

1.  PNG-8,
2.  Selective
3.  Diffusion
4.  Check the  Transparency
5.  Colors: 256
6.  Matte: None
7.  No transparency Dither
8.  Checked  Interlaced
9.  Convert to sRGB

Click on done and saved the image

August 10, 2012

Rounded Corner Menu in CSS compatible with all browsers including IE code Example

Below css3 code can be used to round any html element (div,ul...etc)     
     
                 -webkit-border-radius: 15px;

 -moz-border-radius: 15px;
  border-radius: 15px;

Sample Code 
in HTML




<div class="menu">
<ul>
                      <li><a href="index.html" >HOME</a></li>
                        <li><a href="services.html">ABOUT US</a></li>
<li><a href="services.html">PRODUCTS</a></li>
                        <li><a href="clients.html">CLIENTS</a></li>
                        <li><a href="contact-us.html" >CONTACT US</a></li>
                </ul>
</div>

CSS



.menu{
border:none;
border:0px;
margin:0px;
padding:0px;
font: 67.5% "Lucida Sans Unicode", "Bitstream Vera Sans", "Trebuchet Unicode MS", "Lucida Grande", Verdana, Helvetica, sans-serif;
font-size:15px;
font-weight:bold;

}

.menu ul{
background:#c89810;
height:50px;
list-style:none;
margin:0;
padding:0;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
behavior: url(border-radius.htc);
        -webkit-box-shadow: inset 0px 16px 0px 0px rgba(255, 255, 255, .1);
-moz-box-shadow: inset 0px 16px 0px 0px rgba(255, 255, 255, .1);
box-shadow: inset 0px 16px 0px 0px rgba(255, 255, 255, .1);

}


.menu li{
float:left;
padding:0px 0px 0px 51px;
}


.menu li a{
color:#FFFFFF;
display:block;
font-weight:bold;
line-height:50px;
margin:0px;
padding:0px 25px;
text-align:center;
text-decoration:none;
}

.menu li a:hover{
background:#C60;
color:#000;
text-decoration:none;
-webkit-box-shadow: inset 0px 0px 7px 2px rgba(0, 0, 0, .3);
-moz-box-shadow: inset 0px 0px 7px 2px rgba(0, 0, 0, .3);
box-shadow: inset 0px 0px 7px 2px rgba(0, 0, 0, .3);
}


.menu ul li:hover a{
background:#fccc3d;
color:#000;
text-decoration:none;
}

.menu li ul{
display:none;
height:auto;
padding:0px;
margin:0px;
border:0px;
position:absolute;
width:200px;
z-index:200;
}


.menu li:hover ul{
display:block;
}

.menu li li {
display:block;
float:none;
margin:0px;
padding:0px;
width:200px;
background:#F93;
}

.menu li:hover li a{
background:none;
}

.menu li ul a{
display:block;
height:50px;
font-size:12px;
font-style:normal;
margin:0px;
padding:0px 10px 0px 15px;
text-align:left;
}


This will create a rounded corner menu , but this will work in all browsers except IE. To make it work in IE Create file named 'border-radius.htc'

and paste the below content in the file


--Do not remove this if you are using--
Original Author: Remiz Rahnas
Original Author URL: http://www.htmlremix.com
Published date: 2008/09/24

Changes by Nick Fetchak:
- IE8 standards mode compatibility
- VML elements now positioned behind original box rather than inside of it - should be less prone to breakage
Published date : 2009/11/18


<public:attach event="oncontentready" onevent="oncontentready('v08vnSVo78t4JfjH')" />
<script type="text/javascript">

// findPos() borrowed from http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
var curleft = curtop = 0;

if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
}

return({
'x': curleft,
'y': curtop
});
}

function oncontentready(classID) {
  if (this.className.match(classID)) { return(false); }

if (!document.namespaces.v) { document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); }

this.className = this.className.concat(' ', classID);
var arcSize = Math.min(parseInt(this.currentStyle['-moz-border-radius'] ||
                               this.currentStyle['-webkit-border-radius'] ||
                               this.currentStyle['border-radius'] ||
                               this.currentStyle['-khtml-border-radius']) /
                      Math.min(this.offsetWidth, this.offsetHeight), 1);
var fillColor = this.currentStyle.backgroundColor;
var fillSrc = this.currentStyle.backgroundImage.replace(/^url\("(.+)"\)$/, '$1');
var strokeColor = this.currentStyle.borderColor;
var strokeWeight = parseInt(this.currentStyle.borderWidth);
var stroked = 'true';
if (isNaN(strokeWeight)) {
strokeWeight = 0;
strokeColor = fillColor;
stroked = 'false';
}

this.style.background = 'transparent';
this.style.borderColor = 'transparent';

// Find which element provides position:relative for the target element (default to BODY)
var el = this;
var limit = 100, i = 0;
while ((typeof(el) != 'unknown') && (el.currentStyle.position != 'relative') && (el.tagName != 'BODY')) {
el = el.parentElement;
i++;
if (i >= limit) { return(false); }
}
var el_zindex = parseInt(el.currentStyle.zIndex);
if (isNaN(el_zindex)) { el_zindex = 0; }
//alert('got tag '+ el.tagName +' with pos '+ el.currentStyle.position);

var rect_size = {
'width': this.offsetWidth - strokeWeight,
'height': this.offsetHeight - strokeWeight
};
var el_pos = findPos(el);
var this_pos = findPos(this);
this_pos.y = this_pos.y + (0.5 * strokeWeight) - el_pos.y;
this_pos.x = this_pos.x + (0.5 * strokeWeight) - el_pos.x;

var rect = document.createElement('v:roundrect');
rect.arcsize = arcSize +'px';
rect.strokecolor = strokeColor;
rect.strokeWeight = strokeWeight +'px';
rect.stroked = stroked;
rect.style.display = 'block';
rect.style.position = 'absolute';
rect.style.top = this_pos.y +'px';
rect.style.left = this_pos.x +'px';
rect.style.width = rect_size.width +'px';
rect.style.height = rect_size.height +'px';
rect.style.antialias = true;
rect.style.zIndex = el_zindex - 1;

var fill = document.createElement('v:fill');
fill.color = fillColor;
fill.src = fillSrc;
fill.type = 'tile';

rect.appendChild(fill);
el.appendChild(rect);

var css = el.document.createStyleSheet();
css.addRule("v\\:roundrect", "behavior: url(#default#VML)");
css.addRule("v\\:fill", "behavior: url(#default#VML)");

isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
// IE6 doesn't support transparent borders, use padding to offset original element
if (isIE6 && (strokeWeight > 0)) {
this.style.borderStyle = 'none';
this.style.paddingTop = parseInt(this.currentStyle.paddingTop || 0) + strokeWeight;
this.style.paddingBottom = parseInt(this.currentStyle.paddingBottom || 0) + strokeWeight;
}

if (typeof(window.rounded_elements) == 'undefined') {
window.rounded_elements = new Array();

if (typeof(window.onresize) == 'function') { window.previous_onresize = window.onresize; }
window.onresize = window_resize;
}
this.element.vml = rect;
window.rounded_elements.push(this.element);
}

function window_resize() {
if (typeof(window.rounded_elements) == 'undefined') { return(false); }

for (var i in window.rounded_elements) {
var el = window.rounded_elements[i];

var strokeWeight = parseInt(el.currentStyle.borderWidth);
if (isNaN(strokeWeight)) { strokeWeight = 0; }

var parent_pos = findPos(el.vml.parentNode);
var pos = findPos(el);
pos.y = pos.y + (0.5 * strokeWeight) - parent_pos.y;
pos.x = pos.x + (0.5 * strokeWeight) - parent_pos.x;

el.vml.style.top = pos.y +'px';
el.vml.style.left = pos.x +'px';
}

if (typeof(window.previous_onresize) == 'function') { window.previous_onresize(); }
}
</script>


then in CSS add the below line


behavior: url(border-radius.htc);

Now this will work in IE as well.


July 18, 2012

PagingBulletedListExtender Control from AJAX Control ToolKit Code Example

PagingBulletedListExtender allows alphabetic paging to the bulleted lists.
Read the below post to know how you can use the controls of AJAX Control ToolKit
http://www.dotnetissues.com/2012/07/alwaysvisiblecontrolextender-from.html


To us the PagingBulletedListExtender  set the TargetControlId of this control with the bulleted list which you wants 
to Page alphabetically .

See the below code







PagingBulletedList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PagingBulletedList.aspx.cs" Inherits="PagingBulletedList" %>


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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    
    <div>


    <aspa:pagingbulletedlistextender ID="Pagingbulletedlistextender1" runat="server" TargetControlID="BulletedList1"></aspa:pagingbulletedlistextender><br />
    
     <asp:BulletedList ID="BulletedList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField ="Name" />
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:dotnettrainingConnectionString2 %>" 
        SelectCommand="SELECT [Name] FROM [Employeedetails]"></asp:SqlDataSource>
    </form>
</body>
</html>

July 17, 2012

ConfirmButtonExtender Control from AJAX Control Tool kit

To know how to use Controls from AJAX Control Toolkit follow this post
http://www.dotnetissues.com/2012/07/alwaysvisiblecontrolextender-from.html

When there is some critical Operation on a button click its a good idea to prompt  'Are you sure' message to the user and this can be easily accomplished by ConfirmButtonExtender  control from AJAXControlToolkit

Set the TargetControlId and ConfirmText Property of the ConfirmButtonExtender
As per below code sample



ConfirmButtonExtender.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConfirmButtonExtender.aspx.cs" Inherits="ConfirmButtonExtender" %>


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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">


<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:scriptmanager ID="Scriptmanager1" runat="server" ></asp:scriptmanager>
    <asp:ConfirmButtonExtender ID="ConfirmButtonExtender1" ConfirmText="Are you sure want to delete this" TargetControlID="btnDelete" runat="server"></asp:ConfirmButtonExtender>
    <asp:Button ID="btnDelete" runat ="server" Text ="DELETE"    />
    <div>
    
    </div>
    </form>
</body>
</html>

when you click on the delete button below prompt will come.



AutoCompleteExtender From AJAX Control Tool Kit Code Example

To know how to use controls from AJAX Control Took Kit , you can read this post.

http://www.dotnetissues.com/2012/07/alwaysvisiblecontrolextender-from.html
Below is the cod sample for how to Use AutoCompleteExtender  Control.
Many times when we are typing in Text box we want the relevant text should come automatically after typing few letters , This functionality is being used in almost all the search engines.

To integrate this functionality we can use AutoCompleteExtender   from AJAX Control Toolkit

Drag n drop this control in Web form and set the property TargetControlID
and then Clink on Add AutoComplete Page Method on the target control as below


This will add GetCompletionList(string prefixText, int count, string contextKey) method in code -behind.
Where You have to write the Business logic to get the relevant List

I have a table 'EmployeeDetails' in DB with a column 'Name' as below

Below code I have written for the AutoComplete Name

AutoCompleteExtender .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoCompleteExtender.aspx.cs" Inherits="AutoCompleteExtender" %>


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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
    
    <asp:autocompleteextender ID="Autocompleteextender1" TargetControlID="txtName"  
            runat="server" ServiceMethod="GetCompletionList" UseContextKey="True"></asp:autocompleteextender>
     <br/>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </div>
    
    </form>
</body>
</html>


AutoCompleteExtender .aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;




public partial class AutoCompleteExtender : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
       


    }


    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        SqlConnection connObj = new SqlConnection();
        //DataSet ds;
        SqlCommand objCommand;
       
        connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();
        connObj.Open();


        objCommand = new SqlCommand("Select  Name from Employeedetails where Name LIKE'"+prefixText+"%' ", connObj);
        SqlDataReader dr;
        List<string> NameList = new List<string>();
        dr = objCommand.ExecuteReader();
        while (dr.Read())
        {
            NameList.Add(dr["Name"].ToString());




        }


        return NameList.ToArray();
    }
}


and this is how the behavior will be.


July 16, 2012

AlwaysVisibleControlExtender from AJAXControl ToolKit Code Example

AlwaysVisibleControlextender :When displaying information in the browser sometime you may want to display
some information fixed on the Browser screen ,So we can use this alwaysvisiblecontrolextender  Control
from AJAX Control Tool Kit and set the position on the screen , It will be then visible even after user has scrolled the screen.

Before know how to use this control we have to know How to integrate the AJAX Control Tool Kit in Visual Studio as it is not by default there.To add AJAX Control Tool Kit Download it from the below link
http://ajaxcontroltoolkit.codeplex.com/downloads/get/116534

After that In Toolbox right click and go to AddTab as below

Now Name the tab as you want Ex. AJAXToolKit

Then Right click on the tab and click on Choose items as below

Then Browse for the AJAXCONTROLTOOLKIT.DLL from the download location.

It will add All the controls from AJAXControlToolkit to your VisualStudio Toolbox and you can use them
as any other standard controls.

Nowto Use alwaysvisiblecontrolextender
First Drag the ScriptManager on the page and then Drag the alwaysvisiblecontrolextender

set the position of the alwaysvisiblecontrolextender  control on the page by setting properties
HorizontalOffset and  VerticalOffset
and then Set the TargetControlID Property , which will define which content has to be fixed on the screen

See the Below example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AlwaysVisibleControl.aspx.cs" Inherits="AlwaysVisibleControl" %>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
            <asp:alwaysvisiblecontrolextender ID="Alwaysvisiblecontrolextender1" HorizontalOffset="160" VerticalOffset="140"  TargetControlID="Panel1" runat="server"></asp:alwaysvisiblecontrolextender>
 <asp:panel ID="Panel1" runat="server">
             This Text Is always visible 
            
            </asp:panel>
    </div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
       <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
         <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
           <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
             <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
              <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
       <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
         <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
           <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
             <asp:Label ID="Label10" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
              <asp:Label ID="Label11" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
       <asp:Label ID="Label12" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
         <asp:Label ID="Label13" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
           <asp:Label ID="Label14" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
             <asp:Label ID="Label15" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
              <asp:Label ID="Label16" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
       <asp:Label ID="Label17" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
         <asp:Label ID="Label18" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
           <asp:Label ID="Label19" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
             <asp:Label ID="Label20" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
              <asp:Label ID="Label21" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
       <asp:Label ID="Label22" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
         <asp:Label ID="Label23" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
           <asp:Label ID="Label24" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
             <asp:Label ID="Label25" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
              <asp:Label ID="Label26" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
       <asp:Label ID="Label27" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
         <asp:Label ID="Label28" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
           <asp:Label ID="Label29" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
             <asp:Label ID="Label30" runat="server" Text="Label"></asp:Label><br /><br /><br /><br /><br /><br />
    </form>
</body>
</html>

May 21, 2012

Change Page Title dynamically in ASP.NET using C#

I din't have fixed Page Titles ,I needed to add them dynamically after fetching from DB.

First I used below code to add Page Title dynamically .
  
// Response.Write("<title>" + ds.Tables[0].Rows[0][1].ToString() + "</title>");

This worked in Google Chrome , but not in IE , I looked into Page Source and the title was being added on the top before <html> tag. this was the problem IE was not letting it work .

then below code worked well to add title dynamically in all the browsers

Page.Title = ds.Tables[0].Rows[0][1].ToString();

May 16, 2012

The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

I got below  error while running a live application on godaddy shared hosting server, Couldn't figure what the exact problem was , But I Republished the  Application on Local Machine and Replaced the 'bin' folder of Live App on Hosting with this Republished 'bin' folder .
and the problem was resolved.


The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

March 8, 2012

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.


Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.


above error came when I clicked on a button after session expired.

I solved the above by adding
<pages enableViewStateMac ="false">
in web.config file.

February 28, 2012

Nested GridView within Cell of a OuterGridView

Below sample code is to show how a nested gridview can be added in a GridView Cell.

DB Design
 Stuent Table
     Student _id int
     FirstName Varchar
     LastName varchar
     ContactID int


Contact table
       ContactID int
       Email varchar
       HomePhone varchar
       Mobile varchar

In a outer grid view all the data from table student will come and in column ContactId the data will be retrieved from the contact table and will be added in the inner Gridview .

Below is the code

NestedGridView .aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NestedGridView.aspx.cs" Inherits="NestedGridView" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="OuterGrid" runat="server" 
            onrowdatabound="OuterGrid_RowDataBound">
        </asp:GridView>
    </div>
    </form>
</body>
</html>


NestedGridView .aspx.cs


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;


public partial class NestedGridView : System.Web.UI.Page
{
   // private string connstring = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ConnectionString;
    private SqlConnection connObj;


    private SqlDataAdapter objAdapter;
    private DataSet ds = new DataSet();


    protected void Page_Load(object sender, EventArgs e)
    {


    
        if (!IsPostBack)
        {
            //Bind OuterGrid View Here
            BindOuterGridView();


        }
    }




    private void BindOuterGridView()
    {
        connObj = new SqlConnection();
        connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();
        connObj.Open();






        SqlCommand objCommand = new SqlCommand("Select Student_id, FirstName,LastName,Contactid from student ", connObj);


        objAdapter = new SqlDataAdapter(objCommand);






        objAdapter.Fill(ds);


        connObj.Close();




        OuterGrid.DataSource = ds.Tables[0];
        OuterGrid.DataBind();
        


    }
    protected void OuterGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Create Inner GridView Here
        DataSet ds = new DataSet();
         GridView grd2 =new GridView ();


        GridViewRow ROW = e.Row;
        if (ROW.RowType != DataControlRowType.Header )
        {
         //  grd2 = new GridView();
            int contactID=0;


            if(e.Row.Cells[3].Text!="&nbsp;" )
             contactID = int.Parse(e.Row.Cells[3].Text);


            //grd2 = (GridView)e.Row.FindControl("grd2");
            if (contactID != 0)
            {
                connObj.Open();
                SqlCommand cmd = new SqlCommand("select * from Contact where Contactid=" + contactID, connObj);


                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;


                da.Fill(ds);
                connObj.Close();


                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        //Bind Inner  GridView Here
                        grd2.DataSource = ds.Tables[0];
                        grd2.DataBind();
                    }


                    else
                    {
                        grd2.EmptyDataText = "No Data Found.";
                        grd2.DataBind();
                    }


                }
                //Add Inner GridView To Outer GridView's cell 
                e.Row.Cells[3].Controls.Add(grd2 );
            }
        }
    }
}



Below is the ScreenShot attached of the o/p


February 27, 2012

Multiple Selection in ASP.NET calendar control

Below is the sample code to choose and display multiple dates selected from the calendar

CalMulitpleSelection.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CalMulitpleSelection.aspx.cs" Inherits="CalMulitpleSelection" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>


</head>
<body>
  <form id="Form2" runat="server">
        <h3>Using Calendar</h3>
        Please enter your requested vacation dates:
        <asp:Calendar id="Calendar1" runat="server"   selectionmode="DayWeekMonth" 
            onselectionchanged="Cal_SelectionChanged" 
            ondayrender="Calendar1_DayRender" />
        <asp:Button ID="Show" runat="server" Text="Button" onclick="Show_Click" />
        <br>
        <asp:label id="VacationLabel" runat="Server" />
    </form>
<body>


  


</html>

CalMulitpleSelection.aspx.cs


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;


public partial class CalMulitpleSelection : System.Web.UI.Page
{
    public static List<string> list = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Cal_SelectionChanged(object sender, EventArgs e)
    {
        if (Session["SelectedDates"] != null)
        {
            List<string> selectedList = (List<string>)Session["SelectedDates"];
        foreach (string s in selectedList)
        {
            Calendar1.SelectedDates.Add(Convert.ToDateTime (s));
        }
        list.Clear();
    }
    }
    protected void Show_Click(object sender, EventArgs e)
    {
        if (Session["SelectedDates"] != null)
        {
            List<string> newList = (List<string>)Session["SelectedDates"];
            foreach (string s in newList)
            {
                Response.Write(s+ "<BR/>");
            }
        }
    }


    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsSelected == true)
        {
            list.Add(e.Day.Date.ToString ("MM/dd/yyyy"));
        }
        Session["SelectedDates"] = list;
    }
}

GridView Sorting for Dynamically created columns

Below is the code for sorting on columns in dynamically bounded gridview


ExGridViewSorting .aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExGridViewSorting.aspx.cs" Inherits="ExGridViewSorting" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" onsorting="GridView1_Sorting" AllowSorting="true">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>



ExGridViewSorting .aspx.cs


using System;
using System.Collections.Generic;


using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class ExGridViewSorting : System.Web.UI.Page
{
    SqlConnection connObj = new SqlConnection();
    DataSet ds = new DataSet();
    SqlDataAdapter objAdapter;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {


            FillGrid();
        }
    }


    private void FillGrid()
    {
        connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();
        connObj.Open();


        SqlCommand objCommand = new SqlCommand("Select * from account", connObj);


        objAdapter = new SqlDataAdapter(objCommand);
        objAdapter.Fill(ds);
        connObj.Close();
        GridView1.DataSource = ds.Tables[0];
        ViewState["dtbl"] = ds.Tables[0];
        GridView1.DataBind();


    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = ViewState["dtbl"] as DataTable;
   
        if(dataTable!=null)
        {
        DataView  dvSortedView = new  DataView(dataTable);
       
            //string order=" asc";
            dvSortedView.Sort = e.SortExpression + " " + getSortDirection(e.SortDirection);
        GridView1.DataSource = dvSortedView;
        GridView1.DataBind();
        }
    }


    private string getSortDirection(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;


        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;


            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }


        return newSortDirection;
    }


}









February 20, 2012

Can anyone explain a perse error like this one: "Method 'get_EnableCdn' in type 'System.Web.UI.ScriptManager' from assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation."


I got this error in a web application when I was   upgrading  from a Visual Studio 2008 project to a Visual Studio 2010 project.
The solution was to remove the following section from the web.config file:
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>