ASP.Net/Asp Control/ComboBox

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Get selected item from a ComboBox and check its value (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Panel1.Visible = True
        Panel2.Visible = False
        lblTitle.Text = "Quiz Page"
    End If
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim TotalCorrect as Integer
    Panel1.Visible = False
    Panel2.Visible = True
    lblTitle.Text = "Results Page"
    If DDLAnswer1.SelectedItem.Text = "Second" Then
        TotalCorrect = TotalCorrect + 1
    End If
    If DDLAnswer2.SelectedItem.Text = "False" Then
        TotalCorrect = TotalCorrect + 1
    End If
    If DDLAnswer3.SelectedItem.Text = "False" Then
        TotalCorrect = TotalCorrect + 1
    End If
    lblResult.Text = "You scored " & TotalCorrect _
        & " out 3 correct."
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Quiz Sample Page</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma">
<asp:Label 
    id="lblTitle" 
    runat="server"
/>
<asp:Panel 
    id="Panel1" 
    runat="server"
>
<asp:Label
    id="lblQuestion1"
    runat="server"
    Text="Question 1"
/>
<BR>
<asp:DropDownList 
    id="DDLAnswer1" 
    runat=server>
    <asp:ListItem>Hour</asp:ListItem>
    <asp:ListItem>Minute</asp:ListItem>
    <asp:ListItem>Second</asp:ListItem>
</asp:DropDownList>
<BR><BR>
<asp:Label
    id="lblQuestion2"
    runat="server"
    Font-Bold="True"
    Text="Question 2."
/>
<BR>
<asp:DropDownList 
    id="DDLAnswer2" 
    runat=server>
    <asp:ListItem>True</asp:ListItem>
    <asp:ListItem>False</asp:ListItem>
</asp:DropDownList>
<BR><BR>
<asp:Label
    id="lblQuestion3"
    runat="server"
    Font-Bold="True"
    Text="Question 3"
/>
<BR>
<asp:DropDownList 
    id="DDLAnswer3" 
    runat=server>
    <asp:ListItem>True</asp:ListItem>
    <asp:ListItem>False</asp:ListItem>
</asp:DropDownList>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</asp:Panel>
<asp:Panel 
    id="Panel2" 
    runat="server"
>
<asp:Label
    id="lblResult"
    runat="server"
    Font-Bold="True"
/>
</asp:Panel>
</Font>
</Form>
</BODY>
</HTML>



Get selected value from a combobox and display (C#)

<%@ Page Language="c#" %>
<script runat="server">
    void Page_Load()
    {
      if(Page.IsPostBack)
        lblMessage.Text = "You have selected " + DropList1.SelectedItem.Value;
    }
</script>
<html>
    <head>
    </head>
    <body>
        <form runat="server">
        <asp:DropDownList ID="DropList1" runat="server">
        <asp:listitem>apple</asp:listitem>
        <asp:listitem>banana</asp:listitem>
        <asp:listitem>carrot</asp:listitem>
        <asp:listitem>durian</asp:listitem>
        </asp:DropDownList><br/>
        <asp:Button runat="server" Text="Button"></asp:Button><br/>
        <asp:Label ID="lblMessage" runat="server"/>
        </form>
    </body>
</html>



Set static value for a combobox (asp:dropdownlist) (VB.net)

<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic DropDownList Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Select a Color:<BR>
<BR><BR>
<asp:dropdownlist id="ddl1" runat="server">
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" Selected>Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
    <asp:listitem value="Go" text="Gold"/>
</asp:dropdownlist>
</form>
</BODY>
</HTML>