Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2005, 06:29 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP.Net Jump Menu

I have a javascript jump menu that selects the last 12 months from the present, when a user selects a month, e.g May 2005 I want that selection to stay, in Classic ASP you would use Response.Write("Selected"), how do you do it in ASP.Net VB

Here is my code:

<%
Dim month_number as Integer
Dim month_counter as String
Dim year_counter as Integer
Dim theMonthNumber as String
Dim theYearNumber as String

If Request("MonthNumber") = "" Then
     theMonthNumber = Month(Now)
Else
If Request("MonthNumber") = 0 Then
         theMonthNumber = 0
Else
    theMonthNumber = Request("MonthNumber")
End If
End If

If Request("YearNumber") = "" then
  theYearNumber = Year(Now)
else
  theYearNumber = Request("YearNumber")
End if
%>
<%


theYearNumber = Year(Now)

theMonthNumber = Month(Now)
for month_number = 0 to -12 step -1
month_counter = Month(DateAdd("m", month_number, Date()))
year_counter = Year(DateAdd("m", month_number, Date()))

%>

<option value="register.aspx?MonthNumber=<%=month_counter% >&YearNumber=<%=year_counter%>"><%=MonthName(month _counter) & " " & year_counter %></option>
<% Next %>

</select>

 
Old June 1st, 2005, 06:39 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If you have an ASP dropdownlist and you programmatically change the selected value on the client, you shouldn't need to do anything to get it to stay. The postback handler of .net will automagically preset the selected item when the postback occurs.

Does this answer your question? Or are you asking how to preset a selected value in a DDL? You've posted classic ASP code so I'm not sure exactly what you are trying to accomplish.

-Peter
 
Old June 1st, 2005, 07:00 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have attempted to .Net the code, the problem now is that the drop down is empty, any help would be appreciated.

<asp:DropDownList id="ddlCompany" runat="server" AutoPostBack="True" />

Sub LoadDropDownMonth()
  If Not IsPostBack Then

    Dim month_number as Integer
    Dim month_counter as String
    Dim year_counter as Integer
    Dim theMonthNumber as String
    Dim theYearNumber as String

    If Request("MonthNumber") = "" Then
     theMonthNumber = Month(Now)
     Else
         If Request("MonthNumber") = 0 Then
             theMonthNumber = 0
         Else
             theMonthNumber = Request("MonthNumber")
         End If
    End If

    If Request("YearNumber") = "" then
        theYearNumber = Year(Now)
    else
        theYearNumber = Request("YearNumber")
    End if

    theYearNumber = Year(Now)
    theMonthNumber = Month(Now)

    for month_number = 0 to -12 step -1
    month_counter = Month(DateAdd("m", month_number, OtherDate))
    year_counter = Year(DateAdd("m", month_number, OtherDate))

    ddlCompany.DataTextField = MonthName(month_counter) & " " & year_counter
    ddlCompany.DataValueField = "register.aspx?MonthNumber=" & month_counter & "&YearNumber=" & year_counter
    ddlCompany.DataBind()

    Next

 End If
End Sub

 
Old June 1st, 2005, 07:08 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Since you have not provided a datasource for the DDL, DataBind() will be ineffective. You need to either provide a datasource or manually add the items.

Do a loop similar to what you had before, but use the following:

ddlCompany.Items.Add(New ListItem(...))

The constructor for ListItem will accept two strings, one for the value and one for the text of the item (option) in the DDL. Then you can set the desired item as selected.

-Peter
 
Old June 1st, 2005, 10:35 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, this code populates the ddl fine:

ddlCompany.Items.Add(New ListItem(...))

But how do I populate the value field, at present the value field looks like this, I want to populate the value field with a different values from the text value

<option value="June 2005">June 2005</option>
<option value="May 2005">May 2005</option>
<option value="April 2005">April 2005</option>

Thanks!

 
Old June 1st, 2005, 11:20 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

ddlCompany.Items.Add(New ListItem(text, value))

-Peter
 
Old June 1st, 2005, 11:33 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again Peter, if anyone would like to use the following code, it works fine. The code creates a drop down list of the previous 12 months. e.g. Text = August 2004, Value = 08

Function OtherDate
    OtherDate = Day(Now) &"/"& Month(Now) &"/"& Year(Now)
End Function

Sub LoadDropDownMonth()
  If Not IsPostBack Then

    Dim month_number as Integer
    Dim month_counter as String
    Dim year_counter as Integer
    Dim theMonthNumber as String
    Dim theYearNumber as String


    theYearNumber = Year(Now)
    theMonthNumber = Month(Now)

    for month_number = 0 to -12 step -1
    month_counter = Month(DateAdd("m", month_number, OtherDate))
    year_counter = Year(DateAdd("m", month_number, OtherDate))

    ddlCompany.Items.Add(New ListItem(MonthName(month_counter) & " " & year_counter, month_counter))

    Next

 End If
End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
menu in asp.net 1.1 munchy_cool ASP.NET 1.0 and 1.1 Professional 1 February 14th, 2007 05:53 AM
How do I create a jump menu in XSLT? kwilliams XSLT 4 January 9th, 2006 10:34 AM
Menu in ASP.NET 1.1 tanusree_ghosh General .NET 2 December 22nd, 2004 02:15 AM
hide jump menu? lian_a Classic ASP Basics 8 December 10th, 2004 08:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.