Assuming your
DetailsView
has its
DataSourceID
property set to
"SqlDataSource1"
, you don't need to handle the
ItemInserting
event in the code-behind. The
<%# Bind("...") %>
statements will automatically pass the selected values to the parameters on the data-source control.
EDIT: Try moving the code which populates the lists to the
Init
event of the control. This will give you the same result as declaring the items in the markup:
<InsertItemTemplate>
<asp:DropDownList ID="DropDownListDay" runat="server"
Width="50px" SelectedValue='<%# Bind("BirthDateDay") %>'
OnInit="DayList_Init"
/>
<asp:DropDownList ID="DropDownListMonth" runat="server"
Width="50px" SelectedValue='<%# Bind("BirthDateMonth") %>'
OnInit="MonthList_Init"
/>
<asp:DropDownList ID="DropDownListYear" runat="server"
Width="70px" SelectedValue='<%# Bind("BirthDateYear") %>'
OnInit="YearList_Init"
/>
</InsertItemTemplate>
protected void DayList_Init(object sender, EventArgs e)
{
var list = (DropDownList)sender;
for (int i = 1; i < 32; i++)
{
list.Items.Add(new ListItem(i.ToString());
}
}
protected void MonthList_Init(object sender, EventArgs e)
{
var list = (DropDownList)sender;
for (int i = 1; i < 13; i++)
{
list.Items.Add(new ListItem(i.ToString());
}
}
protected void YearList_Init(object sender, EventArgs e)
{
var list = (DropDownList)sender;
for (int i = 1300; i < 1396; i++)
{
list.Items.Add(new ListItem(i.ToString());
}
}