Sei sulla pagina 1di 4

3/9/2015

Home

HowtouseTreeviewcontrolinAsp.netapplications~DevelopersCode

Asp.Net

C Sharp

Jquery

Mobile Development

Advance Topics

Reviews

How to use Treeview control in Asp.net applications


TanishaSayyad

.NetTutorials,Aspnet,C#,CustomControls

Contact

Search the site

Search!

About Me
T ANI S HA S AY Y AD
Follow

187

V I E W M Y COM P LE T E
P ROF I LE

Hifriends,inthisarticleIwouldliketoexplainhowtobinddatatoTreeViewcontrolinasp.net
usingc#.Fromlastfewweekswearegettingsomanyrequestsabouttreeviewcontrolexample
inasp.netapplications.Thistimewearegoingtoexplainabasictutorialabouttreeviewcontrol

Categories

forbeginners.

.NetTutorials(92)

Pleasefollowthesteps

Ajax(18)
Android(14)

Step:1
InthisarticleIamconsideredStatesanditsdistrictsasnodesanditschildnodes.Letusconsider
asqltableasbelow:
viewplain

01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.

print

AndroidControls(6)
AndroidLayouts(3)

SETANSI_NULLSON
GO

SETQUOTED_IDENTIFIERON
GO

SETANSI_PADDINGON
GO

CREATETABLE[dbo].[ExampleOnTreeView](
[Id][int]NULL,
[StateId][int]NULL,
[StateName][varchar](50)NULL
)ON[PRIMARY]

GO

SETANSI_PADDINGOFF
GO

AndroidTutorials(10)
AndroidBasic(8)
Angularjs(1)
Asha(13)
Aspnet(84)
AUIDesigns(4)
Authentication(4)
C#(80)
ChartControl(2)
CloudComputing(4)

Pleasefindthebelowscreenshot(ExampleTreeViewTable.jpg)forsampledatathatIshownin
treeviewoutput.

CSharp(57)
CustomControls(7)
dotnet(1)
DynamicControls(1)
ExcelSheet(1)
Facebook(6)
Fetchimages(1)
general(11)
GeneralTopics(25)
GoogleDocs(1)

http://www.developerscode.com/2012/08/howtousetreeviewcontrolinaspnet.html

1/4

3/9/2015

HowtouseTreeviewcontrolinAsp.netapplications~DevelopersCode
Google+(2)
Gridview(9)
HTML5(12)
InterviewQuestions(3)
javascript(8)
JQuery(25)
jqueryplugins(1)
jquerymobile(1)
json(2)
listview(2)
listviewpagination(1)
liveurl(1)
metatags(1)
OnlineTips(5)
PhoneVersions(1)
PhoneGap(6)
PhoneGapPlugins(2)
Pinterest(1)

random(1)
Step2:
Then,comingtocodeforTreeViewcontrolinasp.net:In.aspxpage:

RepeaterControl(3)
restfulservice(1)

viewplain

01.
02.
03.
04.
05.
06.

print

Review(8)

<%@PageTitle=""Language="C#"AutoEventWireup="true"CodeFile="TreeViewExample.aspx.cs"Inherits="Year"%>
<title></title><formid="form1"runat="server">
Sessions(3)
<spanstyle="font
size:small;">TreeViewExample:<asp:treeviewid="TreeViewStatesAndDistricts"imageset="Msdn"runat="server">
</asp:treeview>
SocailAPIs(8)
</span></form>
<spanstyle="fontsize:small;">
SocialNetwork(10)
</span>

SocialAPI(4)
Step3:

Spinner(2)

In.aspx.cspage:
viewplain

01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.

print

Sqlite(4)
?

SqlServer(9)

<spanstyle="fontsize:small;">
SusanHannan(1)
usingSystem;
usingSystem.Web;
usingSystem.Web.UI;
Tanisha(70)
usingSystem.Web.UI.WebControls;
usingSystem.Data.SqlClient;
usingSystem.Data;
title(1)
usingSystem.Configuration;

ToolTip(1)
publicpartialclassTreeViewExample:System.Web.UI.Page
{
SqlConnectioncon;
tricks(2)
protectedvoidPage_Load(objectsender,EventArgse)
{
try
Twitter(1)
{
con=newSqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString);
WCF(4)
if(!Page.IsPostBack)
{
//GettingStateNamesfromthedatabasetable(ExampleOnTreeView).
WebConcepts(13)
SqlCommandcmdStates=newSqlCommand("SelectId,StateNamefromExampleOnTreeViewwhereStateIdisnull",con);
DataSetdsStates=newDataSet();
SqlDataAdapterdaStates=newSqlDataAdapter(cmdStates);
windowscommunicationfoundation(1)
daStates.Fill(dsStates);

Xml(5)
intStatesCount=dsStates.Tables[0].Rows.Count;
string[]Names=newstring[StatesCount];
inti=0;

foreach(DataRowdrStatesindsStates.Tables[0].Rows)
{
TreeViewStatesAndDistricts.Nodes.Add(newTreeNode(drStates["StateName"].ToString(),drStates["Id"].ToString()));
Names[i]=drStates["id"].ToString();

http://www.developerscode.com/2012/08/howtousetreeviewcontrolinaspnet.html

2/4

3/9/2015
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.

HowtouseTreeviewcontrolinAsp.netapplications~DevelopersCode

i++;
}

if(i>0)
{
//GettingDistrictNamesfromtheexistingstate.
stringDistrictsQuery="";
i=0;
for(intj=0;j<StatesCount;j++)
{
if(j==0)
DistrictsQuery="StateId="+Names[j];
else
DistrictsQuery+="orStateId="+Names[j];
}

SqlCommandcmdDistricts=newSqlCommand("SelectId,StateId,StateNamefromExampleOnTreeViewwhere"+DistrictsQuery,con);
DataSetdsDistricts=newDataSet();
SqlDataAdapterdaDistricts=newSqlDataAdapter(cmdDistricts);
daDistricts.Fill(dsDistricts);

StatesCount=dsDistricts.Tables[0].Rows.Count;
Names=newstring[StatesCount];

foreach(DataRowdrDistrictsindsDistricts.Tables[0].Rows)
{
TreeViewStatesAndDistricts.FindNode(drDistricts["StateId"].ToString()).ChildNodes.Add(newTreeNode(drDistricts["StateName"].ToString
}

}
}
}
catch
{

}
}
}

</span>

Afterexecutingtheabovecodebypressing(F5)itwillgeneratetheStates&districtsinTreeView
formatasbelow(ExampleOnTreeView.jpg):

MR:Author

Tanisha Sayyad
isaSoftwareEngineer,BloggerandFounderofDevelopersCodefromIndia.Hisarticlesmainlyfocus

http://www.developerscode.com/2012/08/howtousetreeviewcontrolinaspnet.html

3/4

3/9/2015

HowtouseTreeviewcontrolinAsp.netapplications~DevelopersCode
on .Net (Web and Windows based applications), Mobile Technologies
(Android,IPhone,BlackBerry)andSEO.

RelatedPosts

HowtoStoreLogout
informationwhe...

SortingtheDatawith
String,Intege...

2Waystobinddatato
gridviewusi...

GetEmployeeslist
usingJqueryAja...

NEWER POST

Simplelogicto
displaytooltipin...

OLDER POST

Copyright@2013DevelopersCode.

http://www.developerscode.com/2012/08/howtousetreeviewcontrolinaspnet.html

Templateify&Twigplay

4/4

Potrebbero piacerti anche