1

我还是 ASP.net 的新手,但一直在寻找如何在编辑模式下使用后面的代码将项目添加到 DetailsView 的 DropDownList 中。我遇到的一切都没有工作,但也许我应用不正确。

DetailsView 以只读模式启动。我读到我必须在 DataBound 事件中使用 FindControl。这是我到目前为止所拥有的,但它没有增加任何价值。

页面

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

ASP.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();

        }

    }


}

谁能指出我在这里缺少什么?谢谢你的帮助!

4

1 回答 1

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();
                }
            }
        }

编辑:: 现在要在编辑模式下设置下拉列表的选定值,使用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;
  }
于 2013-09-27T07:31:09.363 回答