I have this that gets bound in my code behind:

 <asp:DropDownList id="ddlPopulation" runat="server" DataTextField="population" DataValueField="pid" AppendDataBoundItems="True">
<asp:ListItem>Default</asp:ListItem>

How do I alter the list items in the code behind? I want to do an Html_Decode and Trim on them before they are rendered to the user?

The DataBind code is:

 StringBuilder sql = new StringBuilder();

    // Define sql
    sql.Append("SELECT DISTINCT datasource ");
    sql.Append("FROM meta ");
    sql.Append("WHERE datasource != '' ");
    sql.Append("ORDER BY datasource ASC ");

    IDataReader reader = SqlHelper.GetDataReader(sql.ToString());

    ddlDatasources.DataSource = reader;
    ddlDatasources.DataBind();
有帮助吗?

解决方案

You can subscribe to the DataBound event of the DropDownList and do something like the following:

<asp:DropDownList id="ddlPopulation" runat="server" DataTextField="population" DataValueField="pid" AppendDataBoundItems="True" OnDataBound="ddlPopulation_DataBound">
    </asp:DropDownList>

and

protected void ddlPopulation_DataBound(object sender, EventArgs e) {
  foreach(ListItem Item in ddlPopulation.Items){
    Item.Text = Server.HtmlDecode(Item.Text.Trim());
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top