Monday, March 9, 2009

DropDownList inside Gridview

Hi All,

It is very common to have child controls inside the Gridview.Among them Drop down List is very common.Drop down list inside the gridview and handling their selected index changed event becomes inevitable.

We know that Drop Down list does not Support Command Name property so we cant handle the event in the Row Command event.

Here is the solution of it...which is used very very commonly...

protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e) {

// get reference to the row

GridViewRow gvrow = (GridViewRow)(((Control)sender).NamingContainer);

// Get the reference of this DropDownlist

DropDownList ddl1= (DropDownList) gvrow.FindControl("dropdownlist1");

// Get the reference of other DropDownlist in the same row.

DropDownList ddl2= (DropDownList) gvrow.FindControl("dropdownlist2");

}


Thanks,

Nitin Sharma


Multiple Joins in LINQ



Hi All,
This Post will show how to implement multiple Joins in LINQ.



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;

public partial class JoinsInLINQ : System.Web.UI.Page

{

DemoDataContext DC = new DemoDataContext();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

Bind();

}

}

private void Bind()

{

// How to Apply Joins in LINQ

var items = from P in DC.Emps

join Ep in DC.EmpProducts on P.SINo equals Ep.SINo

join TT in DC.TextTables on P.SINo equals TT.SINo

select new

{

Name = P.Name,

Age = P.Age,

Country = P.Country,

State = P.State

};

GVInJoins.DataSource = items;

GVInJoins.DataBind();

}

}


On Debugging the Query we will find it like as........



{SELECT [t0].[Name], [t0].[Age], [t0].[Country], [t0].[State]
FROM [dbo].[Emp] AS [t0]
INNER JOIN [dbo].[EmpProduct] AS [t1] ON [t0].[SINo] = [t1].[SINo]
INNER JOIN [dbo].[TextTable] AS [t2] ON [t0].[SINo] = [t2].[SINo]
}




Now finally Bind the Output to the Gridview and its ready now...




Thanks to LINQ :)


Thanks,

Nitin Sharma

Add to Favorites/Bookmarks Using Javascript

Hi All,
Many a times while browsing we add our Pages to Favorites/Bookmarks.
This can be done by Javascript.
Below is the Javascript COde , which has benn simply called on HyperLink button.



function AddtoBookMarks()

{

if (document.all)

{

window.external.AddFavorite(location.href, document.title);

}

else if (window.sidebar)

{

window.sidebar.addPanel(document.title,location.href);

}

}

<asp:LinkButton ID="LnkFav" runat="server" OnClientClick="javascript:AddtoBookMarks();return false;">Add this Page to Favoritesasp:LinkButton>

Happy Coding..!

Nitin Sharma