System.Data.DataRowView in DropDownList

前端 未结 3 1295
不思量自难忘°
不思量自难忘° 2021-01-06 15:39

I am trying to populate a dropdown using values from a column. Now the problem is: I am not getting the actual values (the country codes like India(+61)) in the dropdown. In

相关标签:
3条回答
  • 2021-01-06 15:45

    You haven't set the DataTextField in the DropDownList. It's recommended to set also the DataValueField In your aspx add the DataTextField property:

    <asp:DropDownList ID="ddlMobile" runat="server" 
                      DataTextField="CountryCode"
                      DataValueField="CountryCode" />
    

    You can also set it in the code behind, like the other answers show.

    Otherwise the behaviour that you are seeing, is because the DataBound is calling the ToString() to display the info, as you don't provided wich data field look for.

    0 讨论(0)
  • 2021-01-06 15:52

    You are seeing what the default implementation of DataRowView.ToString() does. To pick specific fields from within the DataRow to display, do something like this.

    ddlMobile.DataSource = ds1.Tables["AllUser"];
    ddlMobile.DataTextField = "CountryCode"; // This is text displayed
    ddlMobile.DataValueField = "CountryCode"; // This is the value returned
    ddlMobile.DataBind();
    
    0 讨论(0)
  • 2021-01-06 16:01

    You should set the DataValueField and DataTextField Properties of the drop down.

    ddlMobile.DataSource = ds1.Tables["AUser"];
    ddlMobile.DataValueField = "CountryCode";
    ddlMobile.DataTextField = "CountryName";
    ddlMobile.DataBind();
    

    Here CountryCode and CountryName must be the column names corresponding to those values in your DataRow

    0 讨论(0)
提交回复
热议问题