简体   繁体   中英

Set two values to a dropdownlist from db

I have a dropdownlist inside a detailsview. Right now I'm using DataTextField for displaying the text, but now I wanna add one more textvalue.

My 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. 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:

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

Does anyone know how I could do this?

EDIT:

I think I found where the DDL is binded, it's using tablemapping like this:

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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