Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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 November 29th, 2007, 08:31 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Drop Down List losing value

I am attempting to create an RSVP page as I am getting married next year. However, on the drop down list which keeps track of the number of guests, the value is lost whenever it is submitted to the database. I tried setting it to AutoPostBack = true, but it resets the value back to 1 every time. As you can see, I even attempted to use a session variable to store the selected index.

I am at a loss!!! Any help, suggestions, or comments would be appreciated!

Code:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
       if (Session["List"] != null)
        {
            ((DropDownList)FormView1.FindControl("DropDownList1")).SelectedItem.Text = Session["List"].ToString();
        }
    }

    protected void List_IndexChanged(object sender, EventArgs e)
    {
        Session["List"] = ((DropDownList)FormView1.FindControl("DropDownList1")).SelectedItem.Text;
    }

    void changed(object sender, EventArgs e)
    {
        String rl = ((RadioButtonList)FormView1.FindControl("RadioButtonList1")).SelectedItem.Value;
        switch (rl)
        {
            case "0":
                FormView1.FindControl("Panel1").Visible = false;
                break;
            case "1":
                FormView1.FindControl("Panel1").Visible = true;
                break;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RSVP</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1"
                DefaultMode="Insert">
                <InsertItemTemplate>
                    First Name:
                    <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox><br />
                    Last Name: 
                    <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox><br />
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="changed"
                        AutoPostBack="true">
                        <asp:ListItem Value="0" Selected="True">Sorry, we will not be able to attend</asp:ListItem>
                        <asp:ListItem Value="1">Yes, we will gladly attend</asp:ListItem>
                    </asp:RadioButtonList>
                    <asp:Panel ID="Panel1" runat="server" Visible="false">
                        Number Attending in Party:
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"  OnSelectedIndexChanged="List_IndexChanged" SelectedValue='<%# Bind("NumberAttending") %>'> 
                            <asp:ListItem Value="1">1</asp:ListItem>
                            <asp:ListItem Value="1">2</asp:ListItem>
                            <asp:ListItem Value="1">3</asp:ListItem>
                            <asp:ListItem Value="1">4</asp:ListItem>
                            <asp:ListItem Value="1">5</asp:ListItem>
                            <asp:ListItem Value="1">6</asp:ListItem>
                            <asp:ListItem Value="1">7</asp:ListItem>
                            <asp:ListItem Value="1">8</asp:ListItem>
                            <asp:ListItem Value="1">9</asp:ListItem>
                            <asp:ListItem Value="1">10</asp:ListItem>
                            <asp:ListItem Value="1">11</asp:ListItem>
                            <asp:ListItem Value="1">12</asp:ListItem>
                            <asp:ListItem Value="1">13</asp:ListItem>
                            <asp:ListItem Value="1">14</asp:ListItem>
                            <asp:ListItem Value="1">15</asp:ListItem>
                        </asp:DropDownList><br />
                        Attending Reception:
                        <asp:TextBox ID="ReceptionTextBox" runat="server" Text='<%# Bind("Reception") %>'></asp:TextBox><br />
                    </asp:Panel>
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                        Text="RSVP"></asp:LinkButton>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                        Text="New"></asp:LinkButton>
                </ItemTemplate>
            </asp:FormView>
            &nbsp;
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tsharpConnectionString %>"
                InsertCommand="INSERT INTO [RSVP] ([Date], [FirstName], [LastName], [NumberAttending], [Reception]) VALUES (GetDate(), @FirstName, @LastName, @NumberAttending, @Reception)"
                SelectCommand="SELECT * FROM [RSVP]">
                <InsertParameters>
                    <asp:Parameter Name="Date" Type="DateTime" />
                    <asp:Parameter Name="FirstName" Type="String" />
                    <asp:Parameter Name="LastName" Type="String" />
                    <asp:Parameter Name="NumberAttending" Type="Int32" />
                    <asp:Parameter Name="Reception" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
        </div>
    </form>
</body>
</html>
 
Old November 29th, 2007, 09:10 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I think your issue lies in the fact that the value for all the items in the list is 1. Your values must be unique for you SelectedIndexChanged event to fire. hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 29th, 2007, 09:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

well.. without much enquiryng in the subject, all the values of ddl are 1..

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old November 29th, 2007, 09:15 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

wow.

i stand humbled and humiliated.

 
Old November 29th, 2007, 09:18 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

meh, nothing to be humiliated about, we all need some help from time to time.

=]

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 29th, 2007, 09:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

usually a lot of eyes looks better than a couple...

glad you work it out....

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM
Drop Down List mrideout BOOK: Beginning ASP.NET 1.0 6 March 18th, 2005 03:32 AM
Drop Down List Beginner123 VB How-To 2 March 7th, 2005 01:56 PM
Drop Down List mrideout BOOK: Beginning ASP.NET 1.0 8 September 16th, 2004 07:11 AM





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