Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DetailsView in insert mode. I want to user enter the date by 3 Dropdownlist.But each time it always send same value for all of the item to database and selected value doesn't pass.

(!Ispostback),(IndexChangig) Methods didn't make any change. here is my code:
HTML
<InsertItemTemplate>

                           <asp:DropDownList ID="DropDownListDay" runat="server" Width="50px" SelectedValue='<%# Bind("BirthDateDay") %>'  >
                            </asp:DropDownList>
                            <asp:DropDownList ID="DropDownListMonth" runat="server" Width="50px" SelectedValue='<%# Bind("BirthDateMonth") %>'>
                            </asp:DropDownList>
                            <asp:DropDownList ID="DropDownListYear" runat="server" Width="70px" SelectedValue='<%# Bind("BirthDateYear") %>'>
                            </asp:DropDownList>
                            <br />
                        </InsertItemTemplate>

And the code behind for page_load :
C#
DropDownList dlDay = (DropDownList)DetailsView1.FindControl("DropDownListDay");
           DropDownList dlMonth = (DropDownList)DetailsView1.FindControl("DropDownListMonth");
           DropDownList dlYear = (DropDownList)DetailsView1.FindControl("DropDownListYear");

           for (int i = 1; i < 32; i++)
           {
               dlDay.Items.Add(new ListItem(i.ToString(), i.ToString()));

           }
           for (int j = 1; j < 13; j++)
           {
               dlMonth.Items.Add(new ListItem(j.ToString(), j.ToString()));

           }
           for (int k = 1300; k < 1396; k++)
           {
               dlYear.Items.Add(new ListItem(k.ToString(), k.ToString()));

           }

And the code behind for ItemInserting event:
C#
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {


            DropDownList dlDay = (DropDownList)DetailsView1.FindControl("DropDownListDay");
            DropDownList dlMonth = (DropDownList)DetailsView1.FindControl("DropDownListMonth");
            DropDownList dlYear = (DropDownList)DetailsView1.FindControl("DropDownListYear");

            SqlDataSource1.InsertParameters["BirthDateDay"].DefaultValue =dlDay.SelectedValue;
            SqlDataSource1.InsertParameters["BirthDateMonth"].DefaultValue = dlMonth.SelectedValue;
            SqlDataSource1.InsertParameters["BirthDateYear"].DefaultValue = dlYear.SelectedValue;
}
Posted
Updated 16-Jun-15 23:52pm
v2

1 solution

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

C#
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());
    }
}
 
Share this answer
 
v2
Comments
MohammadReza90 17-Jun-15 9:02am    
Thank you Richard for your reply,I remove that part of code but again default value return to database
Richard Deeming 17-Jun-15 9:21am    
I assume your Page_Load code is inside an if (!IsPostBack) { ... } block?
MohammadReza90 17-Jun-15 9:24am    
No it doesn't;
When I use (!Ispostback) block Null value send to database at all;
Richard Deeming 17-Jun-15 9:26am    
OK, try moving the code that populates the drop-down list to the control's Init event. I'll update my answer with an example.
MohammadReza90 17-Jun-15 9:37am    
Thank you very veryyyyyyyyy much! It worked! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900