asp.net dropdown using xsl output

bic (archive)

Usenet Archive
Joined
Jan 15, 2010
Messages
11
Reaction score
0
Hi,

I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown. Also with a dropdown item selected I want all elements of that item display on the page.

Thanks in advance.

---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
<Employee>
<EmployeeId>001</EmployeeId>
<Name>Stuart</Name>
<Age>28</Age>
</Employee>
</Employees>

---------------sample1.xslt-------------

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>

---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml"));

XslTransform trans = new XslTransform;
trans.Load(Server.MapPath("peopletable.xsl"));

xml1.Document = doc;
xml1.Transform = trans;

}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%> </form>
</body>
</html>
 
G

Guest

Hi,

I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown.  Also with a dropdown item selected I want all elements of that item display on the page.

Thanks in advance.

---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
  <Employee>
    <EmployeeId>001</EmployeeId>
    <Name>Stuart</Name>
    <Age>28</Age>
  </Employee>
</Employees>

---------------sample1.xslt-------------

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>

---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html  > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml"));

         XslTransform trans = new XslTransform;
         trans.Load(Server.MapPath("peopletable.xsl"));

         xml1.Document = doc;
         xml1.Transform = trans;

      }
   </script>
<head id="Head1" runat="server">
    <title>Xml Class Example</title>
</head>
<body>
   <h3>Xml Example</h3>
      <form id="form1" runat="server">
         <asp:Xml id="xml1" runat="server" />
<%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%>      </form>
</body>
</html>

You can try something like this

DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeId"; DropDownList1.DataSource = dv; DropDownList1.DataBind;

Hope this helps
 

bic (archive)

Usenet Archive
Joined
Jan 15, 2010
Messages
11
Reaction score
0
Thanks for your input Alexey. However when I tried it after clearing compiler errors I got this

<html >
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DropDownList1.DataSource = ds;
DropDownList1.DataBind;
}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Name" DataValueField="Job">
</asp:DropDownList>
</form>
</body>
</html>

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.

ALSO, I only tried it as a alternative but the requirements is to use the XSL solution. So anyone please. Thanks.

James

Anon User said:
Hi,

I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown. Also with a dropdown item selected I want all elements of that item display on the page.

Thanks in advance.

---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
<Employee>
<EmployeeId>001</EmployeeId>
<Name>Stuart</Name>
<Age>28</Age>
</Employee>
</Employees>

---------------sample1.xslt-------------

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>

---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml"));

XslTransform trans = new XslTransform;
trans.Load(Server.MapPath("peopletable.xsl"));

xml1.Document = doc;
xml1.Transform = trans;

}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%> </form>
</body>
</html>

You can try something like this

DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "EmployeeId";
DropDownList1.DataSource = dv;
DropDownList1.DataBind;

Hope this helps
.
 
G

Guest

Thanks for your input Alexey.  However when I tried it after clearing compiler errors I got this

<html  >
   <script runat="server">
      void Page_Load(Object sender, EventArgs e)
      {
          DataSet ds = new DataSet;
          ds.ReadXml(Server.MapPath("people.xml"));
          DropDownList1.DataSource = ds;
          DropDownList1.DataBind;
      }
   </script>
<head id="Head1" runat="server">
    <title>Xml Class Example</title>
</head>
<body>
   <h3>Xml Example</h3>
      <form id="form1" runat="server">
         <asp:Xml id="xml1" runat="server" />
<asp:DropDownList ID="DropDownList1" runat="server"
           DataTextField="Name" DataValueField="Job">
    </asp:DropDownList>
      </form>
</body>
</html>

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.

ALSO, I only tried it as a alternative but the requirements is to use the XSL solution.  So anyone please.  Thanks.

James

You can try something like this
        DataSet ds = new DataSet;
        ds.ReadXml(Server.MapPath("people.xml"));
        DataView dv = ds.Tables["Employee"].DefaultView; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeId"; DropDownList1.DataSource = dv; DropDownList1.DataBind;
Hope this helps
.- Hide quoted text -

- Show quoted text -

You get an error because you don't use DataView where you should specify "Employee" as a defaultview. Please look at my example (line #3).
 
G

Guest

Thanks for your input Alexey.  However when I tried it after clearing compiler errors I got this
<html  >
   <script runat="server">
      void Page_Load(Object sender, EventArgs e)
      {
          DataSet ds = new DataSet;
          ds.ReadXml(Server.MapPath("people.xml"));
          DropDownList1.DataSource = ds;
          DropDownList1.DataBind;
      }
   </script>
<head id="Head1" runat="server">
    <title>Xml Class Example</title>
</head>
<body>
   <h3>Xml Example</h3>
      <form id="form1" runat="server">
         <asp:Xml id="xml1" runat="server" />
<asp:DropDownList ID="DropDownList1" runat="server"
           DataTextField="Name" DataValueField="Job">
    </asp:DropDownList>
      </form>
</body>
</html>
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.
ALSO, I only tried it as a alternative but the requirements is to use the XSL solution.  So anyone please.  Thanks.

Anon User said:
Hi,
I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown.  Also with a dropdown item selected I want all elements of that item display on the page. Thanks in advance.
---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
  <Employee>
    <EmployeeId>001</EmployeeId>
    <Name>Stuart</Name>
    <Age>28</Age>
  </Employee>
</Employees>
---------------sample1.xslt-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>
---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html  > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml")); XslTransform trans = new XslTransform; trans.Load(Server.MapPath("peopletable.xsl")); xml1.Document = doc; xml1.Transform = trans; } </script> <head id="Head1" runat="server"> <title>Xml Class Example</title> </head> <body> <h3>Xml Example</h3> <form id="form1" runat="server"> <asp:Xml id="xml1" runat="server" /> <%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%>      </form>
</body>
</html>
- Show quoted text -

You get an error because you don't use DataView where you should specify "Employee" as a defaultview. Please look at my example (line #3).- Hide quoted text -

- Show quoted text -

Regarding XSLT. You can try to use XmlDataSource. I found an example for you that may help: http://weblogs.asp.net/rajbk/pages/431322.aspx

Let me know if you need any help with this
 

bic (archive)

Usenet Archive
Joined
Jan 15, 2010
Messages
11
Reaction score
0
Thanks Alexey. I still got this error with this code

DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView; //Object reference not set to an instance of an object. DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeId"; DropDownList1.DataSource = dv; DropDownList1.DataBind;

However, the xsl solution you gave me as you said is almost what I was looking for. Actually I already found it a few days ago but i thought I needed a little more. Alexey or anyone else for that matter, I now have a dropdown but I also need to have data of the selected displayed on the page, I.E. FirstName and LastName. Your help will be much appreciated.

James

Anon User said:
Thanks for your input Alexey. However when I tried it after clearing compiler errors I got this
<html >
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DropDownList1.DataSource = ds;
DropDownList1.DataBind;
}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Name" DataValueField="Job">
</asp:DropDownList>
</form>
</body>
</html>
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.
ALSO, I only tried it as a alternative but the requirements is to use the XSL solution. So anyone please. Thanks.

:
Hi,
I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown. Also with a dropdown item selected I want all elements of that item display on the page.
Thanks in advance.
---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
<Employee>
<EmployeeId>001</EmployeeId>
<Name>Stuart</Name>
<Age>28</Age>
</Employee>
</Employees>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>
---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml"));
XslTransform trans = new XslTransform;
trans.Load(Server.MapPath("peopletable.xsl"));
xml1.Document = doc;
xml1.Transform = trans;
}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%> </form>
</body>
</html>
You can try something like this
DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "EmployeeId";
DropDownList1.DataSource = dv;
DropDownList1.DataBind;
Hope this helps
.- Hide quoted text -
- Show quoted text -

You get an error because you don't use DataView where you should specify "Employee" as a defaultview. Please look at my example (line #3).- Hide quoted text -

- Show quoted text -

Regarding XSLT. You can try to use XmlDataSource. I found an example for you that may help: http://weblogs.asp.net/rajbk/pages/431322.aspx

Let me know if you need any help with this
.
 
G

Guest

Thanks Alexey.  I still got this error with this code

DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView; //Object reference not set to an instance of an object. DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeId"; DropDownList1.DataSource = dv; DropDownList1.DataBind;

However, the xsl solution you gave me as you said is almost what I was looking for.  Actually I already found it a few days ago but i thought I needed a little more.  Alexey or anyone else for that matter, I now have a dropdown but I also need to have data of the selected displayed on the page, I.E. FirstName and LastName.  Your help will be much appreciated.

James

Anon User said:
Thanks for your input Alexey.  However when I tried it after clearing compiler errors I got this <html  > <script runat="server"> void Page_Load(Object sender, EventArgs e) { DataSet ds = new DataSet; ds.ReadXml(Server.MapPath("people.xml")); DropDownList1.DataSource = ds; DropDownList1.DataBind; } </script> <head id="Head1" runat="server"> <title>Xml Class Example</title> </head> <body> <h3>Xml Example</h3> <form id="form1" runat="server"> <asp:Xml id="xml1" runat="server" /> <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name" DataValueField="Job"> </asp:DropDownList> </form> </body> </html>
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'. ALSO, I only tried it as a alternative but the requirements is to use the XSL solution.  So anyone please.  Thanks. James
--
bic
:
Hi,
I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown.  Also with a dropdown item selected I want all elements of that item display on the page. Thanks in advance.
---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
  <Employee>
    <EmployeeId>001</EmployeeId>
    <Name>Stuart</Name>
    <Age>28</Age>
  </Employee>
</Employees>
---------------sample1.xslt-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>
---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html  > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml")); XslTransform trans = new XslTransform; trans.Load(Server.MapPath("peopletable.xsl")); xml1.Document = doc; xml1.Transform = trans; } </script> <head id="Head1" runat="server"> <title>Xml Class Example</title> </head> <body> <h3>Xml Example</h3> <form id="form1" runat="server"> <asp:Xml id="xml1" runat="server" /> <%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%>      </form>
</body>
</html>
--
bic
You can try something like this
        DataSet ds = new DataSet;
        ds.ReadXml(Server.MapPath("people.xml"));
        DataView dv = ds.Tables["Employee"].DefaultView; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeId"; DropDownList1.DataSource = dv; DropDownList1.DataBind; Hope this helps .- Hide quoted text -
- Show quoted text -
You get an error because you don't use DataView where you should specify "Employee" as a defaultview. Please look at my example (line #3).- Hide quoted text -
- Show quoted text -
Regarding XSLT. You can try to use XmlDataSource. I found an example for you that may help:http://weblogs.asp.net/rajbk/pages/431322.aspx
Let me know if you need any help with this
.- Hide quoted text -

- Show quoted text -

Well, I don't see why you got an error. I copied the complete code to my test app and it works - I see dropdown with "Stuart" in it. Anyway, good that you see how you can use XSLT now.

Regarding your question about selected item. Make sure
AutoPostback="True" for the dropdownlist. Set OnSelectedIndexChanged to an event where you want to display selected item. Then use it to assign the value, e.g. to a Label Control on your page. For example:

Label1.Text = DropDownList1.SelectedItem.Text;

Hope this helps
 

bic (archive)

Usenet Archive
Joined
Jan 15, 2010
Messages
11
Reaction score
0
Alexey, looks like we are moving right along here. However I ran into a couple of issues. First, I was not able to find a way to set OnSelectedIndexChanged to an event. Andwith this two lines of code in page_lonnad I a m ag ain getting this. Label label1= new Label; label1.Text=DropDownList1.SelectedItem.Text; //Object reference not set to an instance of an object.
Anon User said:
Thanks Alexey. I still got this error with this code

DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView; //Object reference not set to an instance of an object. DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeId"; DropDownList1.DataSource = dv; DropDownList1.DataBind;

However, the xsl solution you gave me as you said is almost what I was looking for. Actually I already found it a few days ago but i thought I needed a little more. Alexey or anyone else for that matter, I now have a dropdown but I also need to have data of the selected displayed on the page, I.E. FirstName and LastName. Your help will be much appreciated.

James

Anon User said:
On Jan 19, 6:54 pm, bic wrote:
Thanks for your input Alexey. However when I tried it after clearing compiler errors I got this
<html >
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DropDownList1.DataSource = ds;
DropDownList1.DataBind;
}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Name" DataValueField="Job">
</asp:DropDownList>
</form>
</body>
</html>
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.
ALSO, I only tried it as a alternative but the requirements is to use the XSL solution. So anyone please. Thanks.

:
Hi,
I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown. Also with a dropdown item selected I want all elements of that item display on the page.
Thanks in advance.
---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
<Employee>
<EmployeeId>001</EmployeeId>
<Name>Stuart</Name>
<Age>28</Age>
</Employee>
</Employees>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>
---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml"));
XslTransform trans = new XslTransform;
trans.Load(Server.MapPath("peopletable.xsl"));
xml1.Document = doc;
xml1.Transform = trans;
}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%> </form>
</body>
</html>
You can try something like this
DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "EmployeeId";
DropDownList1.DataSource = dv;
DropDownList1.DataBind;
Hope this helps
.- Hide quoted text -
- Show quoted text -
You get an error because you don't use DataView where you should specify "Employee" as a defaultview. Please look at my example (line #3).- Hide quoted text -
- Show quoted text -
Regarding XSLT. You can try to use XmlDataSource. I found an example for you that may help:http://weblogs.asp.net/rajbk/pages/431322.aspx
Let me know if you need any help with this
.- Hide quoted text -

- Show quoted text -

Well, I don't see why you got an error. I copied the complete code to my test app and it works - I see dropdown with "Stuart" in it. Anyway, good that you see how you can use XSLT now.

Regarding your question about selected item. Make sure
AutoPostback="True" for the dropdownlist. Set OnSelectedIndexChanged to an event where you want to display selected item. Then use it to assign the value, e.g. to a Label Control on your page. For example:

Label1.Text = DropDownList1.SelectedItem.Text;

Hope this helps
.
 

bic (archive)

Usenet Archive
Joined
Jan 15, 2010
Messages
11
Reaction score
0
Alexey, First I want to thank you for all the help which kept me gong. I am still getting the same error also which you said you are not getting

Label label1= new Label;
label1.Text=SelectedItem.Text;
//Object reference not set to an instance of an object.

And even with this code working how do I get all the relevant data to show in my label1 besides the DropDownList1.selectedItem.Text in this case is just FirstName. How do I get it to display LastName and FirstName in label1 when OnSelectedIndexChanged occurs? BTW, could you show me how to set it to an event? Thanks very much.
Anon User said:
Thanks Alexey. I still got this error with this code

DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView; //Object reference not set to an instance of an object. DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeId"; DropDownList1.DataSource = dv; DropDownList1.DataBind;

However, the xsl solution you gave me as you said is almost what I was looking for. Actually I already found it a few days ago but i thought I needed a little more. Alexey or anyone else for that matter, I now have a dropdown but I also need to have data of the selected displayed on the page, I.E. FirstName and LastName. Your help will be much appreciated.

James

Anon User said:
On Jan 19, 6:54 pm, bic wrote:
Thanks for your input Alexey. However when I tried it after clearing compiler errors I got this
<html >
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DropDownList1.DataSource = ds;
DropDownList1.DataBind;
}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Name" DataValueField="Job">
</asp:DropDownList>
</form>
</body>
</html>
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.
ALSO, I only tried it as a alternative but the requirements is to use the XSL solution. So anyone please. Thanks.

:
Hi,
I have an sample code for an asp.net xml control to display xsl data as follows. Can someone show how to modify both xsl code and asp.net code for using this xml's name element to populate an asp.net dropdown. Also with a dropdown item selected I want all elements of that item display on the page.
Thanks in advance.
---------------sample1.xml-------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
<Employee>
<EmployeeId>001</EmployeeId>
<Name>Stuart</Name>
<Age>28</Age>
</Employee>
</Employees>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <b>Employee ID: </b><xsl:value-of select="Employees/Employee/EmployeeId" /><br/> <b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/> <b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/> </body> </html> </xsl:template> </xsl:stylesheet>
---------asp.net----------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <script runat="server"> void Page_Load(Object sender, EventArgs e) { XmlDocument doc = new XmlDocument; doc.Load(Server.MapPath("people.xml"));
XslTransform trans = new XslTransform;
trans.Load(Server.MapPath("peopletable.xsl"));
xml1.Document = doc;
xml1.Transform = trans;
}
</script>
<head id="Head1" runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
<%--<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="FirstName" DataValueField="Street"> </asp:DropDownList>
--%> </form>
</body>
</html>
You can try something like this
DataSet ds = new DataSet;
ds.ReadXml(Server.MapPath("people.xml"));
DataView dv = ds.Tables["Employee"].DefaultView;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "EmployeeId";
DropDownList1.DataSource = dv;
DropDownList1.DataBind;
Hope this helps
.- Hide quoted text -
- Show quoted text -
You get an error because you don't use DataView where you should specify "Employee" as a defaultview. Please look at my example (line #3).- Hide quoted text -
- Show quoted text -
Regarding XSLT. You can try to use XmlDataSource. I found an example for you that may help:http://weblogs.asp.net/rajbk/pages/431322.aspx
Let me know if you need any help with this
.- Hide quoted text -

- Show quoted text -

Well, I don't see why you got an error. I copied the complete code to my test app and it works - I see dropdown with "Stuart" in it. Anyway, good that you see how you can use XSLT now.

Regarding your question about selected item. Make sure
AutoPostback="True" for the dropdownlist. Set OnSelectedIndexChanged to an event where you want to display selected item. Then use it to assign the value, e.g. to a Label Control on your page. For example:

Label1.Text = DropDownList1.SelectedItem.Text;

Hope this helps
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top