Showing posts with label somebody. Show all posts
Showing posts with label somebody. Show all posts

Monday, March 26, 2012

populate checkboxlist from XmlDataSource (ASP.net 2.0)

can somebody please show me some code that will let me populate a
checkboxlist from the xmldatasource, and have the text property set to the
names in the xml file
thanks
you can also email me at i_am_the_freshmaka@dotnet.itags.org.hotmail.commike <mike@.discussions.microsoft.com> ha scritto:
> can somebody please show me some code that will let me populate a
> checkboxlist from the xmldatasource, and have the text property set
> to the names in the xml file
> thanks
With this XML file:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<item value="1" text="Value 1" />
<item value="2" text="Value 2" />
<item value="3" text="Value 3" />
</root>
And this code:
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="XmlDataSource1"
DataTextField="text" DataValueField="value">
</asp:CheckBoxList><asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/Data/XMLFile.xml"></asp:XmlDataSource>
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az
<Lookup>
<Lookup_Entry>
<Ts>
<T>
<M>context</M>
<V>type</V>
<Va>Citizen</Va>
</T>
</Ts>
<U>.html</U>
</Lookup_Entry>
</Lookup>
example of xml file is above
code:
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Lookup
table.xml"
XPath='/Lookup/Lookup_Entry/Ts/T/Va[../../T[M = "context" and V
= "type"]]'>
</asp:XmlDataSource>
<asp:CheckBoxList
ID="CheckBoxList1"
runat="server"
DataSourceID="XmlDataSource1">
</asp:CheckBoxList>
Now how do i get each <Va> that is returned with the xmldatasource as a item
in my checkboxlist
displayed now when run is with a checkbox to the left of each item:
System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
mike
"Andrea Zani" wrote:

> mike <mike@.discussions.microsoft.com> ha scritto:
> With this XML file:
> <?xml version="1.0" encoding="utf-8" ?>
> <root>
> <item value="1" text="Value 1" />
> <item value="2" text="Value 2" />
> <item value="3" text="Value 3" />
> </root>
> And this code:
> <asp:CheckBoxList ID="CheckBoxList1" runat="server"
> DataSourceID="XmlDataSource1"
> DataTextField="text" DataValueField="value">
> </asp:CheckBoxList><asp:XmlDataSource ID="XmlDataSource1" runat="server"
> DataFile="~/Data/XMLFile.xml"></asp:XmlDataSource>
> --
> AZ [Microsoft - .NET MVP]
> Mia Home page: http://ciclismo.sitiasp.it
> Asp.Net community: http://www.aspitalia.com
> Il mio blog: http://blogs.aspitalia.com/az
>
>

populate checkboxlist from XmlDataSource (ASP.net 2.0)

can somebody please show me some code that will let me populate a
checkboxlist from the xmldatasource, and have the text property set to the
names in the xml file

thanks
you can also email me at i_am_the_freshmaka@dotnet.itags.org.hotmail.commike <mike@.discussions.microsoft.com> ha scritto:
> can somebody please show me some code that will let me populate a
> checkboxlist from the xmldatasource, and have the text property set
> to the names in the xml file
> thanks

With this XML file:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<item value="1" text="Value 1" />
<item value="2" text="Value 2" />
<item value="3" text="Value 3" />
</root
And this code:
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="XmlDataSource1"
DataTextField="text" DataValueField="value">
</asp:CheckBoxList><asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/Data/XMLFile.xml"></asp:XmlDataSource
--
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az
<Lookup>
<Lookup_Entry>
<Ts>
<T>
<M>context</M>
<V>type</V>
<Va>Citizen</Va>
</T>
</Ts>
<U>.html</U>
</Lookup_Entry>
</Lookup>
example of xml file is above

code:
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Lookup
table.xml"
XPath='/Lookup/Lookup_Entry/Ts/T/Va[../../T[M = "context" and V
= "type"]]'>
</asp:XmlDataSource>
<asp:CheckBoxList
ID="CheckBoxList1"
runat="server"
DataSourceID="XmlDataSource1">
</asp:CheckBoxList
Now how do i get each <Va> that is returned with the xmldatasource as a item
in my checkboxlist

displayed now when run is with a checkbox to the left of each item:
System.Web.UI.WebControls.XmlDataSourceNodeDescrip tor
System.Web.UI.WebControls.XmlDataSourceNodeDescrip tor
System.Web.UI.WebControls.XmlDataSourceNodeDescrip tor
System.Web.UI.WebControls.XmlDataSourceNodeDescrip tor
System.Web.UI.WebControls.XmlDataSourceNodeDescrip tor
System.Web.UI.WebControls.XmlDataSourceNodeDescrip tor
System.Web.UI.WebControls.XmlDataSourceNodeDescrip tor

mike

"Andrea Zani" wrote:

> mike <mike@.discussions.microsoft.com> ha scritto:
> > can somebody please show me some code that will let me populate a
> > checkboxlist from the xmldatasource, and have the text property set
> > to the names in the xml file
> > thanks
> With this XML file:
> <?xml version="1.0" encoding="utf-8" ?>
> <root>
> <item value="1" text="Value 1" />
> <item value="2" text="Value 2" />
> <item value="3" text="Value 3" />
> </root>
> And this code:
> <asp:CheckBoxList ID="CheckBoxList1" runat="server"
> DataSourceID="XmlDataSource1"
> DataTextField="text" DataValueField="value">
> </asp:CheckBoxList><asp:XmlDataSource ID="XmlDataSource1" runat="server"
> DataFile="~/Data/XMLFile.xml"></asp:XmlDataSource>
> --
> AZ [Microsoft - .NET MVP]
> Mia Home page: http://ciclismo.sitiasp.it
> Asp.Net community: http://www.aspitalia.com
> Il mio blog: http://blogs.aspitalia.com/az
>

Populate data in ListBox for Multiple Selections (C#)

Hi there,
I am new in ASP.NET and wonder if somebody can help me with populating data in a ListBox from a database. Users should be able to choose multiple selections and pass the selected values for a search query. Please let me know if I miss anything.
Thanks a lot in advance!
// search.aspx
<asp:ListBox id="Community2" runat="server"></asp:ListBox>
//search.aspx.cs

if (!Community2.Items.FindByValue("-1").Selected)

{

for (int i=0 ; i<Community2.Items.Count ; i++)

{

if (Community2.ItemsIdea [I].Selected)

{

if (Community2 != "") Community2 += ",";

Community2 += Community2.ItemsIdea [I].Value;

}

}

}


//db.cs

publicvoid PopulateCommunities2(ListControl i_list,bool i_showalloption)

{

OleDbDataAdapter adapter =new OleDbDataAdapter("SELECT distinct community FROM properties", m_conn);

DataTable dt =new DataTable();

adapter.Fill(dt);

i_list.Items.Clear();

if (i_showalloption) i_list.Items.Add(new ListItem("All Communities", "-1"));

for (int i=0 ; i<dt.Rows.Count ; i++)

{

i_list.Items.Add(new ListItem(dt.RowsIdea [I]["community"])); //I am using pre-build code and have no idea what else I could do here.

}

}

For databind of a ListBox, you don't need to use loop to put data into the list. Just use ListBox1.DataSource field. For example:
ListBox1.DataSourse = GetCommunities();
ListBox1.DataValueField = "community";
ListBox1.DataTextField = "community";
ListBox1.DataBind;
GetCommunites method could be a DataSet, DataTable or DataView like:
Private DataTable GetCommunities()
{

OleDbDataAdapter adapter =new OleDbDataAdapter("SELECT distinct community FROM properties", m_conn);

DataTable dt =new DataTable();

adapter.Fill(dt);


return dt;
}

For your process of the user selected items from the ListBox, you can put a "," at the first or last. But, after that, you need to remove the first "," (if you put at first), or remove the last "," (if you put at the last).


Also, you're treating Community2 like a listbox, as in
Community2.Items.FindByValue("-1")
...but you're also treating it as a string, like this:
Community2 += ",";
...sometimes even in the same statement!

if (Community2 != "") Community2 += ",";

Community2 += Community2.Items[i ].Value;

}
You need to decide which one it is (I'm assuming the former).