在编辑模式下将项添加到DetailsView DropDownList

时间:2013-09-26 19:37:52

标签: c# asp.net

我还是ASP.net的新手,但是一直在寻找如何在编辑模式下使用代码来向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();

        }

    }


}

有谁可以指出我在这里缺少的东西?谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

使用DataBound控件的DetailsView事件。

<asp:DetailsView ID="DetailsView1" runat="server" 
     OnDataBound="DetailsView1_DataBound" ...>
</asp:DetailsView>

使用以下方法检查此事件中的EditMode

DetailsView.CurrentMode = DetailsViewMode.Edit

protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
         if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
          {
            DropDownList statusList = DetailsView1.FindControl("StatusList") as
                                      DropDownList;
                if (statusList != null)
                {
                    statusList.Items.Add(new ListItem("Test", "Test"));
                    statusList.DataBind();
                }
            }
        }

修改:: 现在,要在编辑模式下设置Dropdownlist的选定值,请使用Details视图的DataItem属性来获取当前项。 数据项通常用于直接访问当前记录的字段值。

 DataRowView row = (DataRowView)DetailsView1.DataItem
  if (row != null)
   {
     statusList.SelectedIndex =                          
     statusList.Items.IndexOf(ddl.Items.FindByValue(row["Column_Name"].ToString());
   }

或试试这个:

DataRowView row = (DataRowView)DetailsView1.DataItem
ListItem liItem = statusList.Items.FindByText(row["Column_Name"].ToString());
if (liItem != null)
  {
    statusList.Items.FindByText(row["Column_Name"].ToString()).Selected = true;
  }