中继器内的下拉列表

时间:2011-05-18 05:24:24

标签: c# asp.net drop-down-menu

如何使用空值启动下拉列表?

有没有人对如何解决这个问题有任何建议 问题,除了在表中创建一个空白的经理条目,其中 显然不太理想!

非常感谢!

ASPX PAGE

<asp:Repeater ID="GeneralRepeater" runat="server" 
 OnItemDataBound="GeneralRepeater_OnItemDataBound">
   <ItemTemplate>
     <tr>
      <td>
       DxPoc:
         <asp:DropDownList ID="GeneralDDL" DataTextField="DiagnosisCode" 
         DataValueField="DiagnosisCode" runat="server" />
     </td>
    </tr>
   </ItemTemplate>
</asp:Repeater>

背后的代码:

protected void GeneralRepeater_OnItemDataBound(object sender,
                                               RepeaterItemEventArgs e)


     {
            if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL");
                Diagnosis oDiagnosis = new Diagnosis();
                DataView dv = new DataView(oDiagnosis.GetDiagnosis());
                myDDL.DataSource = dv;
                myDDL.DataTextField = "DiagnosisCode";
                myDDL.DataValueField = "DiagnosisCode";
                myDDL.DataBind();

            }
        }

3 个答案:

答案 0 :(得分:0)

在DDL被数据绑定后,您需要将这样的代码插入到ItemDataBound函数中:

    ListItem LI = New ListItem("(empty item)", "0");
    myDDL.Items.Insert(0, LI);
    myDDL.SelectedValue = "0";

答案 1 :(得分:0)

你在找这个吗?

通过设置AppendDataBoundItems="true"

来更改您的列表项目
<asp:DropDownList ID="GeneralDDL" AppendDataBoundItems="true" DataTextField="DiagnosisCode" DataValueField="DiagnosisCode" 
        runat="server">
        <asp:ListItem Text="--Select--" Value=""></asp:ListItem>
    </asp:DropDownList>

答案 2 :(得分:0)

使用以下内容:

    dropDownList.DataSource = AddHeaderItem
    (
        list.ToDictionary
            (instance => instance.Key.ToString(), instance => instance.Value), 
        true, 
        "Please Select an Item..."
    );


    // Add a header item to a Dictionary ..
    public static Dictionary<String, String> AddHeaderItem
        (Dictionary<String, String> items, Boolean addHeaderItem, 
           String headerItemText = "")
    {

        var headerItem = new Dictionary<String, String>();

        if (addHeaderItem)
        {
            headerItem["-1"] = headerItemText;
        }
        //else { }

        return headerItem.Concat(items).ToDictionary
             (item => item.Key, item => item.Value);

    }