1
我還是新來的ASP.net,但一直在尋找如何使用代碼在編輯模式下添加項目到DetailsView的DropDownList。我遇到的一切都沒有工作,但也許我錯誤地應用了它。在編輯模式下添加項目到DetailsView的DropDownList
DetailsView以ReadOnly模式啓動。我讀過我必須在DataBound事件中使用FindControl。這是迄今爲止我所擁有的,但並沒有增加任何價值。
ASPX頁面
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="315px"
AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<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" SelectedValue='<%# Bind("Status") %>'>
</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] = @Status 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="Status" Type="String" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
Aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class EmployeeProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList statusList = DetailsView1.FindControl("StatusList") as DropDownList;
statusList.Items.Add(new ListItem("Test", "Test"));
statusList.DataBind();
}
}
}
任何人都可以指出什麼,我在這裏失蹤?謝謝你的幫助!
非常感謝這個!我錯過了DetailsView的開始標記中的OnDataBind。我不得不拿掉DropDownList中的SelectedValue,因爲我得到錯誤。我想我必須找到另一種方法來設置選定的值?這有助於在編輯模式下添加項目,但一旦我更新了選擇,它似乎並沒有更新我的選擇。我錯過了別的嗎? – Cineno28
編輯我的答案,在編輯模式下設置dropdownlist的選定值。此外,如果更新沒有發生,你應該發佈你的更新代碼,任何更新/更新事件的詳細信息視圖等。或者你正在做什麼來做更新,如果有的話,或者更好地問這個 –
謝謝你的新問題謝謝很多爲增加的編輯。我假設其中的「ddl」應該是Dropdownlist的名稱是否正確?在這種情況下「StatusList」?如果需要,我會繼續處理更新問題併發布另一個問題。再次感謝你的幫助! – Cineno28