Jump to content

add value DropDownList


joecool2005

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...