Jump to content

add value DropDownList


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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...