By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,854 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,854 IT Pros & Developers. It's quick & easy.

C#-WEB: getting selected menuitem to display in textbox

P: 2
Just to start off, I am writing a web application using C#. I have a Menu which is binded to an XML datasource. There are three menu items each with submenus and a textbox where I want to display the value of the selected menu item. Heres the XML datasource that I am using:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Home>
  3.   <Menu text="Patient" description=" ">
  4.     <SubMenu text="First Name" description="firstName"></SubMenu>
  5.     <SubMenu text="Last Name" description="lastName"></SubMenu>
  6.     <SubMenu text="Birth Date" description="birthDate"></SubMenu>
  7.     <SubMenu text="Gender" description="gender"></SubMenu>
  8.     <SubMenu text="SSN" description="ssn"></SubMenu>
  9.   </Menu>
  10.   <Menu text="Contact"  description=" ">
  11.     <SubMenu text="Address" description="ptAddress"></SubMenu>
  12.     <SubMenu text="City" description="city"></SubMenu>
  13.     <SubMenu text="State" description="ptState"></SubMenu>
  14.     <SubMenu text="Zip" description="addPhone"></SubMenu>
  15.     <SubMenu text="Home Phone" description="dayPhone"></SubMenu>
  16.     <SubMenu text="Day Phone" description="dayPhone"></SubMenu>
  17.     <SubMenu text="Mobile Phone" description="mobilePhone"></SubMenu>
  18.     <SubMenu text="Email" description="email"></SubMenu>
  19.   </Menu>
  20.   <Menu text="Exam Info" description=" ">
  21.     <SubMenu text="Last Exam" description="lastExam"></SubMenu>
  22.     <SubMenu text="Is former patient(yes/no)" description="isFormPatient"></SubMenu>
  23.   </Menu>
  24. </Home>
  25.  
Now when I select any of the submenu values under the "Patient" menu, the value of the selected menuitem is displayed properly. If I click on any of the other submenu items under "Contact" or "Exam Info", nothing gets displayed in the textbox.

Here is my HTML code and C# code:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource>
  13.         <br />
  14.     <asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSource1" MaximumDynamicDisplayLevels="4" DynamicHorizontalOffset="1" DynamicVerticalOffset="1" OnMenuItemClick="Menu1_MenuItemClick">
  15.               <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
  16.               <DynamicHoverStyle BackColor="#990000" Font-Bold="False" 
  17.                 ForeColor="White" />
  18.               <DynamicMenuStyle BackColor="#FFFBD6" />
  19.                <StaticSelectedStyle BackColor="#FFCC66" />
  20.               <DynamicSelectedStyle BackColor="#FFCC66" />
  21.               <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
  22.             <DataBindings>
  23.                 <asp:MenuItemBinding DataMember="Menu" TextField="text" ValueField="description" />
  24.                 <asp:MenuItemBinding DataMember="SubMenu" TextField="text" ValueField="description" />
  25.             </DataBindings>
  26.               <StaticHoverStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />
  27.         </asp:Menu>
  28.         <br />
  29.         <br />
  30.         <asp:TextBox ID="TextBox1" runat="server" Width="520px"></asp:TextBox></div>
  31.     </form>
  32. </body>
  33. </html>
  34.  
My C# code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10.  
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.  
  16.     }
  17.     protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
  18.     {
  19.         TextBox1.Text += Menu1.SelectedItem.Value;
  20.     }
  21. }
  22.  
Also, if i change my c# code to
Expand|Select|Wrap|Line Numbers
  1. protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
  2. {
  3.       TextBox1.Text += Menu1.SelectedItem.Text;
  4. }
  5.  
Whenever I click on "Contact" or "Exam Info", I get the value of "Patient"
Any ideas or suggestions?
Thanks in advanced.
Jan 11 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.