0

Is there a way to have a hash table as a select list that, when the user selects an option, it populates a paragraph for that option in a label? I have the code that displays the selected item's value, but I don't know how to add a paragraph or sentence that doesn't show up in the select list. I want a few options, and then wen they pick an option, it says something else (like something that wasn't in the list, like a paragraph or sentence about the option picked). Can anyone help with that?

I'm working with this:

<script  runat="server">
sub Page_Load
if Not Page.IsPostBack then
   dim mycountries=New Hashtable
            mycountries.Add("G", "Grooming")
            mycountries.Add("H", "Health")
            mycountries.Add("F", "Feeding")
            mycountries.Add("E", "Exercise")
            mycountries.Add("S", "Species")
   dd.DataSource=mycountries
   dd.DataValueField="Key"
   dd.DataTextField="Value"
   dd.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
end sub
</script>

<html>
<body>

<form id="Form1" runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ramesh S
0

Hi,

After that drop down declaration,declare one div for each option like

<div id=groomingmessage runat=server style="display:none">
<p>Hellp</p>
</div>

In code behind,

private void dd_selectedIndexChanged()
{
  if(dd.selectedtext == "Grooming")
{
   groomingmessage.style.add("display","block");
}
}

Thx,
Chandru

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.

0

It's not working. What am I doing wrong? Can anyone help? Here is what I added:

<script  runat="server">
sub Page_Load
if Not Page.IsPostBack then
   dim mycountries=New Hashtable
            mycountries.Add("G", "Grooming")
            mycountries.Add("H", "Health")
            mycountries.Add("F", "Feeding")
            mycountries.Add("E", "Exercise")
            mycountries.Add("S", "Species")
   dd.DataSource=mycountries
   dd.DataValueField="Key"
   dd.DataTextField="Value"
   dd.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
end sub
</script>

<html>
<body>

<form id="Form1" runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>

<div id=groomingmessage runat=server style="display:none">
<p>Hellp\</p>
</div>

</form>

</body>
</html>

page behind:

Partial Class information
    Inherits System.Web.UI.Page

End Class

private void dd_selectedIndexChanged()
{
if(dd.selectedtext == "Grooming")
{
groomingmessage.style.add("display","block");
}
}
0

Try this.

.aspx page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TestPage3.aspx.vb" Inherits="TestPage3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
    <asp:DropDownList ID="dd" runat="server" AutoPostBack="True" />
    <br />
    <asp:Label ID="lbl1" runat="server" />
    <br />
    <div id="divParagraph" runat="server">
    </div>
    </form>
</body>
</html>

VB Codebehind

Partial Class TestPage3
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim mycountries As New Hashtable
            mycountries.Add("G", "Grooming")
            mycountries.Add("H", "Health")
            mycountries.Add("F", "Feeding")
            mycountries.Add("E", "Exercise")
            mycountries.Add("S", "Species")
            dd.DataSource = mycountries
            dd.DataValueField = "Key"
            dd.DataTextField = "Value"
            dd.DataBind()
        End If

    End Sub

    Protected Sub dd_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dd.SelectedIndexChanged
        lbl1.Text = "Your favorite country is: " & dd.SelectedItem.Text

        Dim optio As String = dd.SelectedItem.Text
        Select Case optio
            Case "Grooming"
                divParagraph.InnerText = "Write your paragraph for the option 'Grooming'"
            Case "Health"
                divParagraph.InnerText = "Write your paragraph for the option 'Health'"
            Case "Feeding"
                divParagraph.InnerText = "Write your paragraph for the option 'Feeding'"
            Case "Exercise"
                divParagraph.InnerText = "Write your paragraph for the option 'Exercise'"
            Case "Species"
                divParagraph.InnerText = "Write your paragraph for the option 'Species'"
            Case Else
                divParagraph.InnerText = String.Empty
        End Select

    End Sub
End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.