简体   繁体   English

将两个值设置为来自数据库的下拉列表

[英]Set two values to a dropdownlist from db

I have a dropdownlist inside a detailsview. 我在detailsview中有一个下拉列表。 Right now I'm using DataTextField for displaying the text, but now I wanna add one more textvalue. 现在,我正在使用DataTextField来显示文本,但是现在我想再添加一个textvalue。

My aspx: 我的aspx:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="nFuturesId"
                            DataSourceID="FutureCommodityODS" DefaultMode="Insert" OnItemInserting="DetailsView1_ItemInserting"
                            SkinID="detailsviewSkin" OnItemInserted="DetailsView1_ItemInserted">
    <Fields>
        <asp:TemplateField HeaderText="Future" SortExpression="strFutureName">
                <InsertItemTemplate>
                        <BrummerComp:SortableDropDownList ID="DropDownListFuturesInsert" runat="server" DataSourceID="FutureNameODS"
                                        DataTextField="strFutureName" DataValueField="nFuturesId" SkinID="BCdropdownlistSkin">
                        </BrummerComp:SortableDropDownList>
                </InsertItemTemplate>
        </asp:TemplateField>
    </Fields>               
</asp:DetailsView>

I have one more column that I wanna add to the DataTextField called strFutureShortName. 我想将一列添加到DataTextField中,称为strFutureShortName。 I would like it to be something like this: 我希望它是这样的:

DataTextField="strFutureName, strFutureShortName"

My idea was that I could get the DropDownList in my Page_Load and trying to add the values from there: 我的想法是我可以在我的Page_Load中获得DropDownList并尝试从中添加值:

if (!IsPostBack) {
        DropDownList ddl = (DropDownList)DetailsView1.FindControl("DropDownListFuturesInsert");
        ddl.DataTextField = "strFutureName"; //Here I would like "strFutureName" + "strFutureShortName"
    }

But this code isn't working (when I have removed attribute DataTextField in .aspx). 但是此代码不起作用(当我在.aspx中删除了属性DataTextField时)。

Does anyone know how I could do this? 有人知道我该怎么做吗?

EDIT: 编辑:

I think I found where the DDL is binded, it's using tablemapping like this: 我想我找到了绑定DDL的位置,它正在使用表映射,如下所示:

global::System.Data.Common.DataTableMapping tableMapping = new global::System.Data.Common.DataTableMapping();
tableMapping.ColumnMappings.Add("strFutureName", "strFutureName");

You can not directly add multiple value in text field. 您不能直接在文本字段中添加多个值。 You have to create a seperate property for that for your entity and then add this as a datetext field. 您必须为您的实体创建一个单独的属性,然后将其添加为datetext字段。

For example.Let's take person class 举个例子吧

public class Person
{
    public int PersonId {get;set;}
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName 
    {
        get
        {
            return string.Format("{0},{1}", FirstName, LastName);
        }
    }
}

Now you need to set that FullName as datasource. 现在,您需要将该FullName设置为数据源。

<BrummerComp:SortableDropDownList ID="DropDownListFuturesInsert" runat="server"    DataSourceID="FutureNameODS"
                                    DataTextField="FullName" DataValueField="PersonId" SkinID="BCdropdownlistSkin">
                    </BrummerComp:SortableDropDownList>

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

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