我是ASP.net的新手,所以我现在才开始使用TemplateFields。我有一个DetailsView,它包含一个DropDownList,当DetailsView在EditMode中时,它会从我的代码隐藏中填充它。
这很好。但是,从我的DropDownList中选择一个项目,然后单击“更新”后,选择不会更新,但其他DetailsView表单值是。
这是我的代码:
DetailsView& SQL查询:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="315px"
AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnDataBound="DetailsView1_DataBound">
<Fields>
<asp:CommandField ShowEditButton="True" ControlStyle-CssClass="button" >
<ControlStyle CssClass="button"></ControlStyle>
</asp:CommandField>
<asp:BoundField DataField="FirstName" HeaderText="First Name"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name"
SortExpression="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="JobID" HeaderText="JobID" SortExpression="JobID" />
<asp:BoundField DataField="SupervisorID" HeaderText="Supervisor"
SortExpression="SupervisorID" />
<asp:TemplateField HeaderText="Hire Date" SortExpression="HireDate">
<EditItemTemplate>
<span class="input-type-text margin-right relative"><asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HireDate","{0:d}") %>' CssClass="datepicker"></asp:TextBox><img src="images/icons/fugue/calendar-month.png" width="16" height="16"></span>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HireDate","{0:d}") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("HireDate","{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:DropDownList ID="StatusList" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbConnectionString %>"
DeleteCommand="DELETE FROM [Users] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [Users] ([ClientID], [UserName], [Password], [FirstName], [LastName], [Email], [JobID], [SupervisorID], [HireDate], [Status]) VALUES (@ClientID, @UserName, @Password, @FirstName, @LastName, @Email, @JobID, @SupervisorID, @HireDate, @Status)"
ProviderName="<%$ ConnectionStrings:dbConnectionString.ProviderName %>"
SelectCommand="SELECT [ID], [ClientID], [UserName], [Password], [FirstName], [LastName], [Email], [JobID], [SupervisorID], [HireDate], [Status] FROM [Users] WHERE [ID] = @ID"
UpdateCommand="UPDATE [Users] SET [ClientID] = @ClientID, [UserName] = @UserName, [Password] = @Password, [FirstName] = @FirstName, [LastName] = @LastName, [Email] = @Email, [JobID] = @JobID, [SupervisorID] = @SupervisorID, [HireDate] = @HireDate, [Status] = @StatusList WHERE [ID] = @ID">
<SelectParameters>
<asp:QueryStringParameter Name="ID" QueryStringField="curEmp" Type="Int64" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ClientID" Type="Int64" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="JobID" Type="Double" />
<asp:Parameter Name="SupervisorID" Type="Double" />
<asp:Parameter Name="HireDate" Type="DateTime" />
<asp:Parameter Name="StatusList" Type="String" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
代码背后:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList statusList = DetailsView1.FindControl("StatusList") as DropDownList;
//Add list items (later to be added by database)
if (statusList != null)
{
statusList.Items.Add(new ListItem("Select Status", ""));
statusList.Items.Add(new ListItem("Active", "Active"));
statusList.Items.Add(new ListItem("Inactive", "Inactive"));
statusList.Items.Add(new ListItem("Terminated", "Terminated"));
statusList.DataBind();
}
//Highlight selected item in dropdownlist while in edit mode
DataRowView row = (DataRowView)DetailsView1.DataItem;
ListItem liItem = statusList.Items.FindByText(row["Status"].ToString());
if (liItem != null)
{
statusList.Items.FindByText(row["Status"].ToString()).Selected = true;
}
}
}
我不知道从哪里开始。我试图在我的SQLDataSource中为DropDownList(名为StatusList)更改Update命令,但没有任何运气。知道我在这里缺少什么吗?
感谢您的帮助!
答案 0 :(得分:0)
我认为statusList不会绑定到您的数据源。您可能需要绑定aspx或数据源中的下拉列表。
所以,就像在aspx中一样...
<asp:DropDownList ID="statusList" runat="server" SelectedValue='<%# Bind("datafield") %>'>
或在数据源参数中......
<asp:ControlParameter Name="status" ControlID="DetailsView1$statusDropDownList" PropertyName="SelectedValue" Type="String" />