Jump to content

add value DropDownList


joecool2005
 Share

Recommended Posts

Hi,I have a DropDownList like this

<asp:DropDownList id=gender runat="server">	<asp:ListItem>Female</asp:ListItem>	<asp:ListItem>Male</asp:ListItem></asp:DropDownList>

How can I add value for each item?Ex: for female, the value will be 1 for male, the value will be 2 By calling, Request("gender") I should receive 1 or 2 as a valueThx

Link to comment
Share on other sites

asp:ListItem has a "Value" attribute. <asp:ListItem Value="0">Female</asp:ListItem><asp:ListItem Value="1">Male</asp:ListItem>Like all web controls, since the DropDownList just renders as a form element (a SELECT tag), its value can be retrieved from the Request object. For example, to retrieve the value of the control and output it to the screen, we might use code like this Request("gender")

Link to comment
Share on other sites

Thanx it's woking.But when I add a master page is not working anymoreWhy?Master page(MasterPage.master)

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server">   	</head><body>	test<asp:ContentPlaceHolder id="CPH1" runat="server"></asp:ContentPlaceHolder> </body></html>

Page(default.aspx)

<%@ Page Language="VB"  MasterPageFile="MasterPage.master"%><asp:Content ID="Content1" ContentPlaceHolderId="CPH1" runat="server"><script  runat="server">	Sub test(ByVal s As Object, ByVal e As EventArgs)		Response.Write(Request("gender"))	End Sub</script><html><body><form id="Form1" runat="server"><asp:DropDownList id=gender runat="server" OnSelectedIndexChanged="test" AutoPostBack="True" ><asp:ListItem Value="0">Female</asp:ListItem><asp:ListItem Value="1">Male</asp:ListItem></asp:DropDownList>			</form></body></html></asp:Content>

Link to comment
Share on other sites

My guess is because you have multiple HTML and BODY elements in your pages. Take out the HTML and BODY elements in your page because they already exist in the Master Page.

Link to comment
Share on other sites

I removed the <body > on my page and I still have the same problemMasterPage.master

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server">   	</head><body>	test<asp:ContentPlaceHolder id="CPH1" runat="server"></asp:ContentPlaceHolder> </body></html>

default.aspx

<%@ Page Language="VB"  MasterPageFile="MasterPage.master"%><asp:Content ID="Content1" ContentPlaceHolderId="CPH1" runat="server"><script  runat="server">	Sub test(ByVal s As Object, ByVal e As EventArgs)		Response.Write(Request("gender"))	End Sub</script><form id="Form1" runat="server"><asp:DropDownList id=gender runat="server" OnSelectedIndexChanged="test" AutoPostBack="True" ><asp:ListItem Value="0">Female</asp:ListItem><asp:ListItem Value="1">Male</asp:ListItem></asp:DropDownList>			</form></asp:Content>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...