如何从下拉列表中将选定的值传递给数据库? [英] How do I pass a selected value to database from dropdownlist?
本文介绍了如何从下拉列表中将选定的值传递给数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在插入模式下有一个DetailsView。我希望用户通过3 Dropdownlist输入日期。但每次它总是将所有项目的相同值发送到数据库并且所选值不通过。
(!Ispostback),(IndexChangig)方法没有做任何改变。这是我的代码:
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:
<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>
和page_load背后的代码:
And the code behind for page_load :
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()));
}
以及ItemInserting事件背后的代码:
And the code behind for ItemInserting event:
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;
}
推荐答案
假设你的DetailsView
有DataSourceID
属性设置为SqlDataSource1
,您不需要处理ItemInserting
代码隐藏中的事件。<%#Bind(...)%>
语句会自动将所选值传递给数据源控件上的参数。
编辑:尝试将填充列表的代码移动到Init
事件中控制。这将为您提供与声明标记中的项目相同的结果:
Assuming yourDetailsView
has itsDataSourceID
property set to"SqlDataSource1"
, you don't need to handle theItemInserting
event in the code-behind. The<%# Bind("...") %>
statements will automatically pass the selected values to the parameters on the data-source control.
Try moving the code which populates the lists to theInit
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());
}
}
这篇关于如何从下拉列表中将选定的值传递给数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文