vb.net 如何使用asp.net在下拉列表中绑定图像和名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18215256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:38:45  来源:igfitidea点击:

how to bind image and name in dropdownlist using asp.net

c#asp.netvb.net

提问by sonia

I am using this code to bind dropdownlist.

我正在使用此代码绑定下拉列表。

 protected void BindDataToGridviewDropdownlist()
      {
           XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("XMLFILE.xml"));
           DataSet ds = new DataSet();
           ds.ReadXml(xmlreader);
           xmlreader.Close();


           if (ds.Tables.Count != 0)
       {
               ddlDetails.DataSource = ds;

               ddlDetails.DataTextField = "name";
               ddlDetails.DataValueField = "name";
               ddlDetails.DataBind();


    }     

 }

Its working but i am also bind image here in dropdownlist.

它的工作,但我也在下拉列表中绑定图像。

Data comes from xml file.Here is node of xml file.

数据来自xml文件。这里是xml文件的节点。

<ente>
    <name>Sydney</name>
    <img>abc_australia.png</img>
    <descri>ABC Radio Box 9994 GPO Sydney NSW 2001</descri>
    <nazione>AUSTRALIA - AUSTRALIA</nazione>
    <latitudine>-33.870652</latitudine>
    <longitudine>151.208895</longitudine>
    <zoom>-3</zoom>
  </ente>

How i can bind image here.

我如何在这里绑定图像。

采纳答案by Hanlet Esca?o

Change your function to this:

将您的功能更改为:

protected void BindDataToGridviewDropdownlist()
{
    XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("xml/XMLFILE.xml"));
    DataSet ds = new DataSet();
    ds.ReadXml(xmlreader);
    xmlreader.Close();

    if (ds.Tables.Count != 0)
    {
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            ListItem li = new ListItem(dr["name"].ToString(), dr["name"].ToString());
            li.Attributes.Add("data-image", "images/" + dr["img"].ToString());
            ddlDetails.Items.Add(li);
        }
    }

}

This will add an extra attribute to the options inside your selectBox, needed by the plugin.

这将为插件所需的 selectBox 中的选项添加一个额外的属性。