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>
<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>