简体   繁体   English

中继器中的下拉列表

[英]Dropdownlist inside a Repeater

How to start a dropdownlist with an empty values? 如何以空值开始下拉列表?

Does anyone have any suggestions as to how I can get round this problem, aside from creating a blank manager entry in the table, which is obviously not ideal! 除了在表中创建空白管理器条目之外,没有人对我如何解决此问题有任何建议,这显然不理想!

Many thanks! 非常感谢!

ASPX PAGE ASPX页面

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

CODE BEHIND: 代码如下:

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

            }
        }

You will want to insert code like this into your ItemDataBound function, after the DDL has been databound: 在对DDL进行数据绑定之后,您将希望将这样的代码插入ItemDataBound函数中:

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

Are you looking for this? 您在找这个吗?

Alter your listitem by setting AppendDataBoundItems="true" 通过设置AppendDataBoundItems="true"更改您的AppendDataBoundItems="true"

<asp:DropDownList ID="GeneralDDL" AppendDataBoundItems="true" DataTextField="DiagnosisCode" DataValueField="DiagnosisCode" 
        runat="server">
        <asp:ListItem Text="--Select--" Value=""></asp:ListItem>
    </asp:DropDownList>

Use the following: 使用以下内容:

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM