Jump to content

Recommended Posts

How can I add a value to what displays from a function in my class when a user chooses an option from my drop down menu?

 

Here is my drop down menu:

 

<select id="dd">

    <option>Red</option>

    <option>Yellow</option>

    <option>Green</option>

    </select>

 

Then here is my class:

 

<script runat="server">

    Class PetName

        Public FirstName As String

       

        Function GetName() As String

            Dim Whole As String

            Whole = FirstName

            Return (Whole)

        End Function

    End Class

</script>

 

And here is where I set the value to be displayed (I currently have it set to a literal value, but I would like to set it to a value that is set when the user chooses an option in the select menu):

 

<%

        Dim pet1 As PetName

        pet1 = New PetName

        pet1.FirstName = "Bob"

    %>

 

And here is where it displays the value on the page:

 

<p>Suggested Pet Name: <%=pet1.GetName%></p>

 

I know I can probably use an if/else statement, but how do I set it up? Do I set it within my Dim statement? If so, how?

 

Say I want to do this:

 

if dd.SelectedItem.Text = "Red" Then pet1.FirstName = "Sunny"

 

elseif dd.SelectedItem.Text = "Blue Then pet1.FirstName = "Ices"

 

Am I on the right track? How do I actually do this?

 

 

Link to comment
https://forums.phpfreaks.com/topic/177137-solved-aspnet-class-question/
Share on other sites

Ah!  VB!  Run!! :P

 

First, be sure to put runat=server in your select server control.  Otherwise, your script won't be able to get a hold of the value.

 

I think you're on the right track.  I'm not too familiar with using inline coding - I prefer to use a pure code-behind file - but I don't see why the basics of what you're trying to do won't work.  You should clean up your class a bit, though.  Despite its small size, it could be better.  I'd do either (using C#):

 

public class PetName
{
   private string firstName;

   public PetName(string name)
   {
      this.firstName = name;
   }

   public string FirstName
   {
      get { return this.name; }
      set { this.name = value; }
   }
}

 

To treat it like a full-fledged class, or:

 

public struct PetName
{
   public string FirstName;
}

 

To treat it as plain old data.

 

Also, to do what you want to do, you'd most likely need to tie it into the select's onchange event.

 

Do you merely need to store the value of the name, or do you need to display it, too?

I need to display it back to the user. Say they select a color, like red, from the option list. Then it will say, next to the Suggested Pet Name, whatever value I set to be displayed. Like, if the user chooses red, I'd like to display the name, Sunny. How would I do that? I added the dropdown list in place of the other one:

 

<asp:DropDownList ID="DropDownList1" runat="server">

        <asp:ListItem>Red</asp:ListItem>

        <asp:ListItem>Blue</asp:ListItem>

        <asp:ListItem>Yellow</asp:ListItem>

        <asp:ListItem>Green</asp:ListItem>

    </asp:DropDownList>

 

I could do it in C# if you could show me how!

Actually, now that I'm more awake, you don't need another structure (class or struct) to do what you want.  You simply need an event handler.

 

So, this is what I came up with -

 

test.aspx:

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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">
    <div>
        <asp:DropDownList ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" runat="server">
            <asp:ListItem>--</asp:ListItem>
            <asp:ListItem Value="Red">Red</asp:ListItem>
            <asp:ListItem Value="Yellow">Yellow</asp:ListItem>
            <asp:ListItem Value="Green">Green</asp:ListItem>
            <asp:ListItem Value="Blue">Blue</asp:ListItem>
        </asp:DropDownList>
        
        <br /><br />
        
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

 

That should be pretty straight forward.  Setting DropDownList1's AutoPostBack to true is essential in getting this to work.

 

test.aspx.cs:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    public void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch(DropDownList1.SelectedValue)
        {
            case "Red":
                    Label1.Text = "Rusty";
                    break;

            case "Yellow":
                    Label1.Text = "Sunny";
                    break;

            case "Green":
                    Label1.Text = "Grassy";
                    break;

            case "Blue":
                    Label1.Text = "Icy";
                    break;

            default:
                Label1.Text = "Bubba";
                break;
        }
    }

    protected void Page_Load(object sender, EventArgs e){ }
}

 

Again, pretty straight forward.  Just to recap what's going on:

 

DropDownList1 has an event that fires whenever you change its selection - OnSelectedIndexChanged.  In order to do something with the event, you need to tie it to an event handler function.  In this case, the event handler is called DropDownList1_SelectedIndexChanged.

 

The event handler obtains the value of the list's current selected index, and fills Label1 with the correct text.

 

AutoPostBack is needed because there's no submit button to send the info to the server so the function can fire.  AutoPostBack allows the info to be submitted whenever the list's index changes.

 

If you don't like the fact that a page refresh is necessary for it to work, you could always do it in straight JavaScript.  Since you're not accessing any data that lies on the server (database, XML files, etc), there's no real reason to use .NET for something like this.  Unless, of course, this is for educational purposes.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.